@oscarpalmer/atoms 0.196.0 → 0.197.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.mjs +1 -0
- package/dist/array/find.mjs +2 -1
- package/dist/array/first.mjs +2 -1
- package/dist/array/group-by.mjs +2 -1
- package/dist/array/last.mjs +2 -1
- package/dist/array/move.mjs +6 -2
- package/dist/array/sort.mjs +8 -3
- package/dist/array/swap.mjs +2 -1
- package/dist/array/to-map.mjs +2 -1
- package/dist/array/to-record.mjs +2 -1
- package/dist/index.d.mts +25 -10
- package/dist/index.mjs +185 -100
- package/dist/internal/array/callbacks.mjs +1 -0
- package/dist/internal/array/index-of.mjs +2 -1
- package/dist/internal/function/assert.mjs +12 -5
- package/dist/internal/math/aggregate.d.mts +2 -2
- package/dist/internal/sized/map.mjs +3 -2
- package/dist/internal/value/compare.d.mts +1 -1
- package/dist/internal/value/compare.mjs +12 -8
- package/dist/internal/value/handlers.d.mts +1 -1
- package/dist/internal/value/has.mjs +2 -1
- package/dist/math.d.mts +4 -4
- package/dist/promise/index.mjs +2 -1
- package/dist/queue.mjs +2 -1
- package/dist/sized/set.mjs +5 -6
- package/dist/string/fuzzy.mjs +2 -1
- package/dist/string/normalize.mjs +2 -1
- package/dist/string/template.mjs +2 -1
- package/dist/value/clone.d.mts +18 -3
- package/dist/value/clone.mjs +94 -54
- package/dist/value/collection.mjs +3 -1
- package/dist/value/freeze.mjs +6 -2
- package/dist/value/merge.mjs +4 -2
- package/dist/value/transform.mjs +2 -1
- package/package.json +2 -2
|
@@ -49,7 +49,7 @@ declare function max<Item>(items: Item[], callback: (item: Item, index: number,
|
|
|
49
49
|
* @param first Return only the first item with the maximum value
|
|
50
50
|
* @returns Item with the maximum value, or `undefined` if no maximum can be found
|
|
51
51
|
*/
|
|
52
|
-
declare function max<Item extends PlainObject
|
|
52
|
+
declare function max<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
|
|
53
53
|
/**
|
|
54
54
|
* Get the maximum value from a list of items
|
|
55
55
|
*
|
|
@@ -85,7 +85,7 @@ declare function max<Item>(items: Item[], callback: (item: Item, index: number,
|
|
|
85
85
|
* @param key Key to use for value
|
|
86
86
|
* @returns Items with the maximum value
|
|
87
87
|
*/
|
|
88
|
-
declare function max<Item extends PlainObject
|
|
88
|
+
declare function max<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
|
|
89
89
|
/**
|
|
90
90
|
* Get the maximum value from a list of numbers
|
|
91
91
|
*
|
|
@@ -33,11 +33,12 @@ var SizedMap = class extends Map {
|
|
|
33
33
|
* @inheritdoc
|
|
34
34
|
*/
|
|
35
35
|
get(key) {
|
|
36
|
+
let value;
|
|
36
37
|
if (super.has(key)) {
|
|
37
|
-
|
|
38
|
+
value = super.get(key);
|
|
38
39
|
this.#setValue(key, value, true);
|
|
39
|
-
return value;
|
|
40
40
|
}
|
|
41
|
+
return value;
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
43
44
|
* @inheritdoc
|
|
@@ -10,12 +10,12 @@ type Comparator<Value = any> = (first: Value, second: Value) => number;
|
|
|
10
10
|
*/
|
|
11
11
|
declare function compare(first: unknown, second: unknown): number;
|
|
12
12
|
declare namespace compare {
|
|
13
|
+
var deregister: typeof deregisterComparator;
|
|
13
14
|
var handlers: {
|
|
14
15
|
deregister(constructor: Constructor): void;
|
|
15
16
|
handle(first: unknown, second: unknown, ...parameters: unknown[]): number;
|
|
16
17
|
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
17
18
|
};
|
|
18
|
-
var deregister: typeof deregisterComparator;
|
|
19
19
|
var register: typeof registerComparator;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
@@ -32,14 +32,6 @@ function compare(first, second) {
|
|
|
32
32
|
}
|
|
33
33
|
return 0;
|
|
34
34
|
}
|
|
35
|
-
compare.handlers = getCompareHandlers(compare, {
|
|
36
|
-
callback: (first, second, compareStrings) => {
|
|
37
|
-
if (compareStrings) return getString(first).localeCompare(getString(second));
|
|
38
|
-
},
|
|
39
|
-
method: COMPARE_NAME
|
|
40
|
-
});
|
|
41
|
-
compare.deregister = deregisterComparator;
|
|
42
|
-
compare.register = registerComparator;
|
|
43
35
|
function compareNumbers(first, second) {
|
|
44
36
|
if (Object.is(first, second)) return 0;
|
|
45
37
|
if (Number.isNaN(first)) return -1;
|
|
@@ -86,5 +78,17 @@ const comparators = {
|
|
|
86
78
|
number: compareNumbers,
|
|
87
79
|
symbol: compareSymbols
|
|
88
80
|
};
|
|
81
|
+
const compareHandlers = getCompareHandlers(compare, {
|
|
82
|
+
callback: (first, second, compareStrings) => compareStrings ? getString(first).localeCompare(getString(second)) : void 0,
|
|
83
|
+
method: COMPARE_NAME
|
|
84
|
+
});
|
|
85
|
+
compare.deregister = deregisterComparator;
|
|
86
|
+
compare.handlers = compareHandlers;
|
|
87
|
+
compare.register = registerComparator;
|
|
88
|
+
Object.defineProperties(compare, {
|
|
89
|
+
deregister: { value: deregisterComparator },
|
|
90
|
+
handlers: { value: compareHandlers },
|
|
91
|
+
register: { value: registerComparator }
|
|
92
|
+
});
|
|
89
93
|
//#endregion
|
|
90
94
|
export { compare, deregisterComparator, registerComparator };
|
|
@@ -11,7 +11,7 @@ declare function getCompareHandlers<Value>(owner: GenericCallback, options: Opti
|
|
|
11
11
|
};
|
|
12
12
|
declare function getSelfHandlers(owner: GenericCallback, options: Options): {
|
|
13
13
|
deregister(constructor: Constructor): void;
|
|
14
|
-
handle(value: unknown, ...parameters: unknown[]):
|
|
14
|
+
handle(value: unknown, ...parameters: unknown[]): unknown;
|
|
15
15
|
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
16
16
|
};
|
|
17
17
|
//#endregion
|
|
@@ -3,9 +3,10 @@ import { getNestedValue } from "./misc.mjs";
|
|
|
3
3
|
function hasValue(data, path, ignoreCase) {
|
|
4
4
|
return getNestedValue(data, path, ignoreCase === true).ok;
|
|
5
5
|
}
|
|
6
|
-
hasValue.get = hasValueResult;
|
|
7
6
|
function hasValueResult(data, path, ignoreCase) {
|
|
8
7
|
return getNestedValue(data, path, ignoreCase === true);
|
|
9
8
|
}
|
|
9
|
+
hasValue.get = hasValueResult;
|
|
10
|
+
Object.defineProperty(hasValue, "get", { value: hasValueResult });
|
|
10
11
|
//#endregion
|
|
11
12
|
export { hasValue, hasValueResult };
|
package/dist/math.d.mts
CHANGED
|
@@ -95,18 +95,18 @@ declare function median(array: number[]): number;
|
|
|
95
95
|
* @param items List of items
|
|
96
96
|
* @param callback Callback to get an item's value
|
|
97
97
|
* @param first Return only the first item with the minimum value
|
|
98
|
-
* @returns
|
|
98
|
+
* @returns First item with the minimum value, or `undefined` if no minimum can be found
|
|
99
99
|
*/
|
|
100
|
-
declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item
|
|
100
|
+
declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
|
|
101
101
|
/**
|
|
102
102
|
* Get the minimum value from a list of items
|
|
103
103
|
*
|
|
104
104
|
* @param items List of items
|
|
105
105
|
* @param key Key to use for value
|
|
106
106
|
* @param first Return only the first item with the minimum value
|
|
107
|
-
* @returns
|
|
107
|
+
* @returns First item with the minimum value, or `undefined` if no minimum can be found
|
|
108
108
|
*/
|
|
109
|
-
declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item
|
|
109
|
+
declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
|
|
110
110
|
/**
|
|
111
111
|
* Get the minimum value from a list of items
|
|
112
112
|
*
|
package/dist/promise/index.mjs
CHANGED
|
@@ -66,9 +66,10 @@ async function promises(items, options) {
|
|
|
66
66
|
}));
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
promises.result = resultPromises;
|
|
70
69
|
async function resultPromises(items, signal) {
|
|
71
70
|
return promises(items, signal).then(getResultsFromPromises);
|
|
72
71
|
}
|
|
72
|
+
promises.result = resultPromises;
|
|
73
|
+
Object.defineProperty(promises, "result", { value: resultPromises });
|
|
73
74
|
//#endregion
|
|
74
75
|
export { attemptPromise, promises, resultPromises };
|
package/dist/queue.mjs
CHANGED
|
@@ -241,7 +241,6 @@ function keyedQueue(callback, options) {
|
|
|
241
241
|
function queue(callback, options) {
|
|
242
242
|
return createQueue(callback, options);
|
|
243
243
|
}
|
|
244
|
-
queue.keyed = keyedQueue;
|
|
245
244
|
function removeQueue(state, key, id) {
|
|
246
245
|
if (key == null) {
|
|
247
246
|
handleQueues(state, QUEUE_HANDLE_CLEAR);
|
|
@@ -317,5 +316,7 @@ const QUEUE_STATUS_ACTIVE = "active";
|
|
|
317
316
|
const QUEUE_STATUS_EMPTY = "empty";
|
|
318
317
|
const QUEUE_STATUS_FULL = "full";
|
|
319
318
|
const QUEUE_STATUS_PAUSED = "paused";
|
|
319
|
+
queue.keyed = keyedQueue;
|
|
320
|
+
Object.defineProperty(queue, "keyed", { value: keyedQueue });
|
|
320
321
|
//#endregion
|
|
321
322
|
export { QueueError, isKeyedQueue, isQueue, isQueueInstance, keyedQueue, queue };
|
package/dist/sized/set.mjs
CHANGED
|
@@ -45,13 +45,12 @@ var SizedSet = class extends Set {
|
|
|
45
45
|
* @returns Found value if it exists, otherwise `undefined`
|
|
46
46
|
*/
|
|
47
47
|
get(value, update) {
|
|
48
|
-
if (super.has(value))
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
return value;
|
|
48
|
+
if (!super.has(value)) return;
|
|
49
|
+
if (update ?? false) {
|
|
50
|
+
super.delete(value);
|
|
51
|
+
this.add(value);
|
|
54
52
|
}
|
|
53
|
+
return value;
|
|
55
54
|
}
|
|
56
55
|
};
|
|
57
56
|
//#endregion
|
package/dist/string/fuzzy.mjs
CHANGED
|
@@ -56,7 +56,6 @@ function fuzzy(items, configuration) {
|
|
|
56
56
|
});
|
|
57
57
|
return Object.freeze(instance);
|
|
58
58
|
}
|
|
59
|
-
fuzzy.match = fuzzyMatch;
|
|
60
59
|
/**
|
|
61
60
|
* Does the needle match the haystack in a fuzzy way?
|
|
62
61
|
*
|
|
@@ -163,5 +162,7 @@ const FUZZY_LENGTH_DIVISOR = 3;
|
|
|
163
162
|
const FUZZY_MESSAGE_ARRAY = "Fuzzy requires an array of items";
|
|
164
163
|
const FUZZY_MESSAGE_HANDLER = "Fuzzy requires a key or function to stringify items";
|
|
165
164
|
const FUZZY_PROXIMITY_THRESHOLD = 5;
|
|
165
|
+
fuzzy.match = fuzzyMatch;
|
|
166
|
+
Object.defineProperty(fuzzy, "match", { value: fuzzyMatch });
|
|
166
167
|
//#endregion
|
|
167
168
|
export { fuzzy, fuzzyMatch };
|
|
@@ -51,7 +51,6 @@ function initializeNormalizer(options) {
|
|
|
51
51
|
function normalize(value, options) {
|
|
52
52
|
return normalizeString(value, getNormalizeOptions(options));
|
|
53
53
|
}
|
|
54
|
-
normalize.initialize = initializeNormalizer;
|
|
55
54
|
function normalizeString(value, options) {
|
|
56
55
|
if (typeof value !== "string") return "";
|
|
57
56
|
let result = value;
|
|
@@ -103,5 +102,7 @@ const NORMALIZE_SPECIAL_REPLACEMENT = "";
|
|
|
103
102
|
const NORMALIZE_WHITESPACE_PATTERN = /\s+/g;
|
|
104
103
|
const NORMALIZE_WHITESPACE_REPLACEMENT = " ";
|
|
105
104
|
let deburrMemoizer;
|
|
105
|
+
normalize.initialize = initializeNormalizer;
|
|
106
|
+
Object.defineProperty(normalize, "initialize", { value: initializeNormalizer });
|
|
106
107
|
//#endregion
|
|
107
108
|
export { deburr, initializeNormalizer, normalize };
|
package/dist/string/template.mjs
CHANGED
|
@@ -45,7 +45,8 @@ function template(value, ...parameters) {
|
|
|
45
45
|
const { ignoreCase, pattern } = getTemplateOptions(parameters[1]);
|
|
46
46
|
return handleTemplate(value, pattern, ignoreCase, parameters[0]);
|
|
47
47
|
}
|
|
48
|
-
template.initialize = initializeTemplater;
|
|
49
48
|
const TEMPLATE_EXPRESSION_VARIABLE = /{{([\s\S]+?)}}/g;
|
|
49
|
+
template.initialize = initializeTemplater;
|
|
50
|
+
Object.defineProperty(template, "initialize", { value: initializeTemplater });
|
|
50
51
|
//#endregion
|
|
51
52
|
export { initializeTemplater, template };
|
package/dist/value/clone.d.mts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { Constructor, GenericCallback } from "../models.mjs";
|
|
2
2
|
//#region src/value/clone.d.ts
|
|
3
|
+
type CloneOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Copy functions instead of returning `undefined`? _(defaults to `false`)_
|
|
6
|
+
*/
|
|
7
|
+
copyFunctions?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Copy symbols instead of returning `Symbol(description)`? _(defaults to `false`)_
|
|
10
|
+
*/
|
|
11
|
+
copySymbols?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Clone only the value itself, without cloning nested values? _(defaults to `false`)_
|
|
14
|
+
*/
|
|
15
|
+
flat?: boolean;
|
|
16
|
+
};
|
|
3
17
|
/**
|
|
4
18
|
* Clone any kind of value _(shallowly)_
|
|
5
19
|
*
|
|
@@ -12,13 +26,14 @@ declare function clone<Value>(value: Value, flat: true): Value;
|
|
|
12
26
|
* Clone any kind of value _(deeply, if needed)_
|
|
13
27
|
*
|
|
14
28
|
* @param value Value to clone
|
|
29
|
+
* @param options Clone options
|
|
15
30
|
* @returns Cloned value
|
|
16
31
|
*/
|
|
17
|
-
declare function clone<Value>(value: Value): Value;
|
|
32
|
+
declare function clone<Value>(value: Value, options?: CloneOptions): Value;
|
|
18
33
|
declare namespace clone {
|
|
19
34
|
var handlers: {
|
|
20
35
|
deregister(constructor: Constructor): void;
|
|
21
|
-
handle(value: unknown, ...parameters: unknown[]):
|
|
36
|
+
handle(value: unknown, ...parameters: unknown[]): unknown;
|
|
22
37
|
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
23
38
|
};
|
|
24
39
|
var deregister: typeof deregisterCloner;
|
|
@@ -52,4 +67,4 @@ declare function deregisterCloner<Instance>(constructor: Constructor<Instance>):
|
|
|
52
67
|
*/
|
|
53
68
|
declare function registerCloner<Instance>(constructor: Constructor<Instance>, handler?: string | ((value: Instance) => Instance)): void;
|
|
54
69
|
//#endregion
|
|
55
|
-
export { clone, copy, deregisterCloner, registerCloner };
|
|
70
|
+
export { CloneOptions, clone, copy, deregisterCloner, registerCloner };
|
package/dist/value/clone.mjs
CHANGED
|
@@ -2,8 +2,17 @@ import { isArrayOrPlainObject, isTypedArray } from "../internal/is.mjs";
|
|
|
2
2
|
import { getSelfHandlers } from "../internal/value/handlers.mjs";
|
|
3
3
|
//#region src/value/clone.ts
|
|
4
4
|
const CLONE_NAME = "clone";
|
|
5
|
-
function clone(value,
|
|
6
|
-
|
|
5
|
+
function clone(value, options) {
|
|
6
|
+
switch (true) {
|
|
7
|
+
case value === null:
|
|
8
|
+
case value === void 0:
|
|
9
|
+
case typeof value === "bigint":
|
|
10
|
+
case typeof value === "boolean":
|
|
11
|
+
case typeof value === "number":
|
|
12
|
+
case typeof value === "string": return value;
|
|
13
|
+
case value instanceof Date: return new Date(value.getTime());
|
|
14
|
+
default: return cloneAny(value, getCloneParameters(options), 0);
|
|
15
|
+
}
|
|
7
16
|
}
|
|
8
17
|
clone.handlers = getSelfHandlers(clone, {
|
|
9
18
|
callback: tryStructuredClone,
|
|
@@ -11,90 +20,92 @@ clone.handlers = getSelfHandlers(clone, {
|
|
|
11
20
|
});
|
|
12
21
|
clone.deregister = deregisterCloner;
|
|
13
22
|
clone.register = registerCloner;
|
|
14
|
-
function
|
|
15
|
-
|
|
23
|
+
function cloneAny(value, parameters, depth) {
|
|
24
|
+
switch (true) {
|
|
25
|
+
case value == null:
|
|
26
|
+
case typeof value === "bigint":
|
|
27
|
+
case typeof value === "boolean":
|
|
28
|
+
case typeof value === "number":
|
|
29
|
+
case typeof value === "string": return value;
|
|
30
|
+
case value instanceof Date: return new Date(value.getTime());
|
|
31
|
+
case typeof value === "function": return parameters.options?.copyFunctions ? value : void 0;
|
|
32
|
+
case typeof value === "symbol": return parameters.options?.copySymbols ? value : Symbol(value.description);
|
|
33
|
+
case parameters.references.has(value): return parameters.references.get(value);
|
|
34
|
+
case value instanceof ArrayBuffer: return cloneArrayBuffer(value, parameters, depth);
|
|
35
|
+
case value instanceof DataView: return cloneDataView(value, parameters, depth);
|
|
36
|
+
case value instanceof RegExp: return cloneRegularExpression(value, parameters, depth);
|
|
37
|
+
case value instanceof Map: return cloneMap(value, parameters, depth);
|
|
38
|
+
case typeof Node !== "undefined" && value instanceof Node: return cloneNode(value, parameters, depth);
|
|
39
|
+
case value instanceof Set: return cloneSet(value, parameters, depth);
|
|
40
|
+
case isArrayOrPlainObject(value): return cloneObject(value, parameters, depth);
|
|
41
|
+
case isTypedArray(value): return cloneTypedArray(value, parameters, depth);
|
|
42
|
+
default: return clone.handlers.handle(value, parameters, depth);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function cloneArrayBuffer(value, parameters, depth) {
|
|
46
|
+
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
16
47
|
const cloned = new ArrayBuffer(value.byteLength);
|
|
17
48
|
new Uint8Array(cloned).set(new Uint8Array(value));
|
|
18
|
-
references
|
|
49
|
+
parameters.references.set(value, cloned);
|
|
19
50
|
return cloned;
|
|
20
51
|
}
|
|
21
|
-
function cloneDataView(value,
|
|
52
|
+
function cloneDataView(value, parameters, depth) {
|
|
22
53
|
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
23
|
-
const buffer = cloneArrayBuffer(value.buffer);
|
|
54
|
+
const buffer = cloneArrayBuffer(value.buffer, parameters, depth);
|
|
24
55
|
const cloned = new DataView(buffer, value.byteOffset, value.byteLength);
|
|
25
|
-
references.set(value, cloned);
|
|
56
|
+
parameters.references.set(value, cloned);
|
|
26
57
|
return cloned;
|
|
27
58
|
}
|
|
28
|
-
function cloneMap(map,
|
|
59
|
+
function cloneMap(map, parameters, depth) {
|
|
29
60
|
if (depth >= CLONE_MAX_DEPTH) return map;
|
|
61
|
+
const flat = parameters.options?.flat;
|
|
30
62
|
const cloned = /* @__PURE__ */ new Map();
|
|
31
63
|
const entries = map.entries();
|
|
32
|
-
for (const entry of entries) cloned.set(flat ? entry[0] :
|
|
33
|
-
references.set(map, cloned);
|
|
64
|
+
for (const entry of entries) cloned.set(flat ? entry[0] : cloneAny(entry[0], parameters, depth + 1), flat ? entry[1] : cloneAny(entry[1], parameters, depth + 1));
|
|
65
|
+
parameters.references.set(map, cloned);
|
|
34
66
|
return cloned;
|
|
35
67
|
}
|
|
36
|
-
function cloneNode(node,
|
|
68
|
+
function cloneNode(node, parameters, depth) {
|
|
37
69
|
if (depth >= CLONE_MAX_DEPTH) return node;
|
|
38
70
|
const cloned = node.cloneNode(true);
|
|
39
|
-
references.set(node, cloned);
|
|
71
|
+
parameters.references.set(node, cloned);
|
|
40
72
|
return cloned;
|
|
41
73
|
}
|
|
42
|
-
function cloneObject(value,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
74
|
+
function cloneObject(value, parameters, depth) {
|
|
75
|
+
const isArray = Array.isArray(value);
|
|
76
|
+
if (parameters.options?.flat || depth >= CLONE_MAX_DEPTH) return isArray ? value.slice() : { ...value };
|
|
77
|
+
const cloned = isArray ? [] : {};
|
|
78
|
+
const keys = isArray ? void 0 : Object.keys(value);
|
|
79
|
+
const { length } = keys ?? value;
|
|
47
80
|
for (let index = 0; index < length; index += 1) {
|
|
48
|
-
const key = keys[index];
|
|
49
|
-
cloned[key] =
|
|
81
|
+
const key = keys?.[index] ?? index;
|
|
82
|
+
cloned[key] = cloneAny(value[key], parameters, depth + 1);
|
|
50
83
|
}
|
|
51
|
-
references.set(value, cloned);
|
|
84
|
+
parameters.references.set(value, cloned);
|
|
52
85
|
return cloned;
|
|
53
86
|
}
|
|
54
|
-
function cloneRegularExpression(value,
|
|
87
|
+
function cloneRegularExpression(value, parameters, depth) {
|
|
55
88
|
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
56
89
|
const cloned = new RegExp(value.source, value.flags);
|
|
57
90
|
cloned.lastIndex = value.lastIndex;
|
|
58
|
-
references.set(value, cloned);
|
|
91
|
+
parameters.references.set(value, cloned);
|
|
59
92
|
return cloned;
|
|
60
93
|
}
|
|
61
|
-
function cloneSet(set,
|
|
94
|
+
function cloneSet(set, parameters, depth) {
|
|
62
95
|
if (depth >= CLONE_MAX_DEPTH) return set;
|
|
63
96
|
const cloned = /* @__PURE__ */ new Set();
|
|
64
97
|
const values = [...set.values()];
|
|
65
98
|
const { length } = values;
|
|
66
|
-
for (let index = 0; index < length; index += 1) cloned.add(flat ? values[index] :
|
|
67
|
-
references.set(set, cloned);
|
|
99
|
+
for (let index = 0; index < length; index += 1) cloned.add(parameters.options?.flat ? values[index] : cloneAny(values[index], parameters, depth + 1));
|
|
100
|
+
parameters.references.set(set, cloned);
|
|
68
101
|
return cloned;
|
|
69
102
|
}
|
|
70
|
-
function cloneTypedArray(value,
|
|
103
|
+
function cloneTypedArray(value, parameters, depth) {
|
|
71
104
|
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
72
105
|
const cloned = new value.constructor(value);
|
|
73
|
-
references.set(value, cloned);
|
|
106
|
+
parameters.references.set(value, cloned);
|
|
74
107
|
return cloned;
|
|
75
108
|
}
|
|
76
|
-
function cloneValue(value, depth, references, flat) {
|
|
77
|
-
switch (true) {
|
|
78
|
-
case value == null:
|
|
79
|
-
case typeof value === "bigint":
|
|
80
|
-
case typeof value === "boolean":
|
|
81
|
-
case typeof value === "number":
|
|
82
|
-
case typeof value === "string": return value;
|
|
83
|
-
case typeof value === "function": return;
|
|
84
|
-
case typeof value === "symbol": return Symbol(value.description);
|
|
85
|
-
case references.has(value): return references.get(value);
|
|
86
|
-
case value instanceof ArrayBuffer: return cloneArrayBuffer(value, depth, references);
|
|
87
|
-
case value instanceof DataView: return cloneDataView(value, depth, references);
|
|
88
|
-
case value instanceof Date: return new Date(value.getTime());
|
|
89
|
-
case value instanceof RegExp: return cloneRegularExpression(value, depth, references);
|
|
90
|
-
case value instanceof Map: return cloneMap(value, depth, references, flat);
|
|
91
|
-
case typeof Node !== "undefined" && value instanceof Node: return cloneNode(value, depth, references);
|
|
92
|
-
case value instanceof Set: return cloneSet(value, depth, references, flat);
|
|
93
|
-
case isArrayOrPlainObject(value): return cloneObject(value, depth, references, flat);
|
|
94
|
-
case isTypedArray(value): return cloneTypedArray(value, depth, references);
|
|
95
|
-
default: return clone.handlers.handle(value, depth, references);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
109
|
/**
|
|
99
110
|
* Copy any kind of value
|
|
100
111
|
*
|
|
@@ -105,7 +116,7 @@ function cloneValue(value, depth, references, flat) {
|
|
|
105
116
|
* @returns Copied value
|
|
106
117
|
*/
|
|
107
118
|
function copy(value) {
|
|
108
|
-
return
|
|
119
|
+
return clone(value, CLONE_COPY_OPTIONS);
|
|
109
120
|
}
|
|
110
121
|
/**
|
|
111
122
|
* Deregister a clone handler for a specific class
|
|
@@ -117,6 +128,25 @@ function copy(value) {
|
|
|
117
128
|
function deregisterCloner(constructor) {
|
|
118
129
|
clone.handlers.deregister(constructor);
|
|
119
130
|
}
|
|
131
|
+
function getCloneOptions(input) {
|
|
132
|
+
if (typeof input === "boolean") return {
|
|
133
|
+
copyFunctions: false,
|
|
134
|
+
copySymbols: false,
|
|
135
|
+
flat: input === true
|
|
136
|
+
};
|
|
137
|
+
if (typeof input !== "object" || input === null) return CLONE_DEFAULT_OPTIONS;
|
|
138
|
+
return {
|
|
139
|
+
copyFunctions: input.copyFunctions === true,
|
|
140
|
+
copySymbols: input.copySymbols === true,
|
|
141
|
+
flat: input.flat === true
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function getCloneParameters(input) {
|
|
145
|
+
return {
|
|
146
|
+
options: getCloneOptions(input),
|
|
147
|
+
references: /* @__PURE__ */ new WeakMap()
|
|
148
|
+
};
|
|
149
|
+
}
|
|
120
150
|
/**
|
|
121
151
|
* Register a clone handler for a specific class
|
|
122
152
|
*
|
|
@@ -128,17 +158,27 @@ function deregisterCloner(constructor) {
|
|
|
128
158
|
function registerCloner(constructor, handler) {
|
|
129
159
|
clone.handlers.register(constructor, handler);
|
|
130
160
|
}
|
|
131
|
-
function tryStructuredClone(value,
|
|
161
|
+
function tryStructuredClone(value, parameters, depth) {
|
|
132
162
|
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
133
163
|
try {
|
|
134
164
|
const cloned = structuredClone(value);
|
|
135
|
-
references
|
|
165
|
+
parameters.references?.set(value, cloned);
|
|
136
166
|
return cloned;
|
|
137
167
|
} catch {
|
|
138
|
-
references
|
|
168
|
+
parameters.references?.set(value, value);
|
|
139
169
|
return value;
|
|
140
170
|
}
|
|
141
171
|
}
|
|
172
|
+
const CLONE_COPY_OPTIONS = {
|
|
173
|
+
copyFunctions: true,
|
|
174
|
+
copySymbols: true,
|
|
175
|
+
flat: true
|
|
176
|
+
};
|
|
177
|
+
const CLONE_DEFAULT_OPTIONS = {
|
|
178
|
+
copyFunctions: false,
|
|
179
|
+
copySymbols: false,
|
|
180
|
+
flat: false
|
|
181
|
+
};
|
|
142
182
|
const CLONE_MAX_DEPTH = 100;
|
|
143
183
|
//#endregion
|
|
144
184
|
export { clone, copy, deregisterCloner, registerCloner };
|
|
@@ -4,7 +4,9 @@ function inMap(map, value, key) {
|
|
|
4
4
|
const getKey = key === true;
|
|
5
5
|
if (!(map instanceof Map)) return getKey ? void 0 : false;
|
|
6
6
|
if (!getKey) return inSet(new Set(map.values()), value);
|
|
7
|
-
|
|
7
|
+
let found;
|
|
8
|
+
for (const [key, item] of map) if (equal(item, value)) found = key;
|
|
9
|
+
return found;
|
|
8
10
|
}
|
|
9
11
|
/**
|
|
10
12
|
* Does the value exist in a _Set_?
|
package/dist/value/freeze.mjs
CHANGED
|
@@ -10,8 +10,6 @@ function flatFreeze(value) {
|
|
|
10
10
|
function freeze(value, flat) {
|
|
11
11
|
return freezeValue(value, /* @__PURE__ */ new WeakSet(), flat === true);
|
|
12
12
|
}
|
|
13
|
-
freeze.flat = flatFreeze;
|
|
14
|
-
freeze.is = isFrozen;
|
|
15
13
|
function freezeArray(array, references, flat) {
|
|
16
14
|
if (flat) return Object.freeze(array);
|
|
17
15
|
references.add(array);
|
|
@@ -76,5 +74,11 @@ function isFrozen(value) {
|
|
|
76
74
|
return typeof value === "object" && value !== null && Object.isFrozen(value);
|
|
77
75
|
}
|
|
78
76
|
const frozenValues = /* @__PURE__ */ new WeakSet();
|
|
77
|
+
freeze.flat = flatFreeze;
|
|
78
|
+
freeze.is = isFrozen;
|
|
79
|
+
Object.defineProperties(freeze, {
|
|
80
|
+
flat: { value: flatFreeze },
|
|
81
|
+
is: { value: isFrozen }
|
|
82
|
+
});
|
|
79
83
|
//#endregion
|
|
80
84
|
export { flatFreeze, freeze, isFrozen };
|
package/dist/value/merge.mjs
CHANGED
|
@@ -13,7 +13,6 @@ function assign(to, from, options) {
|
|
|
13
13
|
actual.assignValues = true;
|
|
14
14
|
return mergeValues([to, ...from], actual);
|
|
15
15
|
}
|
|
16
|
-
assign.initialize = initializeAssigner;
|
|
17
16
|
function getMergeOptions(options) {
|
|
18
17
|
const actual = {
|
|
19
18
|
assignValues: false,
|
|
@@ -67,7 +66,6 @@ function initializeMerger(options) {
|
|
|
67
66
|
function merge(values, options) {
|
|
68
67
|
return mergeValues(values, getMergeOptions(options));
|
|
69
68
|
}
|
|
70
|
-
merge.initialize = initializeMerger;
|
|
71
69
|
function mergeObjects(values, options, destination, prefix) {
|
|
72
70
|
const { length } = values;
|
|
73
71
|
const isArray = Array.isArray(destination ?? values[0]);
|
|
@@ -97,5 +95,9 @@ function mergeValues(values, options, prefix) {
|
|
|
97
95
|
if (actual.length > 1) return mergeObjects(actual, options, options.assignValues ? actual[0] : void 0, prefix);
|
|
98
96
|
return options.assignValues ? actual[0] : Array.isArray(actual[0]) ? actual[0].slice() : { ...actual[0] };
|
|
99
97
|
}
|
|
98
|
+
assign.initialize = initializeAssigner;
|
|
99
|
+
merge.initialize = initializeMerger;
|
|
100
|
+
Object.defineProperty(assign, "initialize", { value: initializeAssigner });
|
|
101
|
+
Object.defineProperty(merge, "initialize", { value: initializeMerger });
|
|
100
102
|
//#endregion
|
|
101
103
|
export { assign, initializeAssigner, initializeMerger, merge };
|
package/dist/value/transform.mjs
CHANGED
|
@@ -20,7 +20,6 @@ function initializeTransformer(transform) {
|
|
|
20
20
|
function transform(value, transform) {
|
|
21
21
|
return transformValue(value, getTransformer(transform));
|
|
22
22
|
}
|
|
23
|
-
transform.initialize = initializeTransformer;
|
|
24
23
|
function transformValue(value, transformer) {
|
|
25
24
|
if (isNonPlainObject(value)) return {};
|
|
26
25
|
if (transformer == null) return value;
|
|
@@ -34,5 +33,7 @@ function transformValue(value, transformer) {
|
|
|
34
33
|
}
|
|
35
34
|
return value;
|
|
36
35
|
}
|
|
36
|
+
transform.initialize = initializeTransformer;
|
|
37
|
+
Object.defineProperty(transform, "initialize", { value: initializeTransformer });
|
|
37
38
|
//#endregion
|
|
38
39
|
export { initializeTransformer, transform };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.197.0",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"array",
|
|
@@ -277,7 +277,7 @@
|
|
|
277
277
|
"watch": "npx vite build --watch"
|
|
278
278
|
},
|
|
279
279
|
"devDependencies": {
|
|
280
|
-
"@oxlint/plugins": "^1.
|
|
280
|
+
"@oxlint/plugins": "^1.81",
|
|
281
281
|
"@types/node": "^26.4",
|
|
282
282
|
"@vitest/coverage-istanbul": "^4.1",
|
|
283
283
|
"eslint": "^10.9",
|