-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathindex.d.ts
More file actions
263 lines (233 loc) · 7.95 KB
/
index.d.ts
File metadata and controls
263 lines (233 loc) · 7.95 KB
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
/**
Return this value from a `mapper` function to remove a key from an object.
@example
```
import mapObject, {mapObjectSkip} from 'map-obj';
const object = {one: 1, two: 2};
const mapper = (key, value) => value === 1 ? [key, value] : mapObjectSkip;
const result = mapObject(object, mapper);
console.log(result);
//=> {one: 1}
```
*/
export const mapObjectSkip: unique symbol;
/**
Mapper function for transforming object keys and values.
*/
export type Mapper<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
> = (
sourceKey: Extract<keyof SourceObjectType, string>,
sourceValue: SourceObjectType[keyof SourceObjectType],
source: SourceObjectType
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
mapperOptions?: MapperOptions,
] | typeof mapObjectSkip;
/**
Mapper function when `includeSymbols: true` is enabled.
*/
export type MapperWithSymbols<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
> = (
sourceKey: keyof SourceObjectType,
sourceValue: SourceObjectType[keyof SourceObjectType],
source: SourceObjectType
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
mapperOptions?: MapperOptions,
] | typeof mapObjectSkip;
/**
Mapper used when `{deep: true}` is enabled.
In deep mode we may visit nested objects with keys and values unrelated to the top-level object, so we intentionally widen the key and value types.
*/
type DeepMapper<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
> = (
sourceKey: string,
sourceValue: unknown,
source: SourceObjectType
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
mapperOptions?: MapperOptions,
] | typeof mapObjectSkip;
/**
Deep mapper when `includeSymbols: true` is enabled.
*/
type DeepMapperWithSymbols<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
> = (
sourceKey: string | symbol,
sourceValue: unknown,
source: SourceObjectType
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
mapperOptions?: MapperOptions,
] | typeof mapObjectSkip;
export type Options = {
/**
Recurse nested objects and objects in arrays.
@default false
Built-in objects like `RegExp`, `Error`, `Date`, `Map`, `Set`, `WeakMap`, `WeakSet`, `Promise`, `ArrayBuffer`, `DataView`, typed arrays (Uint8Array, etc.), and `Blob` are not recursed into. Special objects like Jest matchers are also automatically excluded.
*/
readonly deep?: boolean;
/**
Include symbol keys in the iteration.
By default, symbol keys are completely ignored and not passed to the mapper function. When enabled, the mapper will also be called with symbol keys from the source object, allowing them to be transformed or included in the result. Only enumerable symbol properties are included.
@default false
*/
readonly includeSymbols?: boolean;
/**
The target object to map properties onto.
@default {}
*/
readonly target?: Record<string | symbol, unknown>;
};
export type DeepOptions = {
readonly deep: true;
} & Options;
export type TargetOptions<TargetObjectType extends Record<string | symbol, unknown>> = {
readonly target: TargetObjectType;
} & Options;
export type SymbolOptions = {
readonly includeSymbols: true;
} & Options;
export type MapperOptions = {
/**
Whether to recurse into `targetValue`.
Requires `deep: true`.
@default true
*/
readonly shouldRecurse?: boolean;
};
/**
Map object keys and values into a new object.
@param source - The source object to copy properties from.
@param mapper - A mapping function.
@example
```
import mapObject, {mapObjectSkip} from 'map-obj';
// Swap keys and values
const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]);
//=> {bar: 'foo'}
// Convert keys to lowercase (shallow)
const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]);
//=> {foo: true, bar: {bAz: true}}
// Convert keys to lowercase (deep recursion)
const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true});
//=> {foo: true, bar: {baz: true}}
// Filter out specific values
const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObjectSkip);
//=> {one: 1}
// Include symbol keys
const symbol = Symbol('foo');
const newObject = mapObject({bar: 'baz', [symbol]: 'qux'}, (key, value) => [key, value], {includeSymbols: true});
//=> {bar: 'baz', [Symbol(foo)]: 'qux'}
```
*/
// Overloads with includeSymbols: true
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
TargetObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: DeepMapperWithSymbols<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions & SymbolOptions & TargetOptions<TargetObjectType>
): TargetObjectType & Record<string | symbol, unknown>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: DeepMapperWithSymbols<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions & SymbolOptions
): Record<string | symbol, unknown>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
TargetObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: MapperWithSymbols<
SourceObjectType,
MappedObjectKeyType,
MappedObjectValueType
>,
options: SymbolOptions & TargetOptions<TargetObjectType>
): TargetObjectType & Record<MappedObjectKeyType, MappedObjectValueType>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: MapperWithSymbols<
SourceObjectType,
MappedObjectKeyType,
MappedObjectValueType
>,
options: SymbolOptions
): Record<MappedObjectKeyType, MappedObjectValueType>;
// Overloads without includeSymbols (default)
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
TargetObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: DeepMapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions & TargetOptions<TargetObjectType>
): TargetObjectType & Record<string | symbol, unknown>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: DeepMapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions
): Record<string | symbol, unknown>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
TargetObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: Mapper<
SourceObjectType,
MappedObjectKeyType,
MappedObjectValueType
>,
options: TargetOptions<TargetObjectType>
): TargetObjectType & Record<MappedObjectKeyType, MappedObjectValueType>;
export default function mapObject<
SourceObjectType extends Record<string | symbol, unknown>,
MappedObjectKeyType extends string | symbol,
MappedObjectValueType,
>(
source: SourceObjectType,
mapper: Mapper<
SourceObjectType,
MappedObjectKeyType,
MappedObjectValueType
>,
options?: Options
): Record<MappedObjectKeyType, MappedObjectValueType>;