chickencurry
Advanced tools
Comparing version 1.0.1 to 1.0.2
62
index.js
@@ -1,47 +0,8 @@ | ||
var slice = Array.prototype.slice, | ||
var merge = require('./lib/merge'), | ||
countDefinedItems = require('./lib/countDefinedItems'), | ||
slice = Array.prototype.slice, | ||
__; | ||
function forEach(arr, fn) { | ||
for (var i = 0; i < arr.length; i++) { | ||
fn(i, arr[i]); | ||
} | ||
} | ||
function map(arr, fn) { | ||
var newArr = []; | ||
forEach(arr, function(key, item) { | ||
newArr.push(fn(key, item)); | ||
}); | ||
return newArr; | ||
} | ||
function mergeArguments(args, curryArgs) { | ||
var mergedArgs = []; | ||
mergedArgs = map(curryArgs, function(key, item) { | ||
if (item === __) { | ||
return args.shift(); | ||
} else { | ||
return item; | ||
} | ||
}); | ||
return mergedArgs.concat(args); | ||
} | ||
function countArgs(args) { | ||
var count = 0; | ||
forEach(args, function(key, item) { | ||
if (item !== __) { | ||
count = count + 1; | ||
} | ||
}); | ||
return count; | ||
} | ||
function recurry(fn, curryArgs) { | ||
function curry(fn, curryArgs) { | ||
return function() { | ||
@@ -51,7 +12,12 @@ var args = slice.call(arguments), | ||
if (fn.length <= countArgs(curryArgs.concat(args))) { | ||
mergedArgs = mergeArguments(args, curryArgs); | ||
if (fn.length <= countDefinedItems(curryArgs.concat(args))) { | ||
mergedArgs = merge(args, curryArgs); | ||
return fn.apply(null, mergedArgs); | ||
} else { | ||
return recurry(fn, curryArgs.concat(args)); | ||
mergedArgs = curryArgs.concat(args); | ||
if (fn.length >= mergedArgs.length) { | ||
return curry(fn, mergedArgs); | ||
} else { | ||
return curry(fn, merge(args, curryArgs)); | ||
} | ||
} | ||
@@ -61,8 +27,8 @@ }; | ||
module.exports = function curry(fn) { | ||
module.exports = function(fn) { | ||
var args = slice.call(arguments, 1); | ||
return recurry(fn, args); | ||
return curry(fn, args); | ||
}; | ||
module.exports.__ = __; |
{ | ||
"name": "chickencurry", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Add some chicken curry to your functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
37
test.js
@@ -71,2 +71,3 @@ var curry = require('./index.js'), | ||
it('should wrap the function if no argument to curry is passed', function() { | ||
var __ = curry.__; | ||
var addCurry = curry(add); | ||
@@ -78,16 +79,42 @@ var joinCurry = curry(join); | ||
expect(addCurry(1)).to.be.a('function'); | ||
expect(addCurry(1, 2)).to.equal(3); | ||
expect(addCurry(1)(2)).to.equal(3); | ||
expect(addCurry(__, 2)(1)).to.equal(3); | ||
expect(addCurry(__)(2)(1)).to.equal(3); | ||
expect(addCurry(2, __)(1)).to.equal(3); | ||
expect(addCurry(2)(__)(1)).to.equal(3); | ||
expect(joinCurry).to.be.a('function'); | ||
expect(joinCurry('_')).to.be.a('function'); | ||
expect(joinCurry('_', '_')).to.be.a('function'); | ||
expect(joinCurry('_','_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_')('_')).to.be.a('function'); | ||
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('_')('_', 'fish')).to.equal('_fish_'); | ||
expect(joinCurry(__)).to.be.a('function'); | ||
expect(joinCurry(__, '_')).to.be.a('function'); | ||
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(curry.__,'_')('_', 'chicken')).to.equal('_chicken_'); | ||
expect(joinCurry('_', curry.__)('-', 'chicken')).to.equal('_chicken-'); | ||
expect(joinCurry(curry.__)('.')('curry')('.')).to.equal('.curry.'); | ||
expect(joinCurry(curry.__)('.')('curry')('$')).to.equal('$curry.'); | ||
expect(joinCurry('_', curry.__, 'chicken')('-')).to.equal('_chicken-'); | ||
expect(joinCurry(curry.__, '_', 'chicken')('-')).to.equal('-chicken_'); | ||
expect(joinDot('.')('chicken')).to.equal('.chicken.'); | ||
expect(joinDot('.', 'chicken')).to.equal('.chicken.'); | ||
expect(joinDot('$', 'chicken')).to.equal('.chicken$'); | ||
expect(joinDot('$')('chicken')).to.equal('.chicken$'); | ||
expect(joinDot(__, 'chicken')('$')).to.equal('.chicken$'); | ||
expect(joinDot(__)('chicken')('$')).to.equal('.chicken$'); | ||
expect(joinDot('$', __)('chicken')).to.equal('.chicken$'); | ||
expect(joinDot('$')(__)('chicken')).to.equal('.chicken$'); | ||
expect(joinDot(__, __)('$', 'chicken')).to.equal('.chicken$'); | ||
expect(joinDot(__, __)('$')('chicken')).to.equal('.chicken$'); | ||
expect(joinDot(__)(__)('$', 'chicken')).to.equal('.chicken$'); | ||
expect(joinDot(__)(__)('$')('chicken')).to.equal('.chicken$'); | ||
}); | ||
@@ -94,0 +121,0 @@ |
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
9710
12
177