@oscarpalmer/atoms 0.186.2 → 0.187.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array/filter.d.mts +4 -4
- package/dist/array/find.d.mts +4 -4
- package/dist/array/first.d.mts +4 -4
- package/dist/array/get.d.mts +27 -1
- package/dist/array/get.mjs +5 -3
- package/dist/array/group-by.d.mts +6 -6
- package/dist/array/last.d.mts +4 -4
- package/dist/array/match.d.mts +7 -6
- package/dist/array/move.d.mts +7 -7
- package/dist/array/move.mjs +1 -1
- package/dist/array/select.d.mts +1 -0
- package/dist/array/slice.d.mts +3 -3
- package/dist/array/sort.d.mts +10 -7
- package/dist/array/sort.mjs +4 -3
- package/dist/array/swap.d.mts +1 -1
- package/dist/array/swap.mjs +1 -1
- package/dist/array/to-map.d.mts +32 -32
- package/dist/array/to-record.d.mts +6 -6
- package/dist/array/to-set.d.mts +6 -6
- package/dist/beacon.d.mts +6 -0
- package/dist/beacon.mjs +3 -0
- package/dist/color/index.d.mts +6 -2
- package/dist/color/index.mjs +6 -2
- package/dist/color/instance.d.mts +78 -14
- package/dist/color/instance.mjs +78 -14
- package/dist/color/misc/get.d.mts +39 -11
- package/dist/color/misc/get.mjs +39 -11
- package/dist/color/misc/is.d.mts +26 -11
- package/dist/color/misc/is.mjs +26 -11
- package/dist/color/misc/state.mjs +1 -1
- package/dist/color/models.d.mts +7 -4
- package/dist/color/space/hex.d.mts +15 -6
- package/dist/color/space/hex.mjs +15 -6
- package/dist/color/space/hsl.d.mts +10 -4
- package/dist/color/space/hsl.mjs +10 -4
- package/dist/color/space/rgb.d.mts +21 -12
- package/dist/color/space/rgb.mjs +21 -12
- package/dist/function/assert.d.mts +20 -14
- package/dist/function/assert.mjs +19 -13
- package/dist/function/limit.d.mts +8 -6
- package/dist/function/limit.mjs +8 -6
- package/dist/function/memoize.d.mts +15 -4
- package/dist/function/memoize.mjs +18 -4
- package/dist/function/once.d.mts +5 -3
- package/dist/function/once.mjs +5 -3
- package/dist/function/retry.d.mts +5 -2
- package/dist/function/retry.mjs +3 -1
- package/dist/function/work.d.mts +146 -98
- package/dist/{kalas.d.mts → herald.d.mts} +17 -8
- package/dist/{kalas.mjs → herald.mjs} +22 -13
- package/dist/index.d.mts +1464 -708
- package/dist/index.mjs +677 -324
- package/dist/internal/array/index-of.d.mts +4 -4
- package/dist/internal/array/insert.mjs +1 -1
- package/dist/internal/array/shuffle.d.mts +1 -0
- package/dist/internal/array/shuffle.mjs +2 -1
- package/dist/internal/is.d.mts +26 -8
- package/dist/internal/is.mjs +26 -8
- package/dist/internal/math/aggregate.d.mts +9 -9
- package/dist/internal/number.d.mts +13 -2
- package/dist/internal/number.mjs +13 -2
- package/dist/internal/random.d.mts +4 -2
- package/dist/internal/random.mjs +7 -5
- package/dist/internal/result.d.mts +17 -11
- package/dist/internal/result.mjs +10 -14
- package/dist/internal/string.d.mts +16 -1
- package/dist/internal/string.mjs +24 -1
- package/dist/internal/value/compare.d.mts +5 -2
- package/dist/internal/value/compare.mjs +5 -2
- package/dist/internal/value/equal.d.mts +12 -3
- package/dist/internal/value/equal.mjs +7 -4
- package/dist/internal/value/get.d.mts +17 -17
- package/dist/internal/value/has.d.mts +55 -11
- package/dist/internal/value/set.d.mts +24 -0
- package/dist/is.d.mts +12 -0
- package/dist/is.mjs +16 -1
- package/dist/logger.d.mts +7 -6
- package/dist/logger.mjs +6 -5
- package/dist/math.d.mts +36 -18
- package/dist/math.mjs +6 -3
- package/dist/models.d.mts +3 -3
- package/dist/promise/delay.d.mts +2 -0
- package/dist/promise/helpers.d.mts +6 -4
- package/dist/promise/helpers.mjs +6 -4
- package/dist/promise/index.d.mts +49 -38
- package/dist/promise/misc.d.mts +10 -7
- package/dist/promise/misc.mjs +4 -3
- package/dist/promise/models.d.mts +18 -19
- package/dist/promise/models.mjs +4 -3
- package/dist/promise/timed.d.mts +8 -6
- package/dist/query.d.mts +2 -0
- package/dist/query.mjs +2 -0
- package/dist/queue.d.mts +15 -4
- package/dist/queue.mjs +10 -3
- package/dist/random.d.mts +9 -2
- package/dist/random.mjs +7 -2
- package/dist/result/index.d.mts +14 -8
- package/dist/result/match.d.mts +18 -10
- package/dist/result/misc.d.mts +14 -7
- package/dist/result/misc.mjs +4 -2
- package/dist/result/models.d.mts +2 -0
- package/dist/result/work/flow.d.mts +86 -62
- package/dist/result/work/pipe.d.mts +55 -33
- package/dist/sized/map.d.mts +19 -16
- package/dist/sized/map.mjs +4 -4
- package/dist/sized/set.d.mts +20 -16
- package/dist/sized/set.mjs +8 -7
- package/dist/string/case.d.mts +8 -0
- package/dist/string/case.mjs +8 -0
- package/dist/string/fuzzy.d.mts +19 -5
- package/dist/string/fuzzy.mjs +7 -0
- package/dist/string/index.d.mts +17 -2
- package/dist/string/index.mjs +7 -9
- package/dist/string/match.d.mts +3 -0
- package/dist/string/match.mjs +3 -0
- package/dist/string/normalize.d.mts +6 -2
- package/dist/string/normalize.mjs +5 -2
- package/dist/string/template.d.mts +38 -9
- package/dist/string/template.mjs +19 -19
- package/dist/value/clone.d.mts +25 -4
- package/dist/value/clone.mjs +36 -22
- package/dist/value/collection.d.mts +9 -6
- package/dist/value/collection.mjs +3 -2
- package/dist/value/diff.d.mts +3 -1
- package/dist/value/diff.mjs +1 -0
- package/dist/value/freeze.d.mts +118 -9
- package/dist/value/freeze.mjs +56 -14
- package/dist/value/index.d.mts +1 -2
- package/dist/value/index.mjs +1 -2
- package/dist/value/merge.d.mts +11 -5
- package/dist/value/merge.mjs +2 -2
- package/dist/value/omit.d.mts +1 -0
- package/dist/value/omit.mjs +1 -0
- package/dist/value/pick.d.mts +1 -0
- package/dist/value/pick.mjs +1 -0
- package/dist/value/shake.d.mts +1 -0
- package/dist/value/shake.mjs +1 -0
- package/dist/value/smush.d.mts +1 -0
- package/dist/value/smush.mjs +1 -0
- package/dist/value/transform.d.mts +7 -3
- package/dist/value/unsmush.d.mts +1 -0
- package/dist/value/unsmush.mjs +1 -0
- package/package.json +11 -7
- package/src/array/filter.ts +4 -4
- package/src/array/find.ts +4 -4
- package/src/array/first.ts +4 -4
- package/src/array/get.ts +40 -5
- package/src/array/group-by.ts +6 -6
- package/src/array/last.ts +4 -4
- package/src/array/match.ts +7 -6
- package/src/array/move.ts +7 -7
- package/src/array/select.ts +1 -0
- package/src/array/slice.ts +3 -3
- package/src/array/sort.ts +15 -10
- package/src/array/swap.ts +1 -1
- package/src/array/to-map.ts +32 -32
- package/src/array/to-record.ts +6 -6
- package/src/array/to-set.ts +6 -6
- package/src/beacon.ts +6 -0
- package/src/color/index.ts +6 -2
- package/src/color/instance.ts +78 -14
- package/src/color/misc/get.ts +39 -11
- package/src/color/misc/is.ts +26 -11
- package/src/color/misc/state.ts +1 -1
- package/src/color/models.ts +7 -4
- package/src/color/space/hex.ts +15 -6
- package/src/color/space/hsl.ts +10 -4
- package/src/color/space/rgb.ts +21 -12
- package/src/function/assert.ts +20 -14
- package/src/function/limit.ts +8 -6
- package/src/function/memoize.ts +24 -5
- package/src/function/once.ts +5 -3
- package/src/function/retry.ts +7 -3
- package/src/function/work.ts +146 -98
- package/src/{kalas.ts → herald.ts} +23 -14
- package/src/index.ts +3 -2
- package/src/internal/array/index-of.ts +4 -4
- package/src/internal/array/insert.ts +1 -1
- package/src/internal/array/shuffle.ts +2 -1
- package/src/internal/is.ts +26 -8
- package/src/internal/math/aggregate.ts +9 -9
- package/src/internal/number.ts +13 -2
- package/src/internal/random.ts +10 -4
- package/src/internal/result.ts +30 -29
- package/src/internal/string.ts +28 -0
- package/src/internal/value/compare.ts +5 -2
- package/src/internal/value/equal.ts +13 -4
- package/src/internal/value/get.ts +17 -17
- package/src/internal/value/has.ts +54 -11
- package/src/internal/value/set.ts +24 -0
- package/src/is.ts +15 -1
- package/src/logger.ts +8 -7
- package/src/math.ts +36 -18
- package/src/models.ts +3 -3
- package/src/promise/delay.ts +2 -0
- package/src/promise/helpers.ts +6 -4
- package/src/promise/index.ts +49 -38
- package/src/promise/misc.ts +10 -7
- package/src/promise/models.ts +18 -19
- package/src/promise/timed.ts +8 -6
- package/src/query.ts +2 -0
- package/src/queue.ts +15 -4
- package/src/random.ts +8 -1
- package/src/result/index.ts +14 -8
- package/src/result/match.ts +18 -10
- package/src/result/misc.ts +17 -9
- package/src/result/models.ts +2 -0
- package/src/result/work/flow.ts +86 -62
- package/src/result/work/pipe.ts +55 -33
- package/src/sized/map.ts +20 -17
- package/src/sized/set.ts +21 -17
- package/src/string/case.ts +8 -0
- package/src/string/fuzzy.ts +19 -5
- package/src/string/index.ts +18 -15
- package/src/string/match.ts +3 -0
- package/src/string/normalize.ts +6 -2
- package/src/string/template.ts +69 -18
- package/src/value/clone.ts +51 -26
- package/src/value/collection.ts +9 -6
- package/src/value/diff.ts +3 -1
- package/src/value/freeze.ts +239 -25
- package/src/value/index.ts +0 -1
- package/src/value/merge.ts +11 -5
- package/src/value/omit.ts +1 -0
- package/src/value/pick.ts +1 -0
- package/src/value/shake.ts +1 -0
- package/src/value/smush.ts +1 -0
- package/src/value/transform.ts +7 -3
- package/src/value/unsmush.ts +1 -0
package/src/value/freeze.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {noop} from '../function';
|
|
1
2
|
import {isPlainObject} from '../is';
|
|
2
3
|
import type {ArrayOrPlainObject, GenericCallback, PlainObject} from '../models';
|
|
3
4
|
|
|
@@ -6,51 +7,180 @@ import type {ArrayOrPlainObject, GenericCallback, PlainObject} from '../models';
|
|
|
6
7
|
/**
|
|
7
8
|
* A frozen value with readonly properties _(going as deep as possible)_
|
|
8
9
|
*/
|
|
9
|
-
export type Frozen<Value
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
export type Frozen<Value> = Value extends unknown[]
|
|
11
|
+
? Readonly<Value>
|
|
12
|
+
: Value extends PlainObject
|
|
13
|
+
? {
|
|
14
|
+
readonly [Key in keyof Value]: Frozen<Value[Key]>;
|
|
15
|
+
}
|
|
16
|
+
: Value extends Map<infer Key, infer Value>
|
|
17
|
+
? ReadonlyMap<Key, Frozen<Value>>
|
|
18
|
+
: Value extends Set<infer Item>
|
|
19
|
+
? ReadonlySet<Frozen<Item>>
|
|
20
|
+
: Readonly<Value>;
|
|
14
21
|
|
|
15
22
|
// #endregion
|
|
16
23
|
|
|
17
24
|
// #region Functions
|
|
18
25
|
|
|
26
|
+
function fakeDelete(): boolean {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
/**
|
|
20
|
-
* Freeze an array and
|
|
31
|
+
* Freeze an array and its indiced values, but not any nested values
|
|
32
|
+
*
|
|
33
|
+
* _Available as `flatFreeze` and `freeze.flat`_
|
|
34
|
+
*
|
|
35
|
+
* @param array Array to freeze
|
|
36
|
+
* @returns Frozen array
|
|
37
|
+
*/
|
|
38
|
+
export function flatFreeze<Item>(array: Item[]): ReadonlyArray<Item>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Freeze a map and its values, but not any nested values
|
|
42
|
+
*
|
|
43
|
+
* _Available as `flatFreeze` and `freeze.flat`_
|
|
44
|
+
*
|
|
45
|
+
* @param map Map to freeze
|
|
46
|
+
* @returns Frozen map
|
|
47
|
+
*/
|
|
48
|
+
export function flatFreeze<Key, Value>(map: Map<Key, Value>): ReadonlyMap<Key, Value>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Freeze an object and its properties, but not any nested values
|
|
52
|
+
*
|
|
53
|
+
* _Available as `flatFreeze` and `freeze.flat`_
|
|
54
|
+
*
|
|
55
|
+
* @param object Object to freeze
|
|
56
|
+
* @returns Frozen object
|
|
57
|
+
*/
|
|
58
|
+
export function flatFreeze<Value extends PlainObject>(object: Value): Readonly<Value>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Freeze a set and its values, but not any nested values
|
|
62
|
+
*
|
|
63
|
+
* _Available as `flatFreeze` and `freeze.flat`_
|
|
64
|
+
*
|
|
65
|
+
* @param set Set to freeze
|
|
66
|
+
* @returns Frozen set
|
|
67
|
+
*/
|
|
68
|
+
export function flatFreeze<Item>(set: Set<Item>): ReadonlySet<Item>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Freeze any value, if possible
|
|
72
|
+
*
|
|
73
|
+
* _(Only arrays, maps, plain objects, and sets are freezable)_
|
|
74
|
+
*
|
|
75
|
+
* _Available as `flatFreeze` and `freeze.flat`_
|
|
76
|
+
*
|
|
77
|
+
* @param value Value to freeze
|
|
78
|
+
* @returns Frozen value
|
|
79
|
+
*/
|
|
80
|
+
export function flatFreeze<Value>(value: Value): Value;
|
|
81
|
+
|
|
82
|
+
export function flatFreeze(value: unknown): unknown {
|
|
83
|
+
return freezeValue(value, new WeakSet(), true);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Freeze an array and its values _(but not any nested values)_, preventing modifications
|
|
88
|
+
*
|
|
89
|
+
* @param array Array to freeze
|
|
90
|
+
* @param flat Freeze _only_ the array, not any nested values
|
|
91
|
+
* @returns Frozen array
|
|
92
|
+
*/
|
|
93
|
+
export function freeze<Item>(array: Item[], flat: true): ReadonlyArray<Item>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Freeze a map and its values _(but not any nested values)_, preventing modifications
|
|
97
|
+
*
|
|
98
|
+
* @param map Map to freeze
|
|
99
|
+
* @param flat Freeze _only_ the map, not any nested values
|
|
100
|
+
* @returns Frozen map
|
|
101
|
+
*/
|
|
102
|
+
export function freeze<Key, Value>(map: Map<Key, Value>, flat: true): ReadonlyMap<Key, Value>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Freeze an object and its properties _(but not any nested values)_, preventing modifications
|
|
106
|
+
*
|
|
107
|
+
* @param object Object to freeze
|
|
108
|
+
* @param flat Freeze _only_ the object, not any nested values
|
|
109
|
+
* @returns Frozen object
|
|
110
|
+
*/
|
|
111
|
+
export function freeze<Value extends PlainObject>(object: Value, flat: true): Readonly<Value>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Freeze a set and its values _(but not any nested values)_, preventing modifications
|
|
115
|
+
*
|
|
116
|
+
* @param set Set to freeze
|
|
117
|
+
* @param flat Freeze _only_ the set, not any nested values
|
|
118
|
+
* @returns Frozen set
|
|
119
|
+
*/
|
|
120
|
+
export function freeze<Item>(set: Set<Item>, flat: true): ReadonlySet<Item>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Freeze an array and its values recursively, preventing modifications
|
|
124
|
+
*
|
|
125
|
+
* _(If you only want to freeze the array itself, but not any nested values, use `freeze(array, true)`, `freeze.flat(array)`, or `flatFreeze(array)` instead)_
|
|
126
|
+
*
|
|
21
127
|
* @param array Array to freeze
|
|
22
128
|
* @returns Frozen array
|
|
23
129
|
*/
|
|
24
130
|
export function freeze<Item>(array: Item[]): Frozen<Item[]>;
|
|
25
131
|
|
|
26
132
|
/**
|
|
27
|
-
* Freeze a
|
|
28
|
-
*
|
|
29
|
-
*
|
|
133
|
+
* Freeze a map and its values recursively, preventing modifications
|
|
134
|
+
*
|
|
135
|
+
* _(If you only want to freeze the map itself, but not any nested values, use `freeze(map, true)`, `freeze.flat(map)`, or `flatFreeze(map)` instead)_
|
|
136
|
+
*
|
|
137
|
+
* @param map Map to freeze
|
|
138
|
+
* @returns Frozen map
|
|
30
139
|
*/
|
|
31
|
-
export function freeze<
|
|
140
|
+
export function freeze<Key, Value>(map: Map<Key, Value>): Frozen<Map<Key, Value>>;
|
|
32
141
|
|
|
33
142
|
/**
|
|
34
143
|
* Freeze an object and all its properties recursively
|
|
144
|
+
*
|
|
145
|
+
* _(If you only want to freeze the object itself, but not any nested values, use `freeze(object, true)`, `freeze.flat(object)`, or `flatFreeze(object)` instead)_
|
|
146
|
+
*
|
|
35
147
|
* @param object Object to freeze
|
|
36
148
|
* @returns Frozen object
|
|
37
149
|
*/
|
|
38
150
|
export function freeze<Value extends PlainObject>(object: Value): Frozen<Value>;
|
|
39
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Freeze a set and its values recursively
|
|
154
|
+
*
|
|
155
|
+
* _(If you only want to freeze the set itself, but not any nested values, use `freeze(set, true)`, `freeze.flat(set)`, or `flatFreeze(set)` instead)_
|
|
156
|
+
*
|
|
157
|
+
* @param set Set to freeze
|
|
158
|
+
* @returns Frozen set
|
|
159
|
+
*/
|
|
160
|
+
export function freeze<Item>(set: Set<Item>): Frozen<Set<Item>>;
|
|
161
|
+
|
|
40
162
|
/**
|
|
41
163
|
* Freeze any value, if possible
|
|
42
164
|
*
|
|
43
|
-
* _(Only arrays,
|
|
165
|
+
* _(Only arrays, maps, plain objects, and sets are freezable)_
|
|
166
|
+
*
|
|
44
167
|
* @param value Value to freeze
|
|
45
168
|
* @returns Frozen value
|
|
46
169
|
*/
|
|
47
170
|
export function freeze<Value>(value: Value): Value;
|
|
48
171
|
|
|
49
|
-
export function freeze(value: unknown): unknown {
|
|
50
|
-
return freezeValue(value, new WeakSet());
|
|
172
|
+
export function freeze(value: unknown, flat?: unknown): unknown {
|
|
173
|
+
return freezeValue(value, new WeakSet(), flat === true);
|
|
51
174
|
}
|
|
52
175
|
|
|
53
|
-
|
|
176
|
+
freeze.flat = flatFreeze;
|
|
177
|
+
freeze.is = isFrozen;
|
|
178
|
+
|
|
179
|
+
function freezeArray(array: unknown[], references: WeakSet<any>, flat: boolean): Frozen<unknown[]> {
|
|
180
|
+
if (flat) {
|
|
181
|
+
return Object.freeze(array) as Frozen<unknown[]>;
|
|
182
|
+
}
|
|
183
|
+
|
|
54
184
|
references.add(array);
|
|
55
185
|
|
|
56
186
|
const {length} = array;
|
|
@@ -59,20 +189,50 @@ function freezeArray(array: unknown[], references: WeakSet<any>): Frozen<unknown
|
|
|
59
189
|
const value = array[index];
|
|
60
190
|
|
|
61
191
|
if (!references.has(value)) {
|
|
62
|
-
array[index] = freezeValue(array[index], references);
|
|
192
|
+
array[index] = freezeValue(array[index], references, false);
|
|
63
193
|
}
|
|
64
194
|
}
|
|
65
195
|
|
|
66
196
|
return Object.freeze(array) as Frozen<unknown[]>;
|
|
67
197
|
}
|
|
68
198
|
|
|
69
|
-
function
|
|
70
|
-
|
|
199
|
+
function freezeMap(
|
|
200
|
+
map: Map<unknown, unknown>,
|
|
201
|
+
references: WeakSet<any>,
|
|
202
|
+
flat: boolean,
|
|
203
|
+
): ReadonlyMap<unknown, unknown> {
|
|
204
|
+
frozenValues.add(map);
|
|
205
|
+
|
|
206
|
+
map.clear = noop;
|
|
207
|
+
map.delete = fakeDelete;
|
|
208
|
+
map.set = () => map;
|
|
209
|
+
|
|
210
|
+
if (flat) {
|
|
211
|
+
return map as ReadonlyMap<unknown, unknown>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
references.add(map);
|
|
71
215
|
|
|
72
|
-
|
|
216
|
+
const entries = map.entries();
|
|
217
|
+
|
|
218
|
+
for (const [key, value] of entries) {
|
|
219
|
+
if (!references.has(value)) {
|
|
220
|
+
map.set(key, freezeValue(value, references, false));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return map as ReadonlyMap<unknown, unknown>;
|
|
73
225
|
}
|
|
74
226
|
|
|
75
|
-
function freezeObject(
|
|
227
|
+
function freezeObject(
|
|
228
|
+
object: PlainObject,
|
|
229
|
+
references: WeakSet<any>,
|
|
230
|
+
flat: boolean,
|
|
231
|
+
): Frozen<PlainObject> {
|
|
232
|
+
if (flat) {
|
|
233
|
+
return Object.freeze(object) as Frozen<PlainObject>;
|
|
234
|
+
}
|
|
235
|
+
|
|
76
236
|
references.add(object);
|
|
77
237
|
|
|
78
238
|
const keys = Object.keys(object);
|
|
@@ -82,27 +242,81 @@ function freezeObject(object: PlainObject, references: WeakSet<any>): Frozen<Pla
|
|
|
82
242
|
const key = keys[index];
|
|
83
243
|
|
|
84
244
|
if (!references.has(object[key])) {
|
|
85
|
-
object[key] = freezeValue(object[key], references);
|
|
245
|
+
object[key] = freezeValue(object[key], references, false);
|
|
86
246
|
}
|
|
87
247
|
}
|
|
88
248
|
|
|
89
249
|
return Object.freeze(object) as Frozen<PlainObject>;
|
|
90
250
|
}
|
|
91
251
|
|
|
92
|
-
function
|
|
252
|
+
function freezeSet(
|
|
253
|
+
set: Set<unknown>,
|
|
254
|
+
references: WeakSet<any>,
|
|
255
|
+
flat: boolean,
|
|
256
|
+
): ReadonlySet<unknown> {
|
|
257
|
+
frozenValues.add(set);
|
|
258
|
+
|
|
259
|
+
set.clear = noop;
|
|
260
|
+
set.delete = fakeDelete;
|
|
261
|
+
set.add = () => set;
|
|
262
|
+
|
|
263
|
+
if (flat) {
|
|
264
|
+
return set as ReadonlySet<unknown>;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
references.add(set);
|
|
268
|
+
|
|
269
|
+
const values = set.values();
|
|
270
|
+
|
|
271
|
+
for (const value of values) {
|
|
272
|
+
if (!references.has(value)) {
|
|
273
|
+
set.add(freezeValue(value, references, false));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return set as ReadonlySet<unknown>;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function freezeValue(value: unknown, references: WeakSet<any>, flat: boolean): unknown {
|
|
93
281
|
switch (true) {
|
|
94
|
-
case
|
|
95
|
-
return
|
|
282
|
+
case isFrozen(value):
|
|
283
|
+
return value;
|
|
284
|
+
|
|
285
|
+
case value instanceof Map:
|
|
286
|
+
return freezeMap(value, references, flat);
|
|
287
|
+
|
|
288
|
+
case value instanceof Set:
|
|
289
|
+
return freezeSet(value, references, flat);
|
|
96
290
|
|
|
97
291
|
case Array.isArray(value):
|
|
98
|
-
return freezeArray(value, references);
|
|
292
|
+
return freezeArray(value, references, flat);
|
|
99
293
|
|
|
100
294
|
case isPlainObject(value):
|
|
101
|
-
return freezeObject(value, references);
|
|
295
|
+
return freezeObject(value, references, flat);
|
|
102
296
|
|
|
103
297
|
default:
|
|
104
298
|
return value;
|
|
105
299
|
}
|
|
106
300
|
}
|
|
107
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Is the value frozen?
|
|
304
|
+
*
|
|
305
|
+
* @param value Value to check
|
|
306
|
+
* @returns `true` if the value is frozen, otherwise `false`
|
|
307
|
+
*/
|
|
308
|
+
export function isFrozen(value: unknown): boolean {
|
|
309
|
+
if (value instanceof Map || value instanceof Set) {
|
|
310
|
+
return frozenValues.has(value);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return typeof value === 'object' && value !== null && Object.isFrozen(value);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// #endregion
|
|
317
|
+
|
|
318
|
+
// #region Variables
|
|
319
|
+
|
|
320
|
+
const frozenValues = new WeakSet();
|
|
321
|
+
|
|
108
322
|
// #endregion
|
package/src/value/index.ts
CHANGED
package/src/value/merge.ts
CHANGED
|
@@ -8,6 +8,9 @@ import type {ArrayOrPlainObject, NestedPartial, PlainObject, UnionToIntersection
|
|
|
8
8
|
*/
|
|
9
9
|
export type AssignOptions = Omit<MergeOptions, 'assignValues'>;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* An assigner function for assigning values from one or more objects to the first one
|
|
13
|
+
*/
|
|
11
14
|
export type Assigner = {
|
|
12
15
|
/**
|
|
13
16
|
* Assign values from one or more objects to the first one
|
|
@@ -34,7 +37,7 @@ export type MergeOptions = {
|
|
|
34
37
|
* Key _(or key epxressions)_ for values that should be replaced
|
|
35
38
|
*
|
|
36
39
|
* ```ts
|
|
37
|
-
* merge([{items: [1, 2, 3]}, {items: [99]}]); // {items: [99]}
|
|
40
|
+
* merge([{items: [1, 2, 3]}, {items: [99]}]); // => {items: [99]}
|
|
38
41
|
* ```
|
|
39
42
|
*/
|
|
40
43
|
replaceableObjects?: string | RegExp | Array<string | RegExp>;
|
|
@@ -42,7 +45,7 @@ export type MergeOptions = {
|
|
|
42
45
|
* Skip nullable values when merging objects?
|
|
43
46
|
*
|
|
44
47
|
* ```ts
|
|
45
|
-
* merge({a: 1, b: 2}, {b: null, c: 3}, {d: null}); // {a: 1, b: 2, c: 3}
|
|
48
|
+
* merge({a: 1, b: 2}, {b: null, c: 3}, {d: null}); // => {a: 1, b: 2, c: 3}
|
|
46
49
|
* ```
|
|
47
50
|
*/
|
|
48
51
|
skipNullableAny?: boolean;
|
|
@@ -50,12 +53,15 @@ export type MergeOptions = {
|
|
|
50
53
|
* Skip nullable values when merging arrays?
|
|
51
54
|
*
|
|
52
55
|
* ```ts
|
|
53
|
-
* merge([1, 2, 3], [null, null, 99]); // [1, 2, 99]
|
|
56
|
+
* merge([1, 2, 3], [null, null, 99]); // => [1, 2, 99]
|
|
54
57
|
* ```
|
|
55
58
|
*/
|
|
56
59
|
skipNullableInArrays?: boolean;
|
|
57
60
|
};
|
|
58
61
|
|
|
62
|
+
/**
|
|
63
|
+
* A merger function for merging multiple arrays or objects into a single one
|
|
64
|
+
*/
|
|
59
65
|
export type Merger = {
|
|
60
66
|
/**
|
|
61
67
|
* Merge multiple arrays or objects into a single one
|
|
@@ -138,7 +144,7 @@ function getReplaceableObjects(value: unknown): ReplaceableObjectsCallback | und
|
|
|
138
144
|
/**
|
|
139
145
|
* Create an assigner with predefined options
|
|
140
146
|
*
|
|
141
|
-
*
|
|
147
|
+
* _Available as `initializeAssigner` and `assign.initialize`_
|
|
142
148
|
*
|
|
143
149
|
* @param options Assigning options
|
|
144
150
|
* @returns Assigner function
|
|
@@ -155,7 +161,7 @@ export function initializeAssigner(options?: AssignOptions): Assigner {
|
|
|
155
161
|
/**
|
|
156
162
|
* Create a merger with predefined options
|
|
157
163
|
*
|
|
158
|
-
*
|
|
164
|
+
* _Available as `initializeMerger` and `merge.initialize`_
|
|
159
165
|
*
|
|
160
166
|
* @param options Merging options
|
|
161
167
|
* @returns Merger function
|
package/src/value/omit.ts
CHANGED
package/src/value/pick.ts
CHANGED
package/src/value/shake.ts
CHANGED
package/src/value/smush.ts
CHANGED
package/src/value/transform.ts
CHANGED
|
@@ -62,7 +62,8 @@ function getTransformer<Value extends PlainObject, Key extends keyof Value>(
|
|
|
62
62
|
/**
|
|
63
63
|
* Initialize a transformer for an object with a transformer function
|
|
64
64
|
*
|
|
65
|
-
*
|
|
65
|
+
* _Available as `initializeTransformer` and `transform.initialize`_
|
|
66
|
+
*
|
|
66
67
|
* @param transform Transformer function
|
|
67
68
|
* @returns Transformer
|
|
68
69
|
*/
|
|
@@ -73,7 +74,8 @@ export function initializeTransformer<Value extends PlainObject>(
|
|
|
73
74
|
/**
|
|
74
75
|
* Initialize a transformer for an object with transformer functions
|
|
75
76
|
*
|
|
76
|
-
*
|
|
77
|
+
* _Available as `initializeTransformer` and `transform.initialize`_
|
|
78
|
+
*
|
|
77
79
|
* @param transformers Keyed transformer functions
|
|
78
80
|
* @returns Transformer
|
|
79
81
|
*/
|
|
@@ -90,7 +92,8 @@ export function initializeTransformer<Value extends PlainObject>(
|
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
/**
|
|
93
|
-
* Transform and objects properties using a transformer
|
|
95
|
+
* Transform and objects properties using a transformer function
|
|
96
|
+
*
|
|
94
97
|
* @param value Object to transform
|
|
95
98
|
* @param transform Transformer function
|
|
96
99
|
* @returns Transformed object
|
|
@@ -102,6 +105,7 @@ export function transform<Value extends PlainObject, Key extends keyof Value>(
|
|
|
102
105
|
|
|
103
106
|
/**
|
|
104
107
|
* Transform and objects properties using a transformer object
|
|
108
|
+
*
|
|
105
109
|
* @param value Object to transform
|
|
106
110
|
* @param transformers Keyed transformer functions
|
|
107
111
|
* @returns Transformed object
|
package/src/value/unsmush.ts
CHANGED