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