-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (100 loc) · 3.2 KB
/
index.js
File metadata and controls
126 lines (100 loc) · 3.2 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
const isObject = value => typeof value === 'object' && value !== null;
// Check if a value is a plain object that should be recursed into
const isObjectCustom = value => {
if (!isObject(value)) {
return false;
}
// Exclude built-in objects
if (
value instanceof RegExp
|| value instanceof Error
|| value instanceof Date
|| value instanceof Map
|| value instanceof Set
|| value instanceof WeakMap
|| value instanceof WeakSet
|| value instanceof Promise
|| value instanceof ArrayBuffer
|| value instanceof DataView
|| ArrayBuffer.isView(value) // Typed arrays
|| (globalThis.Blob && value instanceof globalThis.Blob)
) {
return false;
}
// Exclude Jest matchers
if (typeof value.$$typeof === 'symbol' || typeof value.asymmetricMatch === 'function') {
return false;
}
return true;
};
export const mapObjectSkip = Symbol('mapObjectSkip');
const getEnumerableKeys = (object, includeSymbols) => {
if (includeSymbols) {
const stringKeys = Object.keys(object);
const symbolKeys = Object.getOwnPropertySymbols(object).filter(symbol => Object.getOwnPropertyDescriptor(object, symbol)?.enumerable);
return [...stringKeys, ...symbolKeys];
}
return Object.keys(object);
};
const _mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
const {
target = {},
...processOptions
} = {
deep: false,
includeSymbols: false,
...options,
};
if (isSeen.has(object)) {
return isSeen.get(object);
}
isSeen.set(object, target);
const mapArray = array => array.map(element => isObjectCustom(element) ? _mapObject(element, mapper, processOptions, isSeen) : element);
if (Array.isArray(object)) {
return mapArray(object);
}
for (const key of getEnumerableKeys(object, processOptions.includeSymbols)) {
const value = object[key];
const mapResult = mapper(key, value);
if (mapResult === mapObjectSkip) {
continue;
}
if (!Array.isArray(mapResult)) {
throw new TypeError(`Mapper must return an array or mapObjectSkip, got ${mapResult === null ? 'null' : typeof mapResult}`);
}
if (mapResult.length < 2) {
throw new TypeError(`Mapper must return an array with at least 2 elements [key, value], got ${mapResult.length} elements`);
}
let [newKey, newValue, {shouldRecurse = true} = {}] = mapResult;
// Drop `__proto__` keys.
if (newKey === '__proto__') {
continue;
}
if (processOptions.deep && shouldRecurse && isObjectCustom(newValue)) {
newValue = Array.isArray(newValue)
? mapArray(newValue)
: _mapObject(newValue, mapper, processOptions, isSeen);
}
try {
target[newKey] = newValue;
} catch (error) {
if (error.name === 'TypeError' && error.message.includes('read only')) {
// Skip non-configurable properties
continue;
}
throw error;
}
}
return target;
};
export default function mapObject(object, mapper, options) {
if (!isObject(object)) {
throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
}
if (Array.isArray(object)) {
throw new TypeError('Expected an object, got an array');
}
// Ensure the third mapper argument is always the original input object
const mapperWithRoot = (key, value) => mapper(key, value, object);
return _mapObject(object, mapperWithRoot, options);
}