-
Notifications
You must be signed in to change notification settings - Fork 7
/
over.ts
67 lines (55 loc) · 2.05 KB
/
over.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { GetTransformer, Lens, LensTransformer } from './lens.ts';
import curryN from './utils/curry_n.ts';
import type { FuncArr1, PH } from './utils/types.ts';
// @types
type Over_1<T, F> = (lens: Lens<T, F>) => T;
type Over_2<T, F> = (fn: FuncArr1<F, F>) => T;
type Over_3<T, F> = (target: T) => T;
type Over_2_3<T, F> =
& ((fn: FuncArr1<F, F>) => Over_3<T, F>)
& ((fn: PH, target: T) => Over_2<T, F>)
& ((fn: FuncArr1<F, F>, target: T) => T);
type Over_1_3<F> =
& (<T>(lens: Lens<T, F>) => Over_3<T, F>)
& (<T>(lens: PH, target: T) => Over_1<T, F>)
& (<T>(lens: Lens<T, F>, target: T) => T);
type Over_1_2<T> =
& (<F>(lens: Lens<T, F>) => Over_2<T, F>)
& (<F>(lens: PH, fn: FuncArr1<F, F>) => Over_1<T, F>)
& (<F>(lens: Lens<T, F>, fn: FuncArr1<F, F>) => T);
type Over =
& (<T, F>(lens: Lens<T, F>) => Over_2_3<T, F>)
& (<F>(lens: PH, fn: FuncArr1<F, F>) => Over_1_3<F>)
& (<T>(lens: PH, fn: PH, target: T) => Over_1_2<T>)
& (<T, F>(lens: Lens<T, F>, fn: FuncArr1<F, F>) => Over_3<T, F>)
& (<T, F>(lens: Lens<T, F>, fn: PH, target: T) => Over_2<T, F>)
& (<T, F>(lens: PH, fn: FuncArr1<F, F>, target: T) => Over_1<T, F>)
& (<T, F>(lens: Lens<T, F>, fn: FuncArr1<F, F>, target: T) => T);
function _overTransformer<T, F>(focus: F): LensTransformer<T, F, T> {
return {
value: focus,
transform(setter, target) {
return _overTransformer(setter(this.value, target));
},
};
}
function _over<T, F>(
lens: Lens<T, F>,
fn: FuncArr1<F, F>,
target: T,
): T {
return lens(
((focus) => _overTransformer(fn(focus))) as GetTransformer<T, F, T>,
)(target)
.value;
}
/**
* Returns the result of "setting" the portion of the given data structure `target`
* focused by the given `lens` to the result of applying the given function `fn` to
* the focused value.
*
* const headLens = Fae.lensIndex(0)
* Fae.over(headLens, (x: string) => x.toUpperCase(), ['foo', 'bar', 'baz']) // ['FOO', 'bar', 'baz']
*/
export const over = curryN(3, _over) as Over;