@asd14/m 9.1.0 → 10.0.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/src/debounce/debounce.d.ts +13 -1
- package/dist/src/debounce/debounce.js +27 -20
- package/dist/src/distinct/by/distinct-by.d.ts +3 -0
- package/dist/src/distinct/by/distinct-by.js +3 -0
- package/dist/src/distinct/distinct.js +12 -3
- package/dist/src/escape/escape.js +6 -1
- package/dist/src/escape/reg-exp/escape-reg-exp.d.ts +3 -2
- package/dist/src/escape/reg-exp/escape-reg-exp.js +11 -4
- package/dist/src/flatten/flatten.d.ts +7 -0
- package/dist/src/flatten/flatten.js +14 -1
- package/dist/src/fuzzy-match/fuzzy-match.d.ts +3 -0
- package/dist/src/fuzzy-match/fuzzy-match.js +91 -55
- package/dist/src/group/group-by.d.ts +3 -0
- package/dist/src/group/group-by.js +10 -1
- package/dist/src/index-by/index-by.d.ts +3 -0
- package/dist/src/index-by/index-by.js +10 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/intersect/by/intersect-by.d.ts +3 -0
- package/dist/src/intersect/by/intersect-by.js +3 -0
- package/dist/src/is-deep-equal/is-deep-equal.d.ts +20 -5
- package/dist/src/is-deep-equal/is-deep-equal.js +44 -7
- package/dist/src/is-uri/is-uri.js +12 -9
- package/dist/src/merge/merge.d.ts +5 -1
- package/dist/src/merge/merge.js +12 -6
- package/dist/src/pick/pick.js +7 -0
- package/dist/src/throttle/throttle.d.ts +11 -1
- package/dist/src/throttle/throttle.js +49 -43
- package/dist/src/unite/by/unite-by.d.ts +3 -0
- package/dist/src/unite/by/unite-by.js +3 -0
- package/dist/src/update/update.d.ts +2 -2
- package/dist/src/utils/build-patch.js +3 -1
- package/dist/src/utils/parse-timer-options.d.ts +22 -0
- package/dist/src/utils/parse-timer-options.js +44 -0
- package/dist/src/utils/set-own.d.ts +5 -4
- package/dist/src/utils/set-own.js +6 -5
- package/dist/src/zip-to-object/zip-to-object.js +7 -0
- package/dist/types/object.d.ts +8 -4
- package/docs/SPEC.md +6 -6
- package/docs/how-to/prototype-pollution-tests.md +156 -0
- package/docs/how-to/tape-runtime-tests.md +30 -21
- package/docs/how-to/tstyche-type-tests.md +29 -14
- package/package.json +7 -4
- package/dist/src/evolve/deep-by/evolve-deep-by.d.ts +0 -65
- package/dist/src/evolve/deep-by/evolve-deep-by.js +0 -90
|
@@ -2,7 +2,21 @@
|
|
|
2
2
|
* Ported from fast-deep-equal 3.1.3, its ES6 and React build, to TypeScript
|
|
3
3
|
* https://github.com/epoberezkin/fast-deep-equal
|
|
4
4
|
* MIT License, Copyright (c) 2017 Evgeny Poberezkin
|
|
5
|
-
*/
|
|
5
|
+
*/ // A browser defines SharedArrayBuffer only on a cross-origin isolated page
|
|
6
|
+
const _isBinary = (input)=>input instanceof ArrayBuffer || input instanceof DataView || typeof SharedArrayBuffer === "function" && input instanceof SharedArrayBuffer;
|
|
7
|
+
const _toBytes = (input)=>input instanceof DataView ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength) : new Uint8Array(input);
|
|
8
|
+
const _isBytesEqual = (left, right)=>{
|
|
9
|
+
if (left.length !== right.length) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
for(let index = 0, length = left.length; index < length; index++){
|
|
13
|
+
if (left[index] !== right[index]) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
const _isDeepEqual = (left, right)=>{
|
|
6
20
|
if (left === right) {
|
|
7
21
|
return true;
|
|
8
22
|
}
|
|
@@ -52,6 +66,12 @@
|
|
|
52
66
|
}
|
|
53
67
|
return true;
|
|
54
68
|
}
|
|
69
|
+
// A buffer and a DataView have no keys and no length, so compare their
|
|
70
|
+
// bytes. fast-deep-equal 3.1.3 calls two different buffers equal, and loops
|
|
71
|
+
// forever on a DataView. The constructors match, so `right` has the same type
|
|
72
|
+
if (_isBinary(left)) {
|
|
73
|
+
return _isBytesEqual(_toBytes(left), _toBytes(right));
|
|
74
|
+
}
|
|
55
75
|
if (ArrayBuffer.isView(left) && ArrayBuffer.isView(right)) {
|
|
56
76
|
const leftView = left;
|
|
57
77
|
const rightView = right;
|
|
@@ -83,7 +103,9 @@
|
|
|
83
103
|
}
|
|
84
104
|
const leftRecord = left;
|
|
85
105
|
const rightRecord = right;
|
|
86
|
-
|
|
106
|
+
// React marks an element with a symbol, which JSON cannot produce. A JSON
|
|
107
|
+
// object with a `$$typeof` key keeps its `_owner` in the comparison
|
|
108
|
+
const isReactElement = typeof left.$$typeof === "symbol";
|
|
87
109
|
for(let index = 0, length = keys.length; index < length; index++){
|
|
88
110
|
const key = keys[index];
|
|
89
111
|
if (!Object.hasOwn(right, key)) {
|
|
@@ -100,11 +122,26 @@
|
|
|
100
122
|
return true;
|
|
101
123
|
};
|
|
102
124
|
/**
|
|
103
|
-
* Check two values for deep equality: arrays and typed arrays by items,
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* own
|
|
107
|
-
*
|
|
125
|
+
* Check two values for deep equality: arrays and typed arrays by items, an
|
|
126
|
+
* `ArrayBuffer`, `SharedArrayBuffer` or `DataView` by bytes, `Map` by entries,
|
|
127
|
+
* `Set` by keys, `RegExp` by source and flags, a `Date` or any object with its
|
|
128
|
+
* own `valueOf` or `toString` by that result, other objects by own enumerable
|
|
129
|
+
* keys. Objects of different constructors are never equal. A React element
|
|
130
|
+
* skips its `_owner`, which is circular. Two `NaN` are equal.
|
|
131
|
+
*
|
|
132
|
+
* State a JS program cannot read is not compared, eg. the contents of a `Blob`,
|
|
133
|
+
* the value of a `Promise` or a `#private` field.
|
|
134
|
+
*
|
|
135
|
+
* To compare secrets, eg. two signatures, use `crypto.timingSafeEqual`:
|
|
136
|
+
* `isDeepEqual` returns at the first difference, so its time leaks where the
|
|
137
|
+
* values differ.
|
|
138
|
+
*
|
|
139
|
+
* It recurses, so the stack limits the depth: deeply nested input throws a
|
|
140
|
+
* `RangeError`. The limit depends on V8's stack size and on whether V8 has
|
|
141
|
+
* optimized the function yet. Run `src/is-deep-equal/bench/depth.ts` to find
|
|
142
|
+
* it on your machine, next to Node's, lodash's and underscore's. `JSON.parse`
|
|
143
|
+
* accepts any depth, so check the depth of untrusted input first, if it can
|
|
144
|
+
* be that deep.
|
|
108
145
|
*
|
|
109
146
|
* Input is never mutated.
|
|
110
147
|
*
|
|
@@ -12,9 +12,11 @@ const _buildURIRegExp = ()=>{
|
|
|
12
12
|
const hexDigitOnly = `[${hexDigit}]`;
|
|
13
13
|
const unreserved = `${alpha}${digit}-\\._~`;
|
|
14
14
|
const subDelims = "!\\$&'\\(\\)\\*\\+,;=";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
// RFC 3986: pct-encoded = "%" HEXDIG HEXDIG, never a lone "%". It goes next
|
|
16
|
+
// to a character class as an alternative, since "%" is not in the class, so
|
|
17
|
+
// each character matches one way only and the regex cannot backtrack
|
|
18
|
+
const pctEncoded = `%${hexDigitOnly}{2}`;
|
|
19
|
+
const pcharOnly = `(?:[${unreserved}${subDelims}:@]|${pctEncoded})`;
|
|
18
20
|
// dec-octet, with zero-padded addresses accepted
|
|
19
21
|
const decOctet = `(?:0?0?${digitOnly}|0?[1-9]${digitOnly}|1${digitOnly}${digitOnly}` + `|2[0-4]${digitOnly}|25[0-5])`;
|
|
20
22
|
const ipv4 = `(?:${decOctet}\\.){3}${decOctet}`;
|
|
@@ -32,10 +34,12 @@ const _buildURIRegExp = ()=>{
|
|
|
32
34
|
`(?:(?:${h16}:){0,6}${h16})?::`
|
|
33
35
|
].join("|")})`;
|
|
34
36
|
const ipFuture = `v${hexDigitOnly}+\\.[${unreserved}${subDelims}:]+`;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
// `+.\-`, not `+-.`: inside a class, `+-.` is the range from + to ., which
|
|
38
|
+
// also covers the comma
|
|
39
|
+
const scheme = `[${alpha}][${alpha}${digit}+.\\-]*`;
|
|
40
|
+
const userinfo = `(?:[${unreserved}${subDelims}:]|${pctEncoded})*`;
|
|
37
41
|
const ipLiteral = `\\[(?:${ipv6}|${ipFuture})\\]`;
|
|
38
|
-
const regName = `[${unreserved}${
|
|
42
|
+
const regName = `(?:[${unreserved}${subDelims}]|${pctEncoded}){0,255}`;
|
|
39
43
|
const host = `(?:${ipLiteral}|${ipv4}|${regName})`;
|
|
40
44
|
const authority = `(?:${userinfo}@)?${host}(?::${digitOnly}*)?`;
|
|
41
45
|
const segment = `${pcharOnly}*`;
|
|
@@ -45,12 +49,11 @@ const _buildURIRegExp = ()=>{
|
|
|
45
49
|
const pathRootless = `${segmentNz}${pathAbEmpty}`;
|
|
46
50
|
const hierPart = `(?:(?:\\/\\/${authority}${pathAbEmpty})|${pathAbsolute}|${pathRootless})`;
|
|
47
51
|
// The query ends at the fragment or at the end of the input
|
|
48
|
-
const query = `
|
|
49
|
-
const fragment = `
|
|
52
|
+
const query = `(?:${pcharOnly}|[\\/\\?])*(?=#|$)`;
|
|
53
|
+
const fragment = `(?:${pcharOnly}|[\\/\\?])*`;
|
|
50
54
|
return new RegExp(`^(?:${scheme}:${hierPart})(?:\\?${query})?(?:#${fragment})?$`);
|
|
51
55
|
};
|
|
52
56
|
const URI_REG_EXP = _buildURIRegExp();
|
|
53
|
-
// --- Public function ---
|
|
54
57
|
/**
|
|
55
58
|
* Check that a string is a URI, as [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986)
|
|
56
59
|
* defines it. A URI needs a scheme, `example.com` is not one. Takes one
|
|
@@ -10,7 +10,11 @@ type MergeFn = {
|
|
|
10
10
|
};
|
|
11
11
|
/**
|
|
12
12
|
* Shallow-merge objects left-to-right. Right-most properties win. Every item
|
|
13
|
-
* must be a plain object
|
|
13
|
+
* must be a plain object. Copies own string keys: a symbol key is left out.
|
|
14
|
+
*
|
|
15
|
+
* A `"__proto__"` key stays an own key, and the prototype never changes.
|
|
16
|
+
* `Object.assign` differs here: it assigns each key, so an own `"__proto__"`
|
|
17
|
+
* key from `JSON.parse` replaces the prototype of its target.
|
|
14
18
|
*
|
|
15
19
|
* Accepts variadic objects, an array of objects, or a single object
|
|
16
20
|
* (curried, returns a function expecting the second object).
|
package/dist/src/merge/merge.js
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { isPlainObject } from "../is/is.js";
|
|
2
2
|
import { type } from "../type/type.js";
|
|
3
|
+
import { _setOwn } from "../utils/set-own.js";
|
|
3
4
|
const _merge = (objects)=>{
|
|
4
|
-
|
|
5
|
+
// One result, written key by key: a spread of the result for each object
|
|
6
|
+
// copies it again each time, which grows with the square of the input
|
|
7
|
+
const result = {};
|
|
5
8
|
for (const object of objects){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
9
|
+
for (const key of Object.keys(object)){
|
|
10
|
+
_setOwn(result, key, object[key]);
|
|
11
|
+
}
|
|
10
12
|
}
|
|
11
13
|
return result;
|
|
12
14
|
};
|
|
13
15
|
/**
|
|
14
16
|
* Shallow-merge objects left-to-right. Right-most properties win. Every item
|
|
15
|
-
* must be a plain object
|
|
17
|
+
* must be a plain object. Copies own string keys: a symbol key is left out.
|
|
18
|
+
*
|
|
19
|
+
* A `"__proto__"` key stays an own key, and the prototype never changes.
|
|
20
|
+
* `Object.assign` differs here: it assigns each key, so an own `"__proto__"`
|
|
21
|
+
* key from `JSON.parse` replaces the prototype of its target.
|
|
16
22
|
*
|
|
17
23
|
* Accepts variadic objects, an array of objects, or a single object
|
|
18
24
|
* (curried, returns a function expecting the second object).
|
package/dist/src/pick/pick.js
CHANGED
|
@@ -49,6 +49,13 @@ const _pick = (keys, input)=>{
|
|
|
49
49
|
if (!Array.isArray(keys)) {
|
|
50
50
|
throw new TypeError(`@asd14/m/pick: expected 'keys' to be 'Array', got '${type(keys)}'`);
|
|
51
51
|
}
|
|
52
|
+
// A key that is not a string passes the check of _setOwn, then the
|
|
53
|
+
// assignment turns it into one, eg. ["__proto__"] into "__proto__"
|
|
54
|
+
for(let i = 0, length = keys.length; i < length; i++){
|
|
55
|
+
if (typeof keys[i] !== "string") {
|
|
56
|
+
throw new TypeError(`@asd14/m/pick: expected 'keys[${i}]' to be 'String', got '${type(keys[i])}'`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
52
59
|
// @signature (keys) => (input)
|
|
53
60
|
if (params.length === 1) {
|
|
54
61
|
return (input)=>_pick(keys, input);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
type ThrottleOptions = {
|
|
2
|
-
/** Milliseconds between runs of `fn`, default `50` */
|
|
2
|
+
/** Milliseconds between runs of `fn`, at most 2,147,483,647, default `50` */
|
|
3
3
|
wait?: number;
|
|
4
|
+
/** Drops the call waiting for the window end when aborted, and makes later calls do nothing */
|
|
5
|
+
signal?: AbortSignal;
|
|
4
6
|
/** Run `fn` once more at the end of the window for a dropped call, default `false` */
|
|
5
7
|
hasLastCall?: boolean;
|
|
6
8
|
};
|
|
@@ -36,6 +38,14 @@ type ThrottleFn = {
|
|
|
36
38
|
* onScroll(2)
|
|
37
39
|
* // update(1) runs now, update(2) runs 100 ms later
|
|
38
40
|
*
|
|
41
|
+
* // Abort with a signal: the call waiting for the window end never runs, and
|
|
42
|
+
* // later calls do nothing. The same signal removes the listener
|
|
43
|
+
* const controller = new AbortController()
|
|
44
|
+
* const onResize = throttle({ wait: 100, signal: controller.signal }, layout)
|
|
45
|
+
*
|
|
46
|
+
* window.addEventListener("resize", onResize, { signal: controller.signal })
|
|
47
|
+
* controller.abort()
|
|
48
|
+
*
|
|
39
49
|
* @name throttle
|
|
40
50
|
* @tag Function
|
|
41
51
|
*
|
|
@@ -1,25 +1,41 @@
|
|
|
1
|
-
import { isPlainObject } from "../is/is.js";
|
|
2
1
|
import { type } from "../type/type.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { _parseTimerOptions } from "../utils/parse-timer-options.js";
|
|
3
|
+
const _throttle = (fn, wait, hasLastCall, signal)=>{
|
|
4
|
+
// The window opens when fn runs, and closes when its timer fires. No clock
|
|
5
|
+
// is read, so a wall clock that steps back cannot stretch the window
|
|
6
|
+
let windowTimer;
|
|
7
|
+
// The last call the window dropped, run when the window closes
|
|
8
|
+
let lastParams;
|
|
9
|
+
const run = (params)=>{
|
|
10
|
+
fn(...params);
|
|
11
|
+
// With no wait, every call runs: a 0 ms window would still drop the
|
|
12
|
+
// calls made before the next task
|
|
13
|
+
if (wait > 0) {
|
|
14
|
+
windowTimer = setTimeout(()=>{
|
|
15
|
+
windowTimer = undefined;
|
|
16
|
+
if (lastParams) {
|
|
17
|
+
const params = lastParams;
|
|
18
|
+
lastParams = undefined;
|
|
19
|
+
run(params);
|
|
20
|
+
}
|
|
21
|
+
}, wait);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
signal?.addEventListener("abort", ()=>{
|
|
25
|
+
clearTimeout(windowTimer);
|
|
26
|
+
windowTimer = undefined;
|
|
27
|
+
lastParams = undefined;
|
|
28
|
+
}, {
|
|
29
|
+
once: true
|
|
30
|
+
});
|
|
6
31
|
return (...params)=>{
|
|
7
|
-
|
|
8
|
-
if (lastExecution + wait <= now) {
|
|
9
|
-
clearTimeout(timer);
|
|
10
|
-
timer = undefined;
|
|
11
|
-
lastExecution = now;
|
|
12
|
-
fn(...params);
|
|
32
|
+
if (signal?.aborted) {
|
|
13
33
|
return;
|
|
14
34
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
timer = undefined;
|
|
20
|
-
lastExecution = Date.now();
|
|
21
|
-
fn(...params);
|
|
22
|
-
}, lastExecution + wait - now);
|
|
35
|
+
if (windowTimer === undefined) {
|
|
36
|
+
run(params);
|
|
37
|
+
} else if (hasLastCall) {
|
|
38
|
+
lastParams = params;
|
|
23
39
|
}
|
|
24
40
|
};
|
|
25
41
|
};
|
|
@@ -51,40 +67,30 @@ const _throttle = (fn, wait, hasLastCall)=>{
|
|
|
51
67
|
* onScroll(2)
|
|
52
68
|
* // update(1) runs now, update(2) runs 100 ms later
|
|
53
69
|
*
|
|
70
|
+
* // Abort with a signal: the call waiting for the window end never runs, and
|
|
71
|
+
* // later calls do nothing. The same signal removes the listener
|
|
72
|
+
* const controller = new AbortController()
|
|
73
|
+
* const onResize = throttle({ wait: 100, signal: controller.signal }, layout)
|
|
74
|
+
*
|
|
75
|
+
* window.addEventListener("resize", onResize, { signal: controller.signal })
|
|
76
|
+
* controller.abort()
|
|
77
|
+
*
|
|
54
78
|
* @name throttle
|
|
55
79
|
* @tag Function
|
|
56
80
|
*
|
|
57
81
|
* @see {@link debounce}
|
|
58
82
|
*/ const throttle = (...params)=>{
|
|
59
83
|
const fn = params[params.length - 1];
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
const optionWait = options["wait"];
|
|
68
|
-
const optionHasLastCall = options["hasLastCall"];
|
|
69
|
-
if (optionWait !== undefined) {
|
|
70
|
-
if (typeof optionWait !== "number") {
|
|
71
|
-
throw new TypeError(`@asd14/m/throttle: expected 'wait' to be 'Number', got '${type(optionWait)}'`);
|
|
72
|
-
}
|
|
73
|
-
if (!(optionWait >= 0)) {
|
|
74
|
-
throw new TypeError(`@asd14/m/throttle: expected 'wait' to be 'NonNegative', got '${String(optionWait)}'`);
|
|
75
|
-
}
|
|
76
|
-
wait = optionWait;
|
|
77
|
-
}
|
|
78
|
-
if (optionHasLastCall !== undefined) {
|
|
79
|
-
if (typeof optionHasLastCall !== "boolean") {
|
|
80
|
-
throw new TypeError(`@asd14/m/throttle: expected 'hasLastCall' to be 'Boolean', got '${type(optionHasLastCall)}'`);
|
|
81
|
-
}
|
|
82
|
-
hasLastCall = optionHasLastCall;
|
|
83
|
-
}
|
|
84
|
+
const { options, wait, signal } = _parseTimerOptions(params.length > 1 ? params[0] : undefined, {
|
|
85
|
+
fnName: "throttle"
|
|
86
|
+
});
|
|
87
|
+
const { hasLastCall = false } = options;
|
|
88
|
+
if (typeof hasLastCall !== "boolean") {
|
|
89
|
+
throw new TypeError(`@asd14/m/throttle: expected 'hasLastCall' to be 'Boolean', got '${type(hasLastCall)}'`);
|
|
84
90
|
}
|
|
85
91
|
if (typeof fn !== "function") {
|
|
86
92
|
throw new TypeError(`@asd14/m/throttle: expected 'fn' to be 'Function', got '${type(fn)}'`);
|
|
87
93
|
}
|
|
88
|
-
return _throttle(fn, wait, hasLastCall);
|
|
94
|
+
return _throttle(fn, wait, hasLastCall, signal);
|
|
89
95
|
};
|
|
90
96
|
export { throttle };
|
|
@@ -8,6 +8,9 @@ type UniteByFn = {
|
|
|
8
8
|
* array of the items of both arrays, where the first of each group of equal
|
|
9
9
|
* items wins. Order is preserved - items from the first array appear first.
|
|
10
10
|
*
|
|
11
|
+
* Compares each item with each kept item, so the time grows with the square
|
|
12
|
+
* of the input. To compare with SameValueZero, use `unite`, which is linear.
|
|
13
|
+
*
|
|
11
14
|
* Input is never mutated - always returns a new array.
|
|
12
15
|
*
|
|
13
16
|
* @param {Function} fn - `(newItem, keptItem) => boolean`, true when equal
|
|
@@ -15,6 +15,9 @@ const _uniteBy = (fn, a, b)=>{
|
|
|
15
15
|
* array of the items of both arrays, where the first of each group of equal
|
|
16
16
|
* items wins. Order is preserved - items from the first array appear first.
|
|
17
17
|
*
|
|
18
|
+
* Compares each item with each kept item, so the time grows with the square
|
|
19
|
+
* of the input. To compare with SameValueZero, use `unite`, which is linear.
|
|
20
|
+
*
|
|
18
21
|
* Input is never mutated - always returns a new array.
|
|
19
22
|
*
|
|
20
23
|
* @param {Function} fn - `(newItem, keptItem) => boolean`, true when equal
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Patcher } from "../../types/object.patcher.ts";
|
|
2
|
+
import type { PlainObject } from "../../types/object.ts";
|
|
3
3
|
type UpdateFn = {
|
|
4
4
|
<T extends PlainObject>(patcher: Patcher<T>, input: T): T;
|
|
5
5
|
<T extends PlainObject>(patcher: Patcher<T>): <U extends T>(input: U) => U;
|
|
@@ -51,7 +51,9 @@ import { type } from "../type/type.js";
|
|
|
51
51
|
if (isTransformOnly && !Object.hasOwn(input, key)) {
|
|
52
52
|
continue;
|
|
53
53
|
}
|
|
54
|
-
_setOwn(result, key,
|
|
54
|
+
_setOwn(result, key, // An own value only: a key the input lacks, eg. toString, gives
|
|
55
|
+
// undefined, not the value Object.prototype has
|
|
56
|
+
isFunction ? value(Object.hasOwn(input, key) ? input[key] : undefined, input) : value);
|
|
55
57
|
}
|
|
56
58
|
return result;
|
|
57
59
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PlainObject } from "../../types/object.js";
|
|
2
|
+
type TimerOptions = {
|
|
3
|
+
/** The options object, checked to be plain, for the options of one caller */
|
|
4
|
+
options: PlainObject;
|
|
5
|
+
wait: number;
|
|
6
|
+
signal: AbortSignal | undefined;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Check the options that `debounce` and `throttle` share: `wait`, a delay a
|
|
10
|
+
* timer can hold, and `signal`, an `AbortSignal`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* _parseTimerOptions({ wait: 300 }, { fnName: "debounce" })
|
|
14
|
+
* // => { options: { wait: 300 }, wait: 300, signal: undefined }
|
|
15
|
+
*
|
|
16
|
+
* _parseTimerOptions({ wait: Infinity }, { fnName: "debounce" })
|
|
17
|
+
* // throws "@asd14/m/debounce: expected 'wait' to be 'TimerDelay', got 'Infinity'"
|
|
18
|
+
*/
|
|
19
|
+
declare const _parseTimerOptions: (options: unknown, { fnName }: {
|
|
20
|
+
fnName: string;
|
|
21
|
+
}) => TimerOptions;
|
|
22
|
+
export { _parseTimerOptions };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isPlainObject } from "../is/is.js";
|
|
2
|
+
import { type } from "../type/type.js";
|
|
3
|
+
// A timer stores its delay in 32 bits. Node and browsers run a larger delay,
|
|
4
|
+
// or Infinity, after 1 ms
|
|
5
|
+
const MAX_TIMER_DELAY = 2_147_483_647;
|
|
6
|
+
const DEFAULT_WAIT = 50;
|
|
7
|
+
/**
|
|
8
|
+
* Check the options that `debounce` and `throttle` share: `wait`, a delay a
|
|
9
|
+
* timer can hold, and `signal`, an `AbortSignal`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* _parseTimerOptions({ wait: 300 }, { fnName: "debounce" })
|
|
13
|
+
* // => { options: { wait: 300 }, wait: 300, signal: undefined }
|
|
14
|
+
*
|
|
15
|
+
* _parseTimerOptions({ wait: Infinity }, { fnName: "debounce" })
|
|
16
|
+
* // throws "@asd14/m/debounce: expected 'wait' to be 'TimerDelay', got 'Infinity'"
|
|
17
|
+
*/ const _parseTimerOptions = (options, { fnName })=>{
|
|
18
|
+
if (options === undefined) {
|
|
19
|
+
return {
|
|
20
|
+
options: {},
|
|
21
|
+
wait: DEFAULT_WAIT,
|
|
22
|
+
signal: undefined
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (!isPlainObject(options)) {
|
|
26
|
+
throw new TypeError(`@asd14/m/${fnName}: expected 'options' to be 'Object', got '${type(options)}'`);
|
|
27
|
+
}
|
|
28
|
+
const { wait = DEFAULT_WAIT, signal } = options;
|
|
29
|
+
if (typeof wait !== "number") {
|
|
30
|
+
throw new TypeError(`@asd14/m/${fnName}: expected 'wait' to be 'Number', got '${type(wait)}'`);
|
|
31
|
+
}
|
|
32
|
+
if (!(wait >= 0 && wait <= MAX_TIMER_DELAY)) {
|
|
33
|
+
throw new TypeError(`@asd14/m/${fnName}: expected 'wait' to be 'TimerDelay', got '${String(wait)}'`);
|
|
34
|
+
}
|
|
35
|
+
if (signal !== undefined && !(signal instanceof AbortSignal)) {
|
|
36
|
+
throw new TypeError(`@asd14/m/${fnName}: expected 'signal' to be 'AbortSignal', got '${type(signal)}'`);
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
options,
|
|
40
|
+
wait,
|
|
41
|
+
signal
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export { _parseTimerOptions };
|
|
@@ -2,10 +2,11 @@ import type { PlainObject } from "../../types/object.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Set an own key on an object that the caller is building.
|
|
4
4
|
*
|
|
5
|
-
* A plain assignment of
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* A plain assignment of a key that the prototype chain has, eg. `__proto__` or
|
|
6
|
+
* `constructor`, reaches the inherited property: it calls the `__proto__`
|
|
7
|
+
* setter, and it throws when the prototype is frozen. Such a key gets
|
|
8
|
+
* `Object.defineProperty`. Every other key keeps the assignment, which builds
|
|
9
|
+
* an object about 11 times faster.
|
|
9
10
|
*
|
|
10
11
|
* Writes into `target`. Pass only an object you created.
|
|
11
12
|
*
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Set an own key on an object that the caller is building.
|
|
3
3
|
*
|
|
4
|
-
* A plain assignment of
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* A plain assignment of a key that the prototype chain has, eg. `__proto__` or
|
|
5
|
+
* `constructor`, reaches the inherited property: it calls the `__proto__`
|
|
6
|
+
* setter, and it throws when the prototype is frozen. Such a key gets
|
|
7
|
+
* `Object.defineProperty`. Every other key keeps the assignment, which builds
|
|
8
|
+
* an object about 11 times faster.
|
|
8
9
|
*
|
|
9
10
|
* Writes into `target`. Pass only an object you created.
|
|
10
11
|
*
|
|
@@ -15,7 +16,7 @@
|
|
|
15
16
|
* Object.keys(result)
|
|
16
17
|
* // => ["__proto__"]
|
|
17
18
|
*/ const _setOwn = (target, key, value)=>{
|
|
18
|
-
if (key
|
|
19
|
+
if (key in target && !Object.hasOwn(target, key)) {
|
|
19
20
|
Object.defineProperty(target, key, {
|
|
20
21
|
value,
|
|
21
22
|
enumerable: true,
|
|
@@ -48,6 +48,13 @@ const _zipToObject = (keys, values)=>{
|
|
|
48
48
|
if (!Array.isArray(keys)) {
|
|
49
49
|
throw new TypeError(`@asd14/m/zipToObject: expected 'keys' to be 'Array', got '${type(keys)}'`);
|
|
50
50
|
}
|
|
51
|
+
// A key that is not a string passes the check of _setOwn, then the
|
|
52
|
+
// assignment turns it into one, eg. ["__proto__"] into "__proto__"
|
|
53
|
+
for(let i = 0, length = keys.length; i < length; i++){
|
|
54
|
+
if (typeof keys[i] !== "string") {
|
|
55
|
+
throw new TypeError(`@asd14/m/zipToObject: expected 'keys[${i}]' to be 'String', got '${type(keys[i])}'`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
51
58
|
// @signature (keys) => (values)
|
|
52
59
|
if (params.length === 1) {
|
|
53
60
|
return (values)=>_zipToObject(keys, values);
|
package/dist/types/object.d.ts
CHANGED
|
@@ -100,16 +100,20 @@ export type DeepWrite<T, Path extends readonly Property[], V> = Path extends rea
|
|
|
100
100
|
[K in keyof O]: O[K];
|
|
101
101
|
} : never : unknown;
|
|
102
102
|
/**
|
|
103
|
-
* Resolve the type of all values of an object (union)
|
|
103
|
+
* Resolve the type of all leaf values of an object (union). An array is not a
|
|
104
|
+
* leaf: its items are. A recursive type, eg. a tree, stops after 10 levels,
|
|
105
|
+
* which loses no leaf type, since each level adds the same ones
|
|
104
106
|
*
|
|
105
107
|
* @example
|
|
106
108
|
* DeepValues<{ a: number, b: string }>
|
|
107
109
|
* // => number | string
|
|
108
110
|
*
|
|
109
111
|
* DeepValues<{ a: number, b: { c: string[] } }>
|
|
110
|
-
* // => number | string
|
|
112
|
+
* // => number | string
|
|
111
113
|
*/
|
|
112
|
-
export type DeepValues<T extends PlainObject> = {
|
|
113
|
-
[K in keyof T]: T[K] extends
|
|
114
|
+
export type DeepValues<T extends PlainObject, Depth extends unknown[] = []> = Depth["length"] extends 10 ? never : {
|
|
115
|
+
[K in keyof T]: T[K] extends infer Value ? Value extends readonly unknown[] ? DeepValues<{
|
|
116
|
+
item: Value[number];
|
|
117
|
+
}, [...Depth, unknown]> : Value extends PlainObject ? DeepValues<Value, [...Depth, unknown]> : Value : never;
|
|
114
118
|
}[keyof T];
|
|
115
119
|
export {};
|
package/docs/SPEC.md
CHANGED
|
@@ -106,12 +106,12 @@ update({ tags: () => [] }, todo)
|
|
|
106
106
|
|
|
107
107
|
Each concept follows the same rule in every function:
|
|
108
108
|
|
|
109
|
-
| Concept | Rule
|
|
110
|
-
| -------------------- |
|
|
111
|
-
| A predicate passes | when it returns `=== true`
|
|
112
|
-
| Two values are equal | SameValueZero, as `Array.prototype.includes`
|
|
113
|
-
| A key | an own key, read with `Object.hasOwn`
|
|
114
|
-
| An object | a plain object: its prototype is `Object.prototype` or null
|
|
109
|
+
| Concept | Rule |
|
|
110
|
+
| -------------------- | ----------------------------------------------------------------------------------- |
|
|
111
|
+
| A predicate passes | when it returns `=== true` |
|
|
112
|
+
| Two values are equal | SameValueZero, as `Array.prototype.includes` |
|
|
113
|
+
| A key | an own key, read with `Object.hasOwn`, `__proto__` too. The prototype never changes |
|
|
114
|
+
| An object | a plain object: its prototype is `Object.prototype` or null |
|
|
115
115
|
|
|
116
116
|
```ts
|
|
117
117
|
// Bad: remove compares with `===`, while distinct uses SameValueZero
|