core/get.js

  1. import { nav } from 'nav/nav'
  2. import { toPath } from 'immutadot-parser'
  3. const isGetter = Symbol('isGetter')
  4. /**
  5. * Gets the value at <code>path</code> of <code>obj</code>.
  6. * @memberof core
  7. * @param {*} [obj] The object.
  8. * @param {string|Array} path The path of the property to get.
  9. * @return {*} Returns the value
  10. * @example get({ nested: { prop: 'val' } }, 'nested.prop') // => 'val'
  11. * @since 1.0.0
  12. */
  13. function get(...args) {
  14. if (args.length >= 2)
  15. return _get(...args)
  16. const getter = obj => _get(obj, ...args)
  17. getter[isGetter] = true
  18. return getter
  19. }
  20. function _get(obj, path) {
  21. return nav(toPath(path))(obj).get()
  22. }
  23. function resolveGetter(value, obj) {
  24. if (value && value[isGetter]) return value(obj)
  25. return value
  26. }
  27. // FIXME stop exporting isGetter
  28. export { get, isGetter, resolveGetter }