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.<br/>
  4. * <code>using.placeholder</code> may be used to insert a passed argument before a path argument (see second example).
  5. * @function
  6. * @memberof util
  7. * @param {...(Array|string)} paths The paths to use as arguments.
  8. * @return {Object} Returns an object with immutadot functions.
  9. * @example <caption>Push <code>b</code>, <code>c</code> and <code>4</code> to <code>a</code></caption>
  10. * const o = { nested: { a: [1], b: 2, c: 3 } }
  11. * using('nested.b', 'nested.c').push(o, 'nested.a', 4) // => { nested: { a: [1, 2, 3, 4], b: 2, c: 3 } }
  12. * @example <caption>Replace <code>a</code> by <code>a * b + 4</code> (use placeholder to send <code>updater</code> as first argument)</caption>
  13. * const o = { nested: { a: 2, b: 3 } }
  14. * using(using.placeholder, 'nested.b')
  15. * .update(o, 'nested.a', (a, b, c) => a * b + c, 4) // => { nested: { a: 10, b: 3 } }
  16. * @since 0.1.12
  17. */
  18. const using = (...paths) => new UsingWrapper(...paths)
  19. const { placeholder } = UsingWrapper
  20. using.placeholder = placeholder
  21. using._ = placeholder
  22. export {
  23. placeholder,
  24. placeholder as _,
  25. using as default,
  26. }