-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
367 lines (338 loc) · 10.5 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/** Given an object, a sequence of keys, and a value, deep update that value by recursively copying. Type safe. */
let _generalUpdater = makeUpdater();
export const updateIn: SafeUpdate = function(o: any, ...args: any[]) {
return _generalUpdater(o, args[args.length - 1], args, 0, args.length - 2);
};
/** Core lens shape. Used to construct Lens and can be passed to higher-order functions, such as Lens.comp */
export interface ILens<T, V> {
get(t: T): V;
set(a: T, value: V): T;
}
/** An object which can be used for getting and copy-and-updating substructure of objects. */
export interface Lens<T, V> {
/** Gets the value from the object */
(t: T): V;
/** Gets the value from the object */
get(t: T): V;
/** Given a value, returns a function which updates its argument to that value */
set(v: V): <ST extends T>(t: ST) => ST;
/** Given an object and a value, updates the object's referenced state to that value. */
set<ST extends T>(a: ST, value: V): ST;
/** Given a function that operates on a value, return a function that uses it to update an object's internal value */
update(fn: (v: V) => V): (t: T) => T;
/** Given an object and a function that operates on it's value, return an updated object. */
update(t: T, fn: (v: V) => V): T;
comp<V2>(l: Lens<V, V2>): Lens<T, V2>;
comp<U, V2>(l1: Lens<V, U>, l2: Lens<U, V2>): Lens<T, V2>;
comp<U1, U2, V2>(
l1: Lens<V, U1>,
l2: Lens<U1, U2>,
l3: Lens<U2, V2>
): Lens<T, V2>;
}
/** Core prism shape. Used to construct Prisms and can be passed to higher-order Prism functions, such as comp */
export interface IPrism<T, V> {
get(t: T): V | undefined;
set(a: T, value: V): T;
}
/** An object which can be used for getting and copy-and-updating potentially-undefined substructure of objects. Like lens, but used for optional things. */
export interface Prism<T, V> {
(t: T): V | undefined;
get(t: T): V | undefined;
set(v: V): <ST extends T>(t: ST) => ST;
set<ST extends T>(a: ST, value: V): ST;
update(fn: (v: V | undefined) => V | undefined): (t: T) => T;
update(t: T, fn: (v: V | undefined) => V | undefined): T;
}
/** A mapping to/from one type to another. Used with the `map` function to create a lens that operates on one type via another. */
export type Isomorphism<T, V> = {
to: (t: T) => V;
from: (v: V) => T;
};
export namespace Isomorphism {
export function flip<T, U>(iso: Isomorphism<T, U>): Isomorphism<U, T> {
return { from: iso.to, to: iso.from };
}
export function comp<T, U, V>(
iso: Isomorphism<T, U>,
iso2: Isomorphism<U, V>
): Isomorphism<T, V>;
export function comp<T, U, V, W>(
iso: Isomorphism<T, U>,
iso2: Isomorphism<U, V>,
iso3: Isomorphism<V, W>
): Isomorphism<T, W>;
export function comp<T, U, V, W, X>(
iso: Isomorphism<T, U>,
iso2: Isomorphism<U, V>,
iso3: Isomorphism<V, W>,
iso4: Isomorphism<W, X>
): Isomorphism<T, X>;
export function comp(...isos: Isomorphism<any, any>[]): Isomorphism<any, any>;
export function comp(
...isos: Isomorphism<any, any>[]
): Isomorphism<any, any> {
return {
to(start) {
return isos.reduce((val, iso) => iso.to(val), start);
},
from(end) {
return isos.reduceRight((val, iso) => iso.from(val), end);
}
};
}
}
/** Core module for creating and using prisms, which are get/set proxies that gracefully handle undefined. */
export namespace Prism {
export function of<T, V>(spec: IPrism<T, V>): Prism<T, V> {
var func = <Prism<T, V>>function(o: T) {
return spec.get(o);
};
const set = function(tOrV: T | V, v?: V) {
if (arguments.length === 1) {
return (t: T) => spec.set(t, <V>tOrV);
} else {
return spec.set(<T>tOrV, v!);
}
};
const update = (tOrFn: T | Function, f?: Function) => {
if (f === undefined) {
return (t: T) => func.update(t, <any>tOrFn);
} else {
const t = <T>tOrFn;
const v = spec.get(t);
if (v === undefined) {
return t;
} else {
return spec.set(t, <V>f(v));
}
}
};
const comp = (...prisms: any[]) => (Prism.comp as any)(func, ...prisms);
(func as any).get = spec.get;
(func as any).set = set;
(func as any).update = update;
(func as any).comp = comp;
return <Prism<T, V>>func;
}
export type Prismish<T, U> = ILens<T, U> | IPrism<T, U>;
export function comp<T, U, V>(
l1: Prismish<T, U>,
l2: Prismish<U, V>
): Prism<T, V>;
export function comp<T, U1, U2, V>(
l1: Prismish<T, U1>,
l2: Prismish<U1, U2>,
l3: IPrism<U2, V>
): Prism<T, V>;
export function comp<T, U1, U2, U3, V>(
l1: Prismish<T, U1>,
l2: Prismish<U1, U2>,
l3: IPrism<U2, U3>,
l4: Prismish<U3, V>
): Prism<T, V>;
export function comp<T, U1, U2, U3, U4, V>(
l1: Prismish<T, U1>,
l2: Prismish<U1, U2>,
l3: IPrism<U2, U3>,
l4: Prismish<U3, U4>,
l5: Prismish<U4, V>
): Prism<T, V>;
export function comp(...prisms: Prismish<any, any>[]): Prism<any, any> {
let performComposedSet: any;
return Prism.of({
get: (o: any) =>
prisms.reduce((o, l) => (o === undefined ? o : l.get(o)), o),
set: (o: any, v: any) => {
if (performComposedSet === undefined) {
performComposedSet = makeComposedSetter();
}
return performComposedSet(o, v, prisms, 0);
}
});
}
}
/** Core module for dealing with lenses. Lenses are objects that make it easy to get and update nested structure in a composable, functional way. */
export namespace Lens {
/** Returns a builder which can be used to create a lens that updates a simple object value or nested value */
export function from<T>() {
return new LensFactory<T>();
}
/** Creates a Lens from a simple get/set specification. */
export function of<T, V>(spec: ILens<T, V>): Lens<T, V> {
return (Prism.of as any)(spec);
}
/** Given a lens and a way to map that lens value type to/from another (an isomorphism), returns a lens that can produce that other type. */
export function map<T, U, V>(
l: ILens<T, U>,
f: Isomorphism<U, V>
): Lens<T, V> {
return Lens.of({
get: (o: T) => f.to(l.get(o)),
set: (o, v) => l.set(o, f.from(v))
});
}
/** Given a lens and a way to map into that lens input type from another (an isomorphism), returns a lens that can operate on the other type. */
export function unmap<T, U, V>(
iso: Isomorphism<T, U>,
lens: ILens<U, V>
): Lens<T, V> {
return Lens.of<T, V>({
get: opaque => lens.get(iso.to(opaque)),
set: (opaque, value) => iso.from(lens.set(iso.to(opaque), value))
});
}
/** Compose together lenses for updating nested structures. */
export function comp<T, U, V>(l1: ILens<T, U>, l2: ILens<U, V>): Lens<T, V>;
/** Compose together lenses for updating nested structures. */
export function comp<T, U1, U2, V>(
l1: ILens<T, U1>,
l2: ILens<U1, U2>,
l3: ILens<U2, V>
): Lens<T, V>;
/** Compose together lenses for updating nested structures. */
export function comp<T, U1, U2, U3, V>(
l1: ILens<T, U1>,
l2: ILens<U1, U2>,
l3: ILens<U2, U3>,
l4: ILens<U3, V>
): Lens<T, V>;
/** Compose together lenses for updating nested structures. */
export function comp<T, U1, U2, U3, U4, V>(
l1: ILens<T, U1>,
l2: ILens<U1, U2>,
l3: ILens<U2, U3>,
l4: ILens<U3, U4>,
l5: ILens<U4, V>
): Lens<T, V>;
/** Compose together lenses for updating nested structures. */
export function comp(...lenses: ILens<any, any>[]): Lens<any, any> {
return Prism.comp.apply(undefined, lenses as any) as any;
}
}
export interface SafeUpdate {
<O, K1 extends keyof O>(o: O, k: K1, v: O[K1]): O;
<O, K1 extends keyof O, K2 extends keyof O[K1]>(
o: O,
k: K1,
k2: K2,
v: O[K1][K2]
): O;
<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2]>(
o: O,
k: K1,
k2: K2,
k3: K3,
v: O[K1][K2][K3]
): O;
<
O,
K1 extends keyof O,
K2 extends keyof O[K1],
K3 extends keyof O[K1][K2],
K4 extends keyof O[K1][K2][K3]
>(
o: O,
k: K1,
k2: K2,
k3: K3,
k4: K4,
v: O[K1][K2][K3][K4]
): O;
<
O,
K1 extends keyof O,
K2 extends keyof O[K1],
K3 extends keyof O[K1][K2],
K4 extends keyof O[K1][K2][K3],
K5 extends keyof O[K1][K2][K3][K4]
>(
o: O,
k: K1,
k2: K2,
k3: K3,
k4: K4,
k5: K5,
v: O[K1][K2][K3][K4][K5]
): O;
}
/** Factory to create monomorphic composed setters */
function makeComposedSetter() {
const performComposedSet = (
o: any,
v: any,
lenses: ILens<any, any>[],
index: number
) => {
if (index == lenses.length - 1) {
return lenses[index].set(o, v);
} else {
const inner = lenses[index].get(o);
if (inner) {
return lenses[index].set(
o,
performComposedSet(inner, v, lenses, index + 1)
);
} else {
return o;
}
}
};
return performComposedSet;
}
/** Factory to create monomorphic updaters */
function makeUpdater() {
const performUpdate = (
o: any,
v: any,
keys: string[],
idx: number,
last: number
) => {
const copy = { ...o };
if (idx == last) {
copy[keys[idx]] = v;
return copy;
} else {
copy[keys[idx]] = performUpdate(o[keys[idx]], v, keys, idx + 1, last);
return copy;
}
};
return performUpdate;
}
export class LensFactory<O> {
/** Creates lenses that access/update substructure via a keypath. */
prop<K extends keyof O>(k: K): Lens<O, O[K]>;
/** Creates lenses that access/update substructure via a keypath. */
prop<K extends keyof O, K2 extends keyof O[K]>(
k: K,
k2: K2
): Lens<O, O[K][K2]>;
/** Creates lenses that access/update substructure via a keypath. */
prop<K extends keyof O, K2 extends keyof O[K], K3 extends keyof O[K][K2]>(
k: K,
k2: K2,
k3: K3
): Lens<O, O[K][K2][K3]>;
/** Creates lenses that access/update substructure via a keypath. */
prop<
K extends keyof O,
K2 extends keyof O[K],
K3 extends keyof O[K][K2],
K4 extends keyof O[K][K2][K3]
>(k: K, k2: K2, k3: K3, k4: K4): Lens<O, O[K][K2][K3][K4]>;
prop(...ks: string[]): Lens<any, any> {
let performUpdate: any;
return Lens.of({
get(o: O) {
return ks.reduce((x, k) => (x as any)[k], o);
},
set(o: O, v: any) {
if (performUpdate === undefined) {
performUpdate = makeUpdater();
}
return performUpdate(o, v, ks, 0, ks.length - 1);
}
});
}
}