/
Record.ts
178 lines (165 loc) · 4.54 KB
/
Record.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
/**
* @since 0.1.0
*/
import { Endomorphism, flow, not, Predicate } from "fp-ts/function"
import { Option } from "fp-ts/Option"
import * as R from "fp-ts/Record"
import * as A from "fp-ts/Array"
import * as T from "fp-ts/Tuple"
import { getLastSemigroup } from "fp-ts/Semigroup"
/**
* Get the values from a `Record`.
*
* @example
* import { values } from 'fp-ts-std/Record';
*
* const x = { a: 1, b: 'two' };
*
* assert.deepStrictEqual(values(x), [1, 'two']);
*
* @since 0.1.0
*/
export const values = <A>(x: Record<string, A>): Array<A> => Object.values(x)
/**
* Like `lookup` from fp-ts, but flipped.
*
* @example
* import { lookupFlipped } from 'fp-ts-std/Record';
* import * as A from 'fp-ts/Array';
*
* const x = { a: 1, b: 'two', c: [true] };
* const ks = ['a', 'c'];
*
* assert.deepStrictEqual(A.filterMap(lookupFlipped(x))(ks), [1, [true]]);
*
* @since 0.1.0
*/
export const lookupFlipped = <A>(x: Record<string, A>) => (
k: string,
): Option<A> => R.lookup(k)(x)
/**
* Pick a set of keys from a `Record`. The value-level equivalent of the `Pick`
* type.
*
* @example
* import { pick } from 'fp-ts-std/Record';
*
* type MyType = { a: number; b: string; c: Array<boolean> };
* const picked = pick<MyType>()(['a', 'c']);
*
* assert.deepStrictEqual(picked({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] });
*
* @since 0.1.0
*/
export const pick = <A>() => <K extends keyof A>(ks: Array<K>) => (
x: A,
): Pick<A, K> => {
// I don't believe there's any reasonable way to model this sort of
// transformation in the type system without an assertion - at least here
// it's in a single reused place
const o = {} as Pick<A, K>
/* eslint-disable */
for (const k of ks) {
o[k] = x[k]
}
/* eslint-enable */
return o
}
/**
* Omit a set of keys from a `Record`. The value-level equivalent of the `Omit`
* type.
*
* @example
* import { omit } from 'fp-ts-std/Record';
*
* const sansB = omit(['b']);
*
* assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] });
*
* @since 0.1.0
*/
export const omit = <K extends string>(ks: Array<K>) => <
V,
A extends Record<K, V>
>(
x: Partial<A>,
): Omit<A, K> => {
const y = { ...x }
/* eslint-disable */
for (const k of ks) {
delete y[k]
}
/* eslint-enable */
return y as Omit<A, K>
}
/**
* Filters out key/value pairs in the record for which the predicate upon the
* value holds. This can be thought of as the inverse of ordinary record
* filtering.
*
* @example
* import { reject } from 'fp-ts-std/Record';
* import { Predicate } from 'fp-ts/function';
*
* const isEven: Predicate<number> = n => n % 2 === 0;
*
* assert.deepStrictEqual(reject(isEven)({ a: 1, b: 2, c: 3, d: 4 }), { a: 1, c: 3 });
*
* @since 0.7.0
*/
export const reject = <A>(f: Predicate<A>): Endomorphism<Record<string, A>> =>
R.filter(not(f))
/**
* Merge two records together. For merging many identical records, instead
* consider defining a semigroup.
*
* @example
* import { merge } from 'fp-ts-std/Record';
*
* assert.deepStrictEqual(merge({ a: 1, b: 2 })({ b: 'two', c: true }), { a: 1, b: 'two', c: true });
*
* @since 0.7.0
*/
export const merge = <A>(x: A) => <B>(y: B): A & B => ({ ...x, ...y })
/**
* Invert a record, keeping only the last value should the same key be
* encountered more than once. If you'd like to keep the values that would be
* lost, see instead `invertAll`.
*
* @example
* import { invertLast } from 'fp-ts-std/Record';
* import { fromNumber } from 'fp-ts-std/String';
*
* assert.deepStrictEqual(invertLast(fromNumber)({ a: 1, b: 2, c: 2, d: 3 }), { '1': 'a', '2': 'c', '3': 'd' });
*
* @since 0.7.0
*/
export const invertLast = <A>(
f: (x: A) => string,
): ((x: Record<string, A>) => Record<string, string>) =>
flow(
R.toArray,
A.map(flow(T.mapLeft(f), T.swap)),
R.fromFoldable(getLastSemigroup<string>(), A.array),
)
/**
* Invert a record, collecting values with duplicate keys in an array. Should
* you only care about the last item or are not worried about the risk of
* duplicate keys, see instead `invertLast`.
*
* @example
* import { invertAll } from 'fp-ts-std/Record';
* import { fromNumber } from 'fp-ts-std/String';
*
* assert.deepStrictEqual(invertAll(fromNumber)({ a: 1, b: 2, c: 2, d: 3 }), { '1': ['a'], '2': ['b', 'c'], '3': ['d'] });
*
* @since 0.7.0
*/
export const invertAll = <A>(
f: (x: A) => string,
): ((x: Record<string, A>) => Record<string, Array<string>>) =>
flow(
R.toArray,
A.map(flow(T.bimap(f, A.of), T.swap)),
R.fromFoldable(A.getMonoid<string>(), A.array),
)