lensPath.js 997 B

123456789101112131415161718192021222324252627282930313233343536
  1. import _curry1 from "./internal/_curry1.js";
  2. import assocPath from "./assocPath.js";
  3. import lens from "./lens.js";
  4. import path from "./path.js";
  5. /**
  6. * Returns a lens whose focus is the specified path.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.19.0
  11. * @category Object
  12. * @typedefn Idx = String | Int | Symbol
  13. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  14. * @sig [Idx] -> Lens s a
  15. * @param {Array} path The path to use.
  16. * @return {Lens}
  17. * @see R.view, R.set, R.over
  18. * @example
  19. *
  20. * const xHeadYLens = R.lensPath(['x', 0, 'y']);
  21. *
  22. * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  23. * //=> 2
  24. * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  25. * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
  26. * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
  27. * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
  28. */
  29. var lensPath =
  30. /*#__PURE__*/
  31. _curry1(function lensPath(p) {
  32. return lens(path(p), assocPath(p));
  33. });
  34. export default lensPath;