over.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import _curry3 from "./internal/_curry3.js"; // `Identity` is a functor that holds a single value, where `map` simply
  2. // transforms the held value with the provided function.
  3. var Identity = function (x) {
  4. return {
  5. value: x,
  6. map: function (f) {
  7. return Identity(f(x));
  8. }
  9. };
  10. };
  11. /**
  12. * Returns the result of "setting" the portion of the given data structure
  13. * focused by the given lens to the result of applying the given function to
  14. * the focused value.
  15. *
  16. * @func
  17. * @memberOf R
  18. * @since v0.16.0
  19. * @category Object
  20. * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
  21. * @sig Lens s a -> (a -> a) -> s -> s
  22. * @param {Lens} lens
  23. * @param {*} v
  24. * @param {*} x
  25. * @return {*}
  26. * @see R.view, R.set, R.lens, R.lensIndex, R.lensProp, R.lensPath
  27. * @example
  28. *
  29. * const headLens = R.lensIndex(0);
  30. *
  31. * R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
  32. */
  33. var over =
  34. /*#__PURE__*/
  35. _curry3(function over(lens, f, x) {
  36. // The value returned by the getter function is first transformed with `f`,
  37. // then set as the value of an `Identity`. This is then mapped over with the
  38. // setter function of the lens.
  39. return lens(function (y) {
  40. return Identity(f(y));
  41. })(x).value;
  42. });
  43. export default over;