diff --git a/lib/if.js b/lib/if.js new file mode 100644 index 0000000..1e9ed2d --- /dev/null +++ b/lib/if.js @@ -0,0 +1,6 @@ +import ifElse from './ifElse'; +import curry from './util/curry'; + +export default curry((predicate, trueUpdates, object) => + ifElse(predicate, trueUpdates, {}, object) +); diff --git a/lib/index.js b/lib/index.js index 77375ea..72fb5b4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,6 @@ import freeze from './freeze'; import is from './is'; +import _if from './if'; import ifElse from './ifElse'; import map from './map'; import omit from './omit'; @@ -8,11 +9,9 @@ import update from './update'; import updateIn from './updateIn'; import withDefault from './withDefault'; -import { placeholder as _ } from 'lodash/function/curry'; - const u = update; -u.if = ifElse(_, _, {}); +u.if = _if; u.ifElse = ifElse; u.is = is; u.freeze = freeze; diff --git a/lib/is.js b/lib/is.js index c5e8db0..0e3eb6d 100644 --- a/lib/is.js +++ b/lib/is.js @@ -1,5 +1,5 @@ import splitPath from './util/splitPath'; -import curry from 'lodash/function/curry'; +import curry from './util/curry'; function is(path, predicate, object) { const parts = splitPath(path); diff --git a/lib/update.js b/lib/update.js index 5640197..707b4ec 100644 --- a/lib/update.js +++ b/lib/update.js @@ -75,4 +75,4 @@ function update(updates, object, ...args) { return { ...object, ...resolvedUpdates }; } -export default wrap(update); +export default wrap(update, 2); diff --git a/lib/updateIn.js b/lib/updateIn.js index 09cc6cc..72a9d71 100644 --- a/lib/updateIn.js +++ b/lib/updateIn.js @@ -1,4 +1,4 @@ -import curry from 'lodash/function/curry'; +import curry from './util/curry'; import update from './update'; import splitPath from './util/splitPath'; diff --git a/lib/util/curry.js b/lib/util/curry.js index 1344bdb..1727308 100644 --- a/lib/util/curry.js +++ b/lib/util/curry.js @@ -28,3 +28,25 @@ export function curry3(fn) { return curried; }; } + +export function curry4(fn) { + return function curried(a, b, c, d, e, f) { + const n = arguments.length; + + if (n >= 4) return fn(a, b, c, d, e, f); + if (n === 3) return curry1((d, e, f) => fn(a, b, c, d, e, f)); + if (n === 2) return curry2((c, d, e, f) => fn(a, b, c, d, e, f)); + if (n === 1) return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f)); + return curried; + }; +} + +export default function curry(fn, length = fn.length) { + return [ + fn, + curry1, + curry2, + curry3, + curry4, + ][length](fn); +} diff --git a/lib/withDefault.js b/lib/withDefault.js index 7ef9a48..3dc1fe1 100644 --- a/lib/withDefault.js +++ b/lib/withDefault.js @@ -1,5 +1,5 @@ import update from './update'; -import curry from 'lodash/function/curry'; +import curry from './util/curry'; function withDefault(defaultValue, updates, object) { if (typeof object === 'undefined') { diff --git a/lib/wrap.js b/lib/wrap.js index 1a81f4f..918e286 100644 --- a/lib/wrap.js +++ b/lib/wrap.js @@ -1,4 +1,4 @@ -import curry from 'lodash/function/curry'; +import curry from './util/curry'; import freeze from './freeze'; export default function wrap(func, length = func.length) {