chickencurry
Advanced tools
Comparing version 1.0.2 to 1.1.0
20
index.js
@@ -7,16 +7,16 @@ var merge = require('./lib/merge'), | ||
function curry(fn, curryArgs) { | ||
function curry(fn, length, curryArgs) { | ||
return function() { | ||
var args = slice.call(arguments), | ||
concatArgs = curryArgs.concat(args), | ||
mergedArgs = []; | ||
if (fn.length <= countDefinedItems(curryArgs.concat(args))) { | ||
if (length <= countDefinedItems(concatArgs)) { | ||
mergedArgs = merge(args, curryArgs); | ||
return fn.apply(null, mergedArgs); | ||
} else { | ||
mergedArgs = curryArgs.concat(args); | ||
if (fn.length >= mergedArgs.length) { | ||
return curry(fn, mergedArgs); | ||
if (length >= concatArgs.length) { | ||
return curry(fn, length, concatArgs); | ||
} else { | ||
return curry(fn, merge(args, curryArgs)); | ||
return curry(fn, length, merge(args, curryArgs)); | ||
} | ||
@@ -30,5 +30,11 @@ } | ||
return curry(fn, args); | ||
return curry(fn, fn.length, args); | ||
}; | ||
module.exports.n = function(fn, length) { | ||
var args = slice.call(arguments, 2); | ||
return curry(fn, length + 1, args); | ||
}; | ||
module.exports.__ = __; |
{ | ||
"name": "chickencurry", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Add some chicken curry to your functions", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha --reporter nyan --compilers js:mocha-traceur" | ||
}, | ||
@@ -27,2 +27,3 @@ "repository": { | ||
"mocha": "^2.1.0", | ||
"mocha-traceur": "^2.1.0", | ||
"sinon": "^1.12.2" | ||
@@ -29,0 +30,0 @@ }, |
@@ -44,2 +44,16 @@ Chickencurry | ||
add2(3); // => 5 | ||
function sum3(a, b, c) { | ||
return a + b + c; | ||
} | ||
curry(sum3)(1, 2)(3) // => 6 | ||
curry(sum3)(1)(2)(3) // => 6 | ||
function join() { | ||
return Array.prototype.slice.call(arguments).join(); | ||
} | ||
// Curry n arguments | ||
curry.n(join, 2)(1, 2)(3) // => '1,2,3' | ||
``` | ||
@@ -46,0 +60,0 @@ |
81
test.js
var curry = require('./index.js'), | ||
sinon = require('sinon'), | ||
expect = require('expect.js'); | ||
expect = require('expect.js'), | ||
obj; | ||
describe('chickencurry', function() { | ||
var obj = {}, add, join; | ||
beforeEach(function() { | ||
add = function(a, b) { | ||
return a + b; | ||
}; | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
join = function(a, b, sep) { | ||
return a + sep + b; | ||
}; | ||
obj.greeting = 'Hello'; | ||
obj.greet = function(name) { | ||
return this.greeting + ' ' + name; | ||
}; | ||
}); | ||
function join(a, b, sep) { | ||
return a + sep + b; | ||
} | ||
it('should return a function', function() { | ||
function joinArgs() { | ||
return Array.prototype.slice.call(arguments).join(); | ||
} | ||
obj = { | ||
greeting : 'Hello', | ||
greet : function(name) { | ||
return this.greeting + ' ' + name; | ||
} | ||
}; | ||
describe('chickencurry', () => { | ||
it('should return a function', () => { | ||
expect(curry(add)).to.be.a('function'); | ||
}); | ||
it('should create a curryied function with the given arguments', function() { | ||
it('should create a curryied function with the given arguments', () => { | ||
var add1 = curry(add, 1); | ||
@@ -40,7 +46,7 @@ var add12 = curry(add, 1, 2); | ||
it('should have a placeholder variable', function() { | ||
it('should have a placeholder variable', () => { | ||
expect(curry.__).to.be(undefined); | ||
}); | ||
it('should create a curryied function using a placeholder', function() { | ||
it('should create a curryied function using a placeholder', () => { | ||
var __ = curry.__; | ||
@@ -65,3 +71,3 @@ var join_ = curry(join, __, __, '_'); | ||
it('should keep the scope of a function', function() { | ||
it('should keep the scope of a function', () => { | ||
var greetChicken = curry(obj.greet.bind(obj), 'Chicken'); | ||
@@ -72,3 +78,3 @@ | ||
it('should wrap the function if no argument to curry is passed', function() { | ||
it('should wrap the function if no argument to curry is passed', () => { | ||
var __ = curry.__; | ||
@@ -91,5 +97,5 @@ var addCurry = curry(add); | ||
expect(joinCurry('_', '_')).to.be.a('function'); | ||
expect(joinCurry('_','_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_', '_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_')('_')).to.be.a('function'); | ||
expect(joinCurry('_','_')('chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_', '_')('chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_')('_')('chicken')).to.equal('_chicken_'); | ||
@@ -100,9 +106,9 @@ expect(joinCurry('_')('_', 'fish')).to.equal('_fish_'); | ||
expect(joinCurry(__, '_')).to.be.a('function'); | ||
expect(joinCurry(__,'_', 'chicken')).to.be.a('function'); | ||
expect(joinCurry(__,'_', 'chicken')('_')).to.equal('_chicken_'); | ||
expect(joinCurry(__, '_', 'chicken')).to.be.a('function'); | ||
expect(joinCurry(__, '_', 'chicken')('_')).to.equal('_chicken_'); | ||
expect(joinCurry(__)('_')).to.be.a('function'); | ||
expect(joinCurry(__,'_')('chicken')).to.be.a('function'); | ||
expect(joinCurry(__,'_')('chicken')('$')).to.equal('$chicken_'); | ||
expect(joinCurry(__, '_')('chicken')).to.be.a('function'); | ||
expect(joinCurry(__, '_')('chicken')('$')).to.equal('$chicken_'); | ||
expect(joinCurry(curry.__,'_')('_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry(curry.__, '_')('_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_', curry.__)('-', 'chicken')).to.equal('_chicken-'); | ||
@@ -125,3 +131,3 @@ expect(joinCurry(curry.__)('.')('curry')('$')).to.equal('$curry.'); | ||
it('should be possible to curry arguments of any type', function() { | ||
it('should be possible to curry arguments of any type', () => { | ||
var ajax = function(config, callback) { | ||
@@ -132,5 +138,9 @@ callback('response for: ' + config.url); | ||
var ajaxSpy = curry(ajax, curry.__, spy); | ||
var ajaxGoogle = curry(ajax, { url: 'google.ch' }); | ||
var ajaxGoogle = curry(ajax, { | ||
url: 'google.ch' | ||
}); | ||
ajaxSpy({ url: 'stoeffel.ch' }); | ||
ajaxSpy({ | ||
url: 'stoeffel.ch' | ||
}); | ||
expect(spy.calledWith('response for: stoeffel.ch')).to.be.ok(); | ||
@@ -142,2 +152,11 @@ | ||
}); | ||
it('should curry n arguments', () => { | ||
expect(curry.n(joinArgs, 0)(4)).to.equal('4'); | ||
expect(curry.n(joinArgs, 1)(1, 2)).to.equal('1,2'); | ||
expect(curry.n(joinArgs, 3)(1, 2, 3)(4)).to.equal('1,2,3,4'); | ||
expect(curry.n(joinArgs, 3)(1)(2)(3)(4)).to.equal('1,2,3,4'); | ||
expect(curry.n(joinArgs, 3, 1, 2, 3)(4)).to.equal('1,2,3,4'); | ||
expect(curry.n(joinArgs, 3, void 0, 2, 3, 4)(0)).to.equal('0,2,3,4'); | ||
}); | ||
}); |
10580
196
98
4