node_modules

This commit is contained in:
saeid
2019-07-15 13:07:31 +04:30
parent d3735c6d30
commit ce1a3ebf9c
35713 changed files with 3639219 additions and 1 deletions
+3
View File
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.idea
+112
View File
@@ -0,0 +1,112 @@
# over
JavaScript function overloading framework.
## Installation
```bash
$ npm install over
```
## Quick Examples
```javascript
var over = require('over');
var myfn = over([
[over.string, function (str) { console.log('got a string' + str); }],
[over.string, over.numberOptionalWithDefault(5), over.callbackOptional, function (str, number, callback) {
console.log('got a string and a number and a callback');
callback(str, number);
}],
function() {
// default function if nothing else matches
}
]);
```
## Builtin Test functions
* func
* funcOptional
* funcOptionalWithDefault
* callbackOptional // Will return an empty function of parameter is not given
* string
* stringOptional
* stringOptionalWithDefault
* number
* numberOptional
* numberOptionalWithDefault
* array
* arrayOptional
* arrayOptionalWithDefault
* object
* objectOptional
* objectOptionalWithDefault
The built in optional functions with a suffix of "WithDefault" take a default value as well which will be used if
it is not passed in.
```javascript
var myfn = over([
[over.stringOptionalWithDefault('default value'), function (str) { console.log('got a string' + str); }],
]);
```
## Write your own test functions
Simple optional test
```javascript
function greaterThan5Optional(arg) {
return arg > 5;
}
greaterThan5Optional.optional = true; // mark it as an optional parameter
var myfn = over([
[greaterThan5Optional, function (v) { console.log('got a value' + v); }]
]);
```
Optional test with default
```javascript
function greaterThan5OptionalWithDefault(def) {
return function greaterThan5OptionalWithDefault2(arg) {
if (arg === undefined) {
return { defaultValue: def };
}
return arg > 5;
}
}
greaterThan5OptionalWithDefault.optional = true; // mark it as an optional parameter
var myfn = over([
[greaterThan5OptionalWithDefault, function (v) { console.log('got a value' + v); }]
]);
```
## License
(The MIT License)
Copyright (c) 2012 Near Infinity Corporation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+236
View File
@@ -0,0 +1,236 @@
'use strict';
// overloadDefs
// self, overloadDefs
var overload = module.exports = function () {
var self, selfSet = false, overloadDefs;
if (arguments.length === 1) {
overloadDefs = arguments[0];
} else {
selfSet = true;
self = arguments[0];
overloadDefs = arguments[1];
}
return function () {
if (!selfSet) {
self = this;
}
var args = Array.prototype.slice.call(arguments);
var overloadMatchData = findOverload(overloadDefs, args);
if (!overloadMatchData) {
throw new Error(createErrorMessage('No match found.', overloadDefs));
}
var overloadFn = overloadMatchData.def[overloadMatchData.def.length - 1];
return overloadFn.apply(self, overloadMatchData.args);
};
};
var findOverload = overload.findOverload = function (overloadDefs, args) {
for (var i = 0; i < overloadDefs.length; i++) {
if (i === overloadDefs.length - 1 && typeof(overloadDefs[i]) === 'function') {
return { args: args, def: [overloadDefs[i]] };
}
var newArgs;
if (newArgs = isMatch(overloadDefs[i], args)) {
return { args: newArgs, def: overloadDefs[i] };
}
}
return null;
};
function isMatch(overloadDef, args) {
var overloadDefIdx;
var argIdx;
var newArgs = [];
for (overloadDefIdx = 0, argIdx = 0; overloadDefIdx < overloadDef.length - 1; overloadDefIdx++) {
if (typeof(overloadDef[overloadDefIdx]) !== 'function') {
throw new Error("Invalid overload definition. Array should only contain functions.");
}
//console.log('overloadDef/arg:', overloadDef[overloadDefIdx], args[argIdx]);
var result = overloadDef[overloadDefIdx](args[argIdx]);
//console.log('result:', result);
if (result) {
if (result.hasOwnProperty('defaultValue')) {
newArgs.push(result.defaultValue);
} else {
if (overloadDef[overloadDefIdx].optional && args[argIdx] === null) {
argIdx++;
newArgs.push(overloadDef[overloadDefIdx].defaultValue);
continue;
}
newArgs.push(args[argIdx]);
argIdx++;
}
} else {
if (overloadDef[overloadDefIdx].optional) {
newArgs.push(overloadDef[overloadDefIdx].defaultValue);
continue;
}
return false;
}
}
//console.log('compares', overloadDefIdx, overloadDef.length - 1, argIdx, args.length, newArgs.length);
if (overloadDefIdx === overloadDef.length - 1 && argIdx >= args.length) {
return newArgs;
}
return false;
}
function createErrorMessage(message, overloadDefs) {
message += '\n';
message += ' Possible matches:\n';
for (var i = 0; i < overloadDefs.length; i++) {
var overloadDef = overloadDefs[i];
if (typeof(overloadDef) === 'function') {
message += ' [default]\n';
} else {
var matchers = overloadDef.slice(0, overloadDef.length - 1);
matchers = matchers.map(function (m) {
if (!m) {
return '[invalid argument definition]';
}
return m.name || m;
});
if (matchers.length === 0) {
message += ' ()\n';
} else {
message += ' (' + matchers.join(', ') + ')\n';
}
}
}
return message;
}
// --- func
overload.func = function func(arg) {
return typeof(arg) === 'function';
};
overload.funcOptional = function funcOptional(arg) {
if (!arg) {
return true;
}
return overload.func(arg);
};
overload.funcOptional.optional = true;
overload.funcOptionalWithDefault = function (def) {
var fn = function funcOptionalWithDefault(arg) {
if (arg === undefined) {
return false;
}
return overload.func(arg);
};
fn.optional = true;
fn.defaultValue = def;
return fn;
};
// --- callback
overload.callbackOptional = function callbackOptional(arg) {
if (!arg) {
return { defaultValue: function defaultCallback() {} };
}
return overload.func(arg);
};
overload.callbackOptional.optional = true;
// --- string
overload.string = function string(arg) {
return typeof(arg) === 'string';
};
overload.stringOptional = function stringOptional(arg) {
if (!arg) {
return true;
}
return overload.string(arg);
};
overload.stringOptional.optional = true;
overload.stringOptionalWithDefault = function (def) {
var fn = function stringOptionalWithDefault(arg) {
if (arg === undefined) {
return false;
}
return overload.string(arg);
};
fn.optional = true;
fn.defaultValue = def;
return fn;
};
// --- number
overload.number = function number(arg) {
return typeof(arg) === 'number';
};
overload.numberOptional = function numberOptional(arg) {
if (!arg) {
return true;
}
return overload.number(arg);
};
overload.numberOptional.optional = true;
overload.numberOptionalWithDefault = function (def) {
var fn = function numberOptionalWithDefault(arg) {
if (arg === undefined) {
return false;
}
return overload.number(arg);
};
fn.optional = true;
fn.defaultValue = def;
return fn;
};
// --- array
overload.array = function array(arg) {
return arg instanceof Array;
};
overload.arrayOptional = function arrayOptional(arg) {
if (!arg) {
return true;
}
return overload.array(arg);
};
overload.arrayOptional.optional = true;
overload.arrayOptionalWithDefault = function (def) {
var fn = function arrayOptionalWithDefault(arg) {
if (arg === undefined) {
return false;
}
return overload.array(arg);
};
fn.optional = true;
fn.defaultValue = def;
return fn;
};
// --- object
overload.object = function object(arg) {
return typeof(arg) === 'object';
};
overload.objectOptional = function objectOptional(arg) {
if (!arg) {
return true;
}
return overload.object(arg);
};
overload.objectOptional.optional = true;
overload.objectOptionalWithDefault = function (def) {
var fn = function objectOptionalWithDefault(arg) {
if (arg === undefined) {
return false;
}
return overload.object(arg);
};
fn.optional = true;
fn.defaultValue = def;
return fn;
};
+59
View File
@@ -0,0 +1,59 @@
{
"_from": "over@>= 0.0.5 < 1",
"_id": "over@0.0.5",
"_inBundle": false,
"_integrity": "sha1-8phS5w/X4l82DgE6jsRMgq7bVwg=",
"_location": "/over",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "over@>= 0.0.5 < 1",
"name": "over",
"escapedName": "over",
"rawSpec": ">= 0.0.5 < 1",
"saveSpec": null,
"fetchSpec": ">= 0.0.5 < 1"
},
"_requiredBy": [
"/pullstream"
],
"_resolved": "https://registry.npmjs.org/over/-/over-0.0.5.tgz",
"_shasum": "f29852e70fd7e25f360e013a8ec44c82aedb5708",
"_spec": "over@>= 0.0.5 < 1",
"_where": "D:\\Atom\\Git\\Laravel_CSSd\\node_modules\\pullstream",
"author": {
"name": "Joe Ferner",
"email": "joe@fernsroth.com"
},
"bugs": {
"url": "https://github.com/nearinfinity/node-over/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "JavaScript function overloading framework.",
"devDependencies": {
"nodeunit": "~0.7.4"
},
"engines": {
"node": "*"
},
"homepage": "https://github.com/nearinfinity/node-over#readme",
"keywords": [
"function",
"overload"
],
"license": "MIT",
"main": "overload.js",
"name": "over",
"optionalDependencies": {},
"repository": {
"type": "git",
"url": "git+https://github.com/nearinfinity/node-over.git"
},
"scripts": {
"test": "nodeunit test"
},
"version": "0.0.5"
}
+89
View File
@@ -0,0 +1,89 @@
'use strict';
var overload = require('../');
module.exports = {
'required, good': function (test) {
var items = [
{ fn: overload.func, param: function () {} },
{ fn: overload.funcOptional, param: function () {} },
{ fn: overload.funcOptionalWithDefault(function () {}), param: function () {} },
{ fn: overload.callbackOptional, param: function () {} },
{ fn: overload.string, param: 'test' },
{ fn: overload.stringOptional, param: 'test' },
{ fn: overload.stringOptionalWithDefault('test'), param: 'test' },
{ fn: overload.number, param: 5 },
{ fn: overload.numberOptional, param: 5 },
{ fn: overload.numberOptionalWithDefault(5), param: 5 },
{ fn: overload.array, param: [1] },
{ fn: overload.arrayOptional, param: [1] },
{ fn: overload.arrayOptionalWithDefault([1]), param: [1] },
{ fn: overload.object, param: {} },
{ fn: overload.objectOptional, param: {} },
{ fn: overload.objectOptionalWithDefault({}), param: {} }
];
items.forEach(function (item) {
test.equals(item.fn(item.param), true);
});
test.done();
},
'required, bad': function (test) {
var items = [
{ fn: overload.func, param: 5 },
{ fn: overload.funcOptional, param: 5 },
{ fn: overload.funcOptionalWithDefault(function () {}), param: 5 },
{ fn: overload.callbackOptional, param: 5 },
{ fn: overload.string, param: 5 },
{ fn: overload.stringOptional, param: 5 },
{ fn: overload.stringOptionalWithDefault('test'), param: 5 },
{ fn: overload.number, param: 'test' },
{ fn: overload.numberOptional, param: 'test' },
{ fn: overload.numberOptionalWithDefault(5), param: 'test' },
{ fn: overload.array, param: 5 },
{ fn: overload.arrayOptional, param: 5 },
{ fn: overload.arrayOptionalWithDefault([]), param: 5 },
{ fn: overload.object, param: 5 },
{ fn: overload.objectOptional, param: 5 },
{ fn: overload.objectOptionalWithDefault({}), param: 5 }
];
items.forEach(function (item) {
test.equals(item.fn(item.param), false);
});
test.done();
},
'optional, null': function (test) {
var items = [
{ fn: overload.funcOptional },
{ fn: overload.stringOptional },
{ fn: overload.numberOptional },
{ fn: overload.arrayOptional },
{ fn: overload.objectOptional }
];
items.forEach(function (item) {
test.equals(item.fn(null), true, 'invalid results for function: ' + item.fn.name);
});
test.done();
},
'optional, callback': function (test) {
test.equals(typeof(overload.callbackOptional(null).defaultValue), 'function');
test.done();
},
'with defaults': function (test) {
var fn = function () {};
var items = [
{ fn: overload.funcOptionalWithDefault(fn), expected: fn },
{ fn: overload.stringOptionalWithDefault('test'), expected: 'test' },
{ fn: overload.numberOptionalWithDefault(5), expected: 5 },
{ fn: overload.arrayOptionalWithDefault([]), expected: [] },
{ fn: overload.objectOptionalWithDefault({}), expected: {} }
];
items.forEach(function (item) {
test.deepEqual(item.fn.defaultValue, item.expected);
});
test.done();
}
};
+295
View File
@@ -0,0 +1,295 @@
'use strict';
var overload = require('../');
module.exports = {
'no parameters, valid args': function (test) {
var called = 0;
var fn = overload([
[function () { called++; return 42; }]
]);
var ret = fn();
test.equals(called, 1);
test.equals(ret, 42);
test.done();
},
'no parameters, invalid args': function (test) {
var called = 0;
var fn = overload([
[function () { called++; }]
]);
try {
fn(1);
test.fail('should throw exception');
} catch (ex) {
}
test.done();
},
'default callback': function (test) {
var called = 0;
var args = null;
var fn = overload([
[overload.func, function (fn) { test.fail('should not be called.'); }],
function () {
called++;
args = Array.prototype.slice.call(arguments);
}
]);
fn('test');
test.equals(called, 1, 'overload not called');
test.deepEqual(args, ['test']);
test.done();
},
'one parameter, valid args': function (test) {
var called = 0;
var argFnCalled = 0;
var fn = overload([
[overload.func, function (fn) {
called++;
fn();
}]
]);
fn(function () { argFnCalled++; });
test.equals(called, 1, 'overload not called');
test.equals(argFnCalled, 1, 'arg function not called');
test.done();
},
'one parameter, invalid args': function (test) {
var called = 0;
var fn = overload([
[overload.func, function () { called++; }]
]);
try {
fn(1);
test.fail('should throw exception');
} catch (ex) {
}
test.done();
},
'optional parameter, valid args': function (test) {
var called = 0;
var argFnCalled = 0;
var fn = overload([
[overload.funcOptional, function (fn) {
called++;
fn();
}]
]);
fn(function () { argFnCalled++; });
test.equals(called, 1, 'overload not called');
test.equals(argFnCalled, 1, 'arg function not called');
test.done();
},
'optional parameter, no args': function (test) {
var called = 0;
var fn = overload([
[overload.funcOptional, function (fn) {
called++;
if (fn) {
test.fail('no function should be passed');
}
}]
]);
fn();
test.equals(called, 1, 'overload not called');
test.done();
},
'optional parameter, invalid args': function (test) {
var called = 0;
var fn = overload([
[overload.funcOptional, function () { called++; }]
]);
try {
fn(1);
test.fail('should throw exception');
} catch (ex) {
}
test.done();
},
'multiple overloads, valid args': function (test) {
var called = 0;
var val = null;
var fn = overload([
[overload.number, function (n) { test.fail('this function should not be called'); }],
[overload.string, function (s) {
val = s;
called++;
}]
]);
fn('test');
test.equals(called, 1, 'overload not called');
test.equals(val, 'test', 'overload called with wrong value');
test.done();
},
'multiple overloads, invalid args': function (test) {
var called = 0;
var fn = overload([
[overload.number, function (n) { test.fail('this function should not be called'); }],
[overload.string, function (s) { called++; }]
]);
try {
fn(function () {});
test.fail('should throw exception');
} catch (ex) {
}
test.done();
},
'optional parameter, with default value': function (test) {
var called = 0;
var val = null;
var fn = overload([
[overload.numberOptionalWithDefault(5), overload.func, function (n) {
called++;
val = n;
}]
]);
fn(function() {});
test.equals(called, 1, 'overload not called');
test.equals(val, 5);
test.done();
},
'number optional parameter, called with non-zero number': function (test) {
var called = 0;
var val = null;
var fn = overload([
[overload.numberOptionalWithDefault(5), overload.func, function (n) {
called++;
val = n;
}]
]);
fn(3, function() {});
test.equals(called, 1, 'overload not called');
test.equals(val, 3);
test.done();
},
'number optional parameter, called with zero': function (test) {
var called = 0;
var val = null;
var fn = overload([
[overload.numberOptionalWithDefault(5), overload.func, function (n) {
called++;
val = n;
}]
]);
fn(0, function() {});
test.equals(called, 1, 'overload not called');
test.equals(val, 0);
test.done();
},
'string optional parameter, called with empty string': function (test) {
var called = 0;
var val = null;
var fn = overload([
[overload.stringOptionalWithDefault("hello world"), overload.func, function (n) {
called++;
val = n;
}]
]);
fn("", function() {});
test.equals(called, 1, 'overload not called');
test.equals(val, "");
test.done();
},
'callback, with default value': function (test) {
var called = 0;
var fn = overload([
[overload.callbackOptional, function (callback) {
callback();
}]
]);
fn(function () { called++; });
test.equals(called, 1, 'overload not called');
test.done();
},
'multiple optionals': function (test) {
var called = 0;
var args;
var fn = overload([
[overload.string, overload.arrayOptionalWithDefault(null), overload.callbackOptional, function (str, arr, callback) {
args = arguments;
called++;
}]
]);
fn('test');
test.equals(called, 1, 'overload not called');
test.equals(args.length, 3);
test.equals(args[0], 'test');
test.ok(args[1] === null);
test.equals(typeof(args[2]), 'function');
test.done();
},
'multiple optionals, skipping middle one': function (test) {
var called = 0;
var args;
var fn = overload([
[overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
args = arguments;
called++;
}]
]);
var fnTest = function zzz() {};
fn('test', fnTest);
test.equals(called, 1, 'overload not called');
test.equals(args.length, 3);
test.equals(args[0], 'test');
test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
test.equals(args[2].name, 'zzz');
test.done();
},
'multiple optionals, null middle one': function (test) {
var called = 0;
var args;
var fn = overload([
[overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
args = arguments;
called++;
}]
]);
var fnTest = function zzz() {};
fn('test', null, fnTest);
test.equals(called, 1, 'overload not called');
test.equals(args.length, 3);
test.equals(args[0], 'test');
test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
test.equals(args[2].name, 'zzz');
test.done();
},
'complex': function (test) {
var called = 0;
var args = null;
var fn = overload([
[overload.string, function () { test.fail('this function should not be called'); }],
[overload.string, overload.number, overload.funcOptional, function () {
args = Array.prototype.slice.call(arguments, 0);
called++;
}]
]);
var argFn = function () {};
fn('test', 5, argFn);
test.equals(called, 1, 'overload not called');
test.deepEqual(args, ['test', 5, argFn], 'overload called with wrong args');
test.done();
}
};