better-curry
Advanced tools
Comparing version 1.5.1 to 1.6.0
{ | ||
"name": "better-curry", | ||
"main": "index.js", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Paulo Cesar <email@pocesar.e4ward.com>" |
79
index.js
@@ -32,3 +32,3 @@ (function (root, factory){ | ||
noContext = context === null, | ||
themArgs = [], | ||
themArgs = [], outFn, | ||
instead = false; | ||
@@ -51,3 +51,3 @@ | ||
case 0: | ||
return function zeroArgs(){ | ||
outFn = function zeroArgs(){ | ||
return noContext ? | ||
@@ -57,4 +57,5 @@ fn() | ||
}; | ||
break; | ||
case 1: | ||
return function oneArg(arg1){ | ||
outFn = function oneArg(arg1){ | ||
return noContext ? | ||
@@ -68,4 +69,5 @@ fn( | ||
}; | ||
break; | ||
case 2: | ||
return function twoArgs(arg1, arg2){ | ||
outFn = function twoArgs(arg1, arg2){ | ||
return noContext ? | ||
@@ -81,4 +83,5 @@ fn( | ||
}; | ||
break; | ||
case 3: | ||
return function threeArgs(arg1, arg2, arg3){ | ||
outFn = function threeArgs(arg1, arg2, arg3){ | ||
return noContext ? | ||
@@ -96,4 +99,5 @@ fn( | ||
}; | ||
break; | ||
case 4: | ||
return function fourArgs(arg1, arg2, arg3, arg4){ | ||
outFn = function fourArgs(arg1, arg2, arg3, arg4){ | ||
return noContext ? | ||
@@ -113,4 +117,5 @@ fn( | ||
}; | ||
break; | ||
case 5: | ||
return function fiveArgs(arg1, arg2, arg3, arg4, arg5){ | ||
outFn = function fiveArgs(arg1, arg2, arg3, arg4, arg5){ | ||
return noContext ? | ||
@@ -132,4 +137,5 @@ fn( | ||
}; | ||
break; | ||
case 6: | ||
return function sixArgs(arg1, arg2, arg3, arg4, arg5, arg6){ | ||
outFn = function sixArgs(arg1, arg2, arg3, arg4, arg5, arg6){ | ||
return noContext ? | ||
@@ -153,4 +159,5 @@ fn( | ||
}; | ||
break; | ||
case 7: | ||
return function sevenArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7){ | ||
outFn = function sevenArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7){ | ||
return noContext ? | ||
@@ -176,4 +183,5 @@ fn( | ||
}; | ||
break; | ||
case 8: | ||
return function eightArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8){ | ||
outFn = function eightArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8){ | ||
return noContext ? | ||
@@ -201,4 +209,5 @@ fn( | ||
}; | ||
break; | ||
case 9: | ||
return function nineArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9){ | ||
outFn = function nineArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9){ | ||
return noContext ? | ||
@@ -228,4 +237,5 @@ fn( | ||
}; | ||
break; | ||
case 10: | ||
return function tenArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10){ | ||
outFn = function tenArgs(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10){ | ||
return noContext ? | ||
@@ -257,14 +267,19 @@ fn( | ||
}; | ||
break; | ||
default: | ||
// really? 11 args? | ||
if (themArgs.length) { | ||
outFn = function variadic(){ | ||
return fn.apply(context, slice(themArgs, arguments)); | ||
}; | ||
} else { | ||
outFn = function variadic(){ | ||
return fn.apply(context, slice(arguments)); | ||
}; | ||
} | ||
} | ||
// really? 11 args? | ||
if (themArgs.length) { | ||
return function variadic(){ | ||
return fn.apply(context, slice(themArgs, arguments)); | ||
}; | ||
} else { | ||
return function variadic(){ | ||
return fn.apply(context, slice(arguments)); | ||
}; | ||
} | ||
outFn.__length = fn.length; | ||
return outFn; | ||
} | ||
@@ -285,3 +300,3 @@ | ||
function Wrap(fn, context, len, checkArguments){ | ||
len = len || fn.length; | ||
var _len = len || fn.length; | ||
@@ -292,3 +307,5 @@ checkArguments = checkArguments || false; | ||
if (len !== fn.length && argumentsRegexp.test(fn)) { | ||
len = -1; | ||
_len = -1; | ||
} else if (fn.__length) { | ||
_len = fn.__length; | ||
} | ||
@@ -299,3 +316,3 @@ } | ||
return template(fn, len, context); | ||
return template(fn, _len, context); | ||
} | ||
@@ -315,3 +332,3 @@ | ||
function Predefine(fn, args, context, len, checkArguments){ | ||
len = len || fn.length; | ||
var _len = len || fn.length; | ||
@@ -322,3 +339,5 @@ context = context || null; | ||
if (len !== fn.length && argumentsRegexp.test(fn)) { | ||
len = -1; | ||
_len = -1; | ||
} else if (fn.__length) { | ||
_len = fn.__length; | ||
} | ||
@@ -329,3 +348,7 @@ } | ||
return template(fn, len, context, args); | ||
if (args.length === 0) { | ||
return Wrap(fn, context, len, checkArguments); | ||
} | ||
return template(fn, _len, context, args); | ||
} | ||
@@ -332,0 +355,0 @@ |
{ | ||
"name": "better-curry", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "Forget Function.bind and func.apply(context, arguments), performance matters! For a better curry!", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=0.8" | ||
}, | ||
@@ -46,3 +46,3 @@ "repository": { | ||
"expect.js": "*", | ||
"benchmark": "^1.0.0", | ||
"benchmark": "1.x.x", | ||
"delegates": "*" | ||
@@ -49,0 +49,0 @@ }, |
@@ -67,2 +67,15 @@ [![Build Status](https://travis-ci.org/pocesar/js-bettercurry.png?branch=master)](https://travis-ci.org/pocesar/js-bettercurry?branch=master) | ||
Generated function will have a `__length` property, that equals to the original fn length. | ||
You may hint the function by appending the expected length to your function (or if it's already wrapped): | ||
```js | ||
var myfunc = function(arg1, arg2, arg3, arg4){ | ||
}; | ||
myfunc.__length = 3; | ||
BetterCurry.wrap(myfunc, null, true); // creates a threeArgs wrapped function | ||
``` | ||
#### `BetterCurry.predefine(fn, args [, context[, len [, checkArguments]]])` | ||
@@ -99,2 +112,4 @@ | ||
Generated function will have a `__length` property, that equals to the original fn length | ||
#### `BetterCurry.delegate(proto, target)` | ||
@@ -161,6 +176,6 @@ | ||
=============================== Coverage summary =============================== | ||
Statements : 100% ( 123/123 ), 3 ignored | ||
Branches : 100% ( 327/327 ), 5 ignored | ||
Statements : 100% ( 138/138 ), 3 ignored | ||
Branches : 100% ( 330/330 ), 5 ignored | ||
Functions : 100% ( 30/30 ) | ||
Lines : 100% ( 123/123 ) | ||
Lines : 100% ( 138/138 ) | ||
================================================================================ | ||
@@ -167,0 +182,0 @@ ``` |
@@ -24,3 +24,3 @@ /*jshint evil:true */ | ||
for (var x = 0; x <= i; x++) { | ||
out.push(Math.round((Math.random() * 20) + 1)); | ||
out.push(x); | ||
} | ||
@@ -60,2 +60,10 @@ return out; | ||
it('should contain the original lenght of the base function on the wrapped function', function(){ | ||
for (var i = 1; i < fs.length; i++) { | ||
var args = craft(i - 1), func = BetterCurry.predefine(fs[i - 1], args); | ||
expect(func.__length).to.be(i); | ||
} | ||
}); | ||
it('should work on zero length functions', function (){ | ||
@@ -145,2 +153,3 @@ function base(){ | ||
base = function(a1, a2) { | ||
@@ -153,2 +162,8 @@ return a1 + a2; | ||
expect(curried.toString()).to.match(/oneArg/); | ||
expect(curried.__length).to.be(2); | ||
curried = BetterCurry.predefine(curried, [], null, 1, true); | ||
expect(curried.toString()).to.match(/twoArgs/); | ||
expect(curried.__length).to.be(1); | ||
}); | ||
@@ -234,2 +249,8 @@ | ||
expect(curried.toString()).to.match(/oneArg/); | ||
expect(curried.__length).to.be(2); | ||
curried = BetterCurry.wrap(curried, null, 1, true); | ||
expect(curried.toString()).to.match(/twoArgs/); | ||
expect(curried.__length).to.be(1); | ||
}); | ||
@@ -236,0 +257,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
213
179910
14
6057