util

Methods

(static) convert(updater) → {function}

Source:
Since:
  • 0.2.0
See:
  • update for more information.
Wraps an updater function, returning a new function taking object, path and …args as parameters.
The updater function is invoked with value and …args.
Be carefull, the updater function must not mutate its value argument.
Example

Wrapping an updater

const inc = (v, i = 1) => v + i // this function increments a number with an optional value which defaults to 1
const incProp = convert(inc)
const object = { nested: { prop: 4 } }
incProp(object, 'nested.prop') // => { nested: { prop: 5 } }
incProp(object, 'nested.prop', 2) // => { nested: { prop: 6 } }
Parameters:
Name Type Description
updater function The updater function.
Returns:
Returns the wrapped function.
Type
function

(static) protect(reference) → {function}

Source:
Since:
  • 0.3.0
🚫 This is an experimental feature, do not use.
Protect a reference, allowing to apply mutations on it, and then retrieve the result of the mutations.
Example
protect({ a: 1, b: { c: 3 } })(protectedRef => {
  protectedRef.a++
  protectedRef.b.d = protectedRef.a * protectedRef.b.c
  delete protectedRef.b.c
}) // => { a: 2, b: { d: 6 } }
Parameters:
Name Type Description
reference Object Reference to protect.
Returns:
Function to be called with a callback accepting the protected reference, returning the result of the mutations performed by the callback.
Type
function

(static) using(…paths) → {Object}

Source:
Since:
  • 0.1.12
Allows to specify one or several paths to use as arguments for an immutadot function call.
using.placeholder may be used to insert a passed argument before a path argument (see second example).
Examples

Push b, c and 4 to a

const o = { nested: { a: [1], b: 2, c: 3 } }
using('nested.b', 'nested.c').push(o, 'nested.a', 4) // => { nested: { a: [1, 2, 3, 4], b: 2, c: 3 } }

Replace a by a * b + 4 (use placeholder to send updater as first argument)

const o = { nested: { a: 2, b: 3 } }
using(using.placeholder, 'nested.b')
  .update(o, 'nested.a', (a, b, c) => a * b + c, 4) // => { nested: { a: 10, b: 3 } }
Parameters:
Name Type Attributes Description
paths Array | string
repeatable
The paths to use as arguments.
Returns:
Returns an object with immutadot functions.
Type
Object