bloody-curry
Advanced tools
Comparing version 0.1.1 to 0.2.0
51
index.js
@@ -1,30 +0,33 @@ | ||
;(function(root, name, fn){ | ||
var exports = fn(root) | ||
if(typeof define == "function" && define.amd) return define(exports) | ||
if(typeof module !== 'undefined' && module.exports) module.exports = exports | ||
root[name] = exports | ||
})(this, "curry", function(){ | ||
var nativePush = [].push | ||
var nativeConcat = [].concat | ||
, nativeSlice = [].slice | ||
, hasOwnProperty = {}.hasOwnProperty | ||
function createCurried(func, length, currentArgs){ | ||
function createCurried(fn, args, length, thisValue){ | ||
function curried(){ | ||
var currentArgs = nativeConcat.apply(args, arguments) | ||
if(length - currentArgs.length <= 0) { | ||
return fn.apply(thisValue === void 0 ? this : thisValue, currentArgs) | ||
} | ||
return createCurried(fn, currentArgs, length, thisValue) | ||
function curried() { | ||
var args = arguments | ||
var concatenatedArgs = [] | ||
nativePush.apply(concatenatedArgs, currentArgs) | ||
nativePush.apply(concatenatedArgs, args) | ||
if(concatenatedArgs.length >= length) { | ||
return func.apply(this, concatenatedArgs) | ||
} | ||
curried.prototype = fn.prototype | ||
return curried | ||
return createCurried( | ||
func, | ||
length, | ||
concatenatedArgs | ||
) | ||
} | ||
function curry(fn, length, thisValue){ | ||
length = typeof length == "number" ? length : fn.length | ||
return createCurried(fn, [], length, thisValue) | ||
return curried | ||
} | ||
function curry(func, length) { | ||
if(length == null) { | ||
length = func.length | ||
} | ||
return curry | ||
}) | ||
return createCurried(func, length, []) | ||
} | ||
module.exports = curry |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -10,0 +10,0 @@ "directories": { |
@@ -1,13 +0,20 @@ | ||
# curry.js | ||
# curry | ||
[![Build Status](https://travis-ci.org/bloodyowl/curry.png?branch=master)](https://travis-ci.org/bloodyowl/curry) | ||
[![browser support](https://ci.testling.com/bloodyowl/curry.png) | ||
](https://ci.testling.com/bloodyowl/curry) | ||
## install | ||
simple currying script | ||
```console | ||
$ npm install bloody-curry | ||
``` | ||
## require | ||
```javascript | ||
var curry = require("curry") | ||
``` | ||
simple currying script | ||
```javascript | ||
function add(a,b,c,d){ | ||
@@ -22,12 +29,9 @@ return a + b + c + d | ||
## API | ||
## api | ||
### `curry(function:fn[, number:length][, any:thisValue])` | ||
### curry(func[, number:length]) | ||
Returns a curried function that keeps returning a function until the sum of the passed arguments's length reaches `length` (if defined) or `fn.length`. An optional `thisValue` can be defined. | ||
Returns a curried function that keeps returning a function until the sum of | ||
the passed arguments's length reaches `length` (if defined) or `fn.length`. | ||
## Run tests | ||
``` | ||
$ npm test | ||
``` | ||
## [license](LICENSE.md) |
var tape = require("tape") | ||
, curry = require("../") | ||
var curry = require("..") | ||
tape("Curry", function(test){ | ||
var curried = curry(add) | ||
, curriedLength = curry(addNoLimit, 4) | ||
, curriedThisValue = curry(getThisValue, null, {foo:"bar"}) | ||
, curriedCtor = curry(Ctor) | ||
function add(a,b,c,d){ | ||
return a + b + c + d | ||
tape("curry", function(test){ | ||
function map(func, array) { | ||
return array.map(func) | ||
} | ||
function addNoLimit(){ | ||
var args = arguments | ||
, l = args.length | ||
, r = 0 | ||
while(--l > -1) { | ||
r += args[l] | ||
} | ||
return r | ||
function square(number) { | ||
return Math.pow(number, 2) | ||
} | ||
function getThisValue(a){ | ||
return this | ||
function squareRoot(number) { | ||
return Math.sqrt(number) | ||
} | ||
function Ctor(a,b){ | ||
this.a += a | ||
this.a += b | ||
var curriedMap = curry(map) | ||
var curriedSquareMap = curriedMap(square) | ||
var curriedSquareRootMap = curriedMap(squareRoot) | ||
test.deepEqual( | ||
curriedSquareMap([1, 2, 3, 4]), | ||
[1, 4, 9, 16] | ||
) | ||
test.deepEqual( | ||
curriedSquareRootMap([1, 4, 9, 16]), | ||
[1, 2, 3, 4] | ||
) | ||
test.end() | ||
}) | ||
tape("curry #2", function(test){ | ||
function add(a, b, c, d, e) { | ||
return a + b + c + d + e | ||
} | ||
Ctor.prototype.a = 1 | ||
test.equal(new curriedCtor(2,3).a, 6, "Keeps prototype") | ||
test.equal(curriedThisValue(1).foo, "bar", "Passes thisValue") | ||
test.equal(curried(1)(2)(3)(4), 10, "Curry one param") | ||
test.equal(curried(1, 2)(3, 4), 10, "Curry with multiple") | ||
test.equal(curried(1, 2, 3, 4), 10, "Curry with one call") | ||
test.equal(curried(1, 2)(3, 4, 5), 10, "Curry with one extra param") | ||
test.equal(curriedLength(1, 2)(3, 4), 10, "Curry with limit set manually") | ||
var curriedAdd = curry(add) | ||
test.equal( | ||
curriedAdd(1, 2, 3, 4, 5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1, 2, 3, 4)(5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1, 2, 3)(4)(5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1, 2)(3)(4)(5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1)(2)(3)(4)(5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1)(2)(3, 4)(5), | ||
15 | ||
) | ||
test.end() | ||
}) | ||
tape("custom length", function(test){ | ||
function add(a, b) { | ||
return Array.prototype.reduce.call(arguments, function(acc, item) { | ||
return acc + item | ||
}, 0) | ||
} | ||
var curriedAdd = curry(add, 5) | ||
test.equal( | ||
curriedAdd(1)(2)(3)(4)(5), | ||
15 | ||
) | ||
test.equal( | ||
curriedAdd(1, 2)(3, 4, 5), | ||
15 | ||
) | ||
test.end() | ||
}) |
98
37
4789