-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
composer-helpers.ts
79 lines (68 loc) · 3.05 KB
/
composer-helpers.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
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Console from 'global/console';
const identity = state => state;
/** Returns a function that logs a value with a given message */
export function log(text: string): (value: any) => void {
return value => Console.log(text, value);
}
/** Wraps a value in an object and stores it the `payload` field */
export function payload_<P>(payload: P) {
return {payload};
}
/** Wraps a value in an object and stores it the `payload` field */
export function apply_<State, P>(
updater: (state: State, nextPayload: P) => State,
payload: P
): (state: State) => State {
return state => updater(state, payload);
}
export function with_<State>(
fn: (state: State) => (nextState: State) => State
): (state: State) => State {
return state => fn(state)(state);
}
export function if_<State>(pred: boolean, fn: (state: State) => State): (state: State) => State {
return pred ? fn : identity;
}
export function compose_<State>(fns: Array<(s: State) => State>): (s: State) => State {
return state => fns.reduce((state2, fn) => fn(state2), state);
}
/** Returns a reducer function that merges props with state */
export function merge_<Props>(obj: Props): <State>(state: State) => State {
return state => ({...state, ...obj});
}
export function pick_<Prop extends string>(
prop: Prop
): <Value>(fn: (p: Value) => Value) => <State extends Record<Prop, Value>>(state: State) => State {
return fn => state => ({...state, [prop]: fn(state[prop])});
}
export function swap_<X extends {id: string}>(item: X): (arr: X[]) => X[] {
return arr => arr.map(a => (a.id === item.id ? item : a));
}
export function map_<X, T>(fn: (state: X) => T): (arr: X[]) => T[] {
return arr => arr.map(e => fn(e));
}
export function filterOutById<X extends {id: string}>(id: string): (arr: X[]) => X[] {
return arr => arr.filter(e => e.id !== id);
}
export function removeElementAtIndex<X>(index: number): (arr: X[]) => X[] {
return arr => [...arr.slice(0, index), ...arr.slice(index + 1, arr.length)];
}