util/using.js

  1. import UsingWrapper from './UsingWrapper'
  2. /**
  3. * Allows to specify one or several paths to use as arguments for an immutadot function call.
  4. * @function using
  5. * @memberof util
  6. * @param {...(Array|string)} paths The paths to use as arguments.
  7. * @return {Object} Returns an object with immutadot functions.
  8. * @example <caption>Add <code>b</code> to <code>a</code></caption>
  9. * const o = { nested: { a: 2, b: 3 } }
  10. * using('nested.b').add(o, 'nested.a') // => { nested: { a: 5, b: 3 } }
  11. * @example <caption>Replace <code>a</code> by <code>a * b + c</code></caption>
  12. * const o = { nested: { a: 2, b: 3, c: 4 } }
  13. * using('nested.b', 'nested.c')
  14. * .update(o, 'nested.a', (a, b, c) => a * b + c) // => { nested: { a: 10, b: 3, c: 4 } }
  15. * @since 0.1.12
  16. */
  17. export default (...paths) => new UsingWrapper(...paths)