seq/chain.js

  1. import ChainWrapper from './ChainWrapper'
  2. /**
  3. * Creates an immutadot wrapper instance that wraps <code>object</code> allowing to make sequences of immutadot functions calls on it.<br/>
  4. * The result of such sequences must be unwrapped with {@link seq#value|value}.<br/>
  5. * The object on which the functions are called may be changed with an optional <code>path</code>.
  6. * @function
  7. * @memberof seq
  8. * @param {Object} object The object to wrap.
  9. * @param {Array|string} [path] The path of the object on which functions are called.
  10. * @return {Object} Returns the new immutadot wrapper instance.
  11. * @example <caption>Chain several operations</caption>
  12. * chain({ nested1: { prop: 'old' }, nested2: { prop: 1 } })
  13. * .set('nested1.prop', 'new')
  14. * .unset('nested2.prop')
  15. * .value() // => { nested1: { prop: 'new' }, nested2: {} }
  16. * @example <caption>Chain on a path</caption>
  17. * chain({ nested: { prop1: 'old', prop2: 1 } }, 'nested')
  18. * .set('prop1', 'new')
  19. * .unset('prop2')
  20. * .value() // => { nested: { prop1: 'new' } }
  21. * @see {@link seq#value|value} for more information.
  22. * @since 0.1.8
  23. */
  24. const chain = (object, path) => new ChainWrapper(object, path)
  25. export default chain