curry-named
Advanced tools
Comparing version 1.0.0 to 1.1.0
20
index.js
'use strict' | ||
function curryNamed (keys, fn) { | ||
return function helper (o) { | ||
return function (o2) { | ||
const obj = Object.assign({}, o, o2) | ||
return includes(keys, Object.keys(obj)) ? fn(obj) : helper(obj) | ||
return function helper (cache) { | ||
return function (...objects) { | ||
const incoming = Object.assign({}, ...objects) | ||
const argsOverridden = includesSome(Object.keys(cache), Object.keys(incoming)) | ||
const error = `The following arguments were overidden: ${argsOverridden}` | ||
if (argsOverridden.length > 0) throw Error(error) | ||
const obj = Object.assign({}, cache, ...objects) | ||
return includesEvery(keys, Object.keys(obj)) ? fn(obj) : helper(obj) | ||
} | ||
} | ||
}({}) | ||
} | ||
const includes = (mandatory, received) => | ||
const includesEvery = (mandatory, received) => | ||
mandatory.every(key => | ||
received.includes(key)) | ||
const includesSome = (mandatory, received) => | ||
mandatory.filter(key => | ||
received.includes(key)) | ||
module.exports = curryNamed |
{ | ||
"name": "curry-named", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Curry for named arguments", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,3 @@ # curry-named | ||
## usage | ||
## Usage | ||
@@ -19,2 +19,11 @@ ```js | ||
### Partial application works too | ||
```js | ||
cont { partial } = require('lodash/fp') | ||
const partiallyApplied = partial(foo)([{ a: 1 }]) | ||
const actual = partiallyApplied({ b: 2 })({ c: 4 }) | ||
const expected = 7 | ||
``` | ||
## Inspiration | ||
@@ -21,0 +30,0 @@ |
43
test.js
const test = require('tape') | ||
const { partial } = require('lodash/fp') | ||
const curry = require('./') | ||
test('basic test', function (t) { | ||
const foo = curry( | ||
['a', 'b', 'c'], | ||
({a, b, c}) => a + b + c) | ||
const foo = curry( | ||
['a', 'b', 'c'], | ||
({a, b, c}) => a + b + c) | ||
test('original function', t => { | ||
const actual = foo({ a: 1, b: 2, c: 4}) | ||
const expected = 7 | ||
t.equal(actual, expected) | ||
t.end() | ||
}) | ||
// const actual = curry(foo, keys)({ a: 1 })({ a: 1, b: 2, c: 3}) TODO fail | ||
const actual = foo({ a: 1 })({ b: 2, c: 3}) | ||
const expected = 6 | ||
test('currying', t => { | ||
const actual = foo({ a: 1 })({ b: 2, c: 4}) | ||
const expected = 7 | ||
t.equal(actual, expected) | ||
t.end() | ||
}) | ||
test('partial application (simple)', t => { | ||
const actual = foo({ a: 1 })({ b: 2 }, { c: 4 }) | ||
const expected = 7 | ||
t.equal(actual, expected) | ||
t.end() | ||
}) | ||
test('partial application (lodash)', t => { | ||
const partiallyApplied = partial(foo)([{ a: 1 }]) | ||
const actual = partiallyApplied({ b: 2 })({ c: 4 }) | ||
const expected = 7 | ||
t.equal(actual, expected) | ||
t.end() | ||
}) | ||
test('argument overriding', t => { | ||
try { | ||
foo({ a: 1 })({ a: 1, b: 2, c: 4}) | ||
} catch (e) { | ||
return t.end() | ||
} | ||
}) | ||
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
51162
58
31