Skip to content

Commit

Permalink
feat: added partial application support
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-kovalev committed Oct 21, 2019
1 parent fbaf5b1 commit 6807c8a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const set = require('./set');
const remove = require('./remove');

module.exports = {
...mapValues({ update, set, remove }, preparePath)
...mapValues({ update, set, remove }, enhance)
};

function mapValues(object, map) {
Expand All @@ -15,10 +15,30 @@ function mapValues(object, map) {
);
}

function enhance(fn) {
return specialCurry(
(...args) => preparePath(fn)(...args),
fn.length
);
}

function preparePath(fn) {
return (object, path, ...rest) => fn(
object,
Array.isArray(path) ? path : path.split('.'),
...rest
);
}

function specialCurry(fn, arity = fn.length) {
return (...args) => {
if (args.length >= arity) {
return fn(...args);
}

return specialCurry(
(object, ...rest) => fn(object, ...args, ...rest),
arity - args.length
);
};
}

0 comments on commit 6807c8a

Please sign in to comment.