@asd14/m 9.0.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.
Files changed (67) hide show
  1. package/dist/src/debounce/debounce.d.ts +13 -1
  2. package/dist/src/debounce/debounce.js +27 -20
  3. package/dist/src/distinct/by/distinct-by.d.ts +3 -0
  4. package/dist/src/distinct/by/distinct-by.js +3 -0
  5. package/dist/src/distinct/distinct.js +12 -3
  6. package/dist/src/drop/drop.d.ts +38 -0
  7. package/dist/src/drop/drop.js +56 -0
  8. package/dist/src/escape/escape.js +6 -1
  9. package/dist/src/escape/reg-exp/escape-reg-exp.d.ts +3 -2
  10. package/dist/src/escape/reg-exp/escape-reg-exp.js +11 -4
  11. package/dist/src/flatten/flatten.d.ts +7 -0
  12. package/dist/src/flatten/flatten.js +14 -1
  13. package/dist/src/fuzzy-match/fuzzy-match.d.ts +3 -0
  14. package/dist/src/fuzzy-match/fuzzy-match.js +91 -55
  15. package/dist/src/group/group-by.d.ts +19 -7
  16. package/dist/src/group/group-by.js +20 -2
  17. package/dist/src/index-by/index-by.d.ts +70 -0
  18. package/dist/src/index-by/index-by.js +82 -0
  19. package/dist/src/index.d.ts +12 -1
  20. package/dist/src/index.js +12 -9
  21. package/dist/src/intersect/by/intersect-by.d.ts +48 -0
  22. package/dist/src/intersect/by/intersect-by.js +75 -0
  23. package/dist/src/intersect/intersect.d.ts +42 -0
  24. package/dist/src/intersect/intersect.js +63 -0
  25. package/dist/src/is-deep-equal/is-deep-equal.d.ts +20 -5
  26. package/dist/src/is-deep-equal/is-deep-equal.js +44 -7
  27. package/dist/src/is-uri/is-uri.js +12 -9
  28. package/dist/src/max/by/max-by.d.ts +52 -0
  29. package/dist/src/max/by/max-by.js +69 -0
  30. package/dist/src/max/max.d.ts +34 -0
  31. package/dist/src/max/max.js +49 -0
  32. package/dist/src/merge/merge.d.ts +5 -1
  33. package/dist/src/merge/merge.js +12 -6
  34. package/dist/src/min/by/min-by.d.ts +52 -0
  35. package/dist/src/min/by/min-by.js +69 -0
  36. package/dist/src/min/min.d.ts +34 -0
  37. package/dist/src/min/min.js +49 -0
  38. package/dist/src/pick/pick.js +7 -0
  39. package/dist/src/read/read.d.ts +1 -0
  40. package/dist/src/read/read.js +1 -0
  41. package/dist/src/take/last/take-last.d.ts +38 -0
  42. package/dist/src/take/last/take-last.js +56 -0
  43. package/dist/src/take/take.d.ts +38 -0
  44. package/dist/src/take/take.js +56 -0
  45. package/dist/src/throttle/throttle.d.ts +11 -1
  46. package/dist/src/throttle/throttle.js +49 -43
  47. package/dist/src/unite/by/unite-by.d.ts +3 -0
  48. package/dist/src/unite/by/unite-by.js +3 -0
  49. package/dist/src/update/update.d.ts +2 -2
  50. package/dist/src/utils/build-patch.js +3 -1
  51. package/dist/src/utils/parse-timer-options.d.ts +22 -0
  52. package/dist/src/utils/parse-timer-options.js +44 -0
  53. package/dist/src/utils/set-own.d.ts +5 -4
  54. package/dist/src/utils/set-own.js +6 -5
  55. package/dist/src/values/values.d.ts +42 -0
  56. package/dist/src/values/values.js +46 -0
  57. package/dist/src/write/write.d.ts +53 -0
  58. package/dist/src/write/write.js +104 -0
  59. package/dist/src/zip-to-object/zip-to-object.js +7 -0
  60. package/dist/types/object.d.ts +53 -4
  61. package/docs/SPEC.md +8 -7
  62. package/docs/how-to/prototype-pollution-tests.md +156 -0
  63. package/docs/how-to/tape-runtime-tests.md +30 -21
  64. package/docs/how-to/tstyche-type-tests.md +29 -14
  65. package/package.json +8 -5
  66. package/dist/src/evolve/deep-by/evolve-deep-by.d.ts +0 -65
  67. package/dist/src/evolve/deep-by/evolve-deep-by.js +0 -90
@@ -0,0 +1,70 @@
1
+ import type { UnaryMorphism } from "../../types/function.js";
2
+ type IndexByFn = {
3
+ <T1, T2, T3>(fns: [
4
+ UnaryMorphism<T1, T2>,
5
+ UnaryMorphism<T2, T3>,
6
+ UnaryMorphism<T3, string | number>
7
+ ], input: readonly T1[]): Record<string, T1>;
8
+ <T1, T2, T3>(fns: [
9
+ UnaryMorphism<T1, T2>,
10
+ UnaryMorphism<T2, T3>,
11
+ UnaryMorphism<T3, string | number>
12
+ ]): (input: readonly T1[]) => Record<string, T1>;
13
+ <T1, T2>(fns: [UnaryMorphism<T1, T2>, UnaryMorphism<T2, string | number>], input: readonly T1[]): Record<string, T1>;
14
+ <T1, T2>(fns: [UnaryMorphism<T1, T2>, UnaryMorphism<T2, string | number>]): (input: readonly T1[]) => Record<string, T1>;
15
+ <T>(fn: (item: T) => string | number, input: readonly T[]): Record<string, T>;
16
+ <T>(fn: (item: T) => string | number): (input: readonly T[]) => Record<string, T>;
17
+ };
18
+ /**
19
+ * Index the items of an array by the key a function returns for each. Returns
20
+ * an object where each key holds one item. The function returns a string or a
21
+ * number, and the key is its string form.
22
+ *
23
+ * On a duplicate key the last item wins.
24
+ *
25
+ * Only own keys count, so `"constructor"`, `"toString"` and `"__proto__"` work
26
+ * as keys.
27
+ *
28
+ * Input is never mutated - always returns a new object.
29
+ *
30
+ * @param {Function|Array} fn - `(item) => key`, or a pipe array of functions
31
+ * @param {Array} [input] - Array to index
32
+ *
33
+ * @returns {Object} Object of items, one for each key
34
+ *
35
+ * @throws {TypeError} When `fn` returns neither a string nor a number, eg.
36
+ * `expected 'fn(input[1])' to be 'String' or 'Number', got 'Array'`
37
+ *
38
+ * @example
39
+ * indexBy(user => user.id, [
40
+ * { id: 1, name: "alice" },
41
+ * { id: 2, name: "bob" }
42
+ * ])
43
+ * // => { 1: { id: 1, name: "alice" }, 2: { id: 2, name: "bob" } }
44
+ *
45
+ * // The last item wins on a duplicate key
46
+ * indexBy(user => user.role)([
47
+ * { name: "alice", role: "admin" },
48
+ * { name: "carol", role: "admin" }
49
+ * ])
50
+ * // => { admin: { name: "carol", role: "admin" } }
51
+ *
52
+ * // A pipe array builds the key in steps
53
+ * indexBy([read("name"), name => name.toUpperCase()], users)
54
+ * // => { ALICE: {...}, BOB: {...} }
55
+ *
56
+ * // In a pipe - list the keys
57
+ * pipe(
58
+ * indexBy(n => n % 3),
59
+ * keys
60
+ * )([1, 2, 3, 4, 5])
61
+ * // => ["0", "1", "2"]
62
+ *
63
+ * @name indexBy
64
+ * @tag Array
65
+ *
66
+ * @see {@link groupBy}
67
+ */
68
+ declare const indexBy: IndexByFn;
69
+ export { indexBy };
70
+ export type { IndexByFn };
@@ -0,0 +1,82 @@
1
+ import { type } from "../type/type.js";
2
+ import { _setOwn } from "../utils/set-own.js";
3
+ import { _toFunction } from "../utils/to-function.js";
4
+ const _indexBy = (fn, input)=>{
5
+ if (!Array.isArray(input)) {
6
+ throw new TypeError(`@asd14/m/indexBy: expected 'input' to be 'Array', got '${type(input)}'`);
7
+ }
8
+ const result = {};
9
+ for(let i = 0, length = input.length; i < length; i++){
10
+ const item = input[i];
11
+ const key = fn(item);
12
+ // The types allow a string or a number. Anything else would turn into a
13
+ // string that can match another item's key, eg. ["admin"] into "admin"
14
+ if (typeof key !== "string" && typeof key !== "number") {
15
+ throw new TypeError(`@asd14/m/indexBy: expected 'fn(input[${i}])' to be 'String' or 'Number', got '${type(key)}'`);
16
+ }
17
+ _setOwn(result, String(key), item);
18
+ }
19
+ return result;
20
+ };
21
+ /**
22
+ * Index the items of an array by the key a function returns for each. Returns
23
+ * an object where each key holds one item. The function returns a string or a
24
+ * number, and the key is its string form.
25
+ *
26
+ * On a duplicate key the last item wins.
27
+ *
28
+ * Only own keys count, so `"constructor"`, `"toString"` and `"__proto__"` work
29
+ * as keys.
30
+ *
31
+ * Input is never mutated - always returns a new object.
32
+ *
33
+ * @param {Function|Array} fn - `(item) => key`, or a pipe array of functions
34
+ * @param {Array} [input] - Array to index
35
+ *
36
+ * @returns {Object} Object of items, one for each key
37
+ *
38
+ * @throws {TypeError} When `fn` returns neither a string nor a number, eg.
39
+ * `expected 'fn(input[1])' to be 'String' or 'Number', got 'Array'`
40
+ *
41
+ * @example
42
+ * indexBy(user => user.id, [
43
+ * { id: 1, name: "alice" },
44
+ * { id: 2, name: "bob" }
45
+ * ])
46
+ * // => { 1: { id: 1, name: "alice" }, 2: { id: 2, name: "bob" } }
47
+ *
48
+ * // The last item wins on a duplicate key
49
+ * indexBy(user => user.role)([
50
+ * { name: "alice", role: "admin" },
51
+ * { name: "carol", role: "admin" }
52
+ * ])
53
+ * // => { admin: { name: "carol", role: "admin" } }
54
+ *
55
+ * // A pipe array builds the key in steps
56
+ * indexBy([read("name"), name => name.toUpperCase()], users)
57
+ * // => { ALICE: {...}, BOB: {...} }
58
+ *
59
+ * // In a pipe - list the keys
60
+ * pipe(
61
+ * indexBy(n => n % 3),
62
+ * keys
63
+ * )([1, 2, 3, 4, 5])
64
+ * // => ["0", "1", "2"]
65
+ *
66
+ * @name indexBy
67
+ * @tag Array
68
+ *
69
+ * @see {@link groupBy}
70
+ */ const indexBy = (...params)=>{
71
+ const fn = _toFunction(params[0], {
72
+ fnName: "indexBy",
73
+ paramName: "fn"
74
+ });
75
+ // @signature (fn) => (input)
76
+ if (params.length === 1) {
77
+ return (input)=>_indexBy(fn, input);
78
+ }
79
+ // @signature (fn, input)
80
+ return _indexBy(fn, params[1]);
81
+ };
82
+ export { indexBy };
@@ -19,6 +19,7 @@ export * from "./count/count.js";
19
19
  export * from "./count/with/count-with.js";
20
20
  export * from "./distinct/by/distinct-by.js";
21
21
  export * from "./distinct/distinct.js";
22
+ export * from "./drop/drop.js";
22
23
  export * from "./drop/last/drop-last.js";
23
24
  export * from "./evolve/many/evolve-many.js";
24
25
  export * from "./evolve/with/evolve-with.js";
@@ -32,10 +33,15 @@ export * from "./first/first.js";
32
33
  export * from "./flatten/flatten.js";
33
34
  export * from "./for-each/for-each.js";
34
35
  export * from "./group/group-by.js";
36
+ export * from "./index-by/index-by.js";
37
+ export * from "./intersect/by/intersect-by.js";
38
+ export * from "./intersect/intersect.js";
35
39
  export * from "./last/last.js";
36
40
  export * from "./map/indexed/map-indexed.js";
37
41
  export * from "./map/map.js";
38
42
  export * from "./map/matrix/map-matrix.js";
43
+ export * from "./max/by/max-by.js";
44
+ export * from "./min/by/min-by.js";
39
45
  export * from "./partition/partition.js";
40
46
  export * from "./partition/with/partition-with.js";
41
47
  export * from "./push/push.js";
@@ -46,12 +52,13 @@ export * from "./sort/by/sort-by.js";
46
52
  export * from "./sort/sort.js";
47
53
  export * from "./sort/with/sort-with.js";
48
54
  export * from "./split-in-groups-of/split-in-groups-of.js";
55
+ export * from "./take/last/take-last.js";
56
+ export * from "./take/take.js";
49
57
  export * from "./unite/by/unite-by.js";
50
58
  export * from "./unite/unite.js";
51
59
  export * from "./update/with/update-with.js";
52
60
  export * from "./upsert/with/upsert-with.js";
53
61
  export * from "./zip-to-object/zip-to-object.js";
54
- export * from "./evolve/deep-by/evolve-deep-by.js";
55
62
  export * from "./evolve/evolve.js";
56
63
  export * from "./is-match/is-match.js";
57
64
  export * from "./keys/keys.js";
@@ -61,6 +68,8 @@ export * from "./pick/pick.js";
61
68
  export * from "./read/read.js";
62
69
  export * from "./unflatten/unflatten.js";
63
70
  export * from "./update/update.js";
71
+ export * from "./values/values.js";
72
+ export * from "./write/write.js";
64
73
  export * from "./escape/escape.js";
65
74
  export * from "./escape/html/escape-html.js";
66
75
  export * from "./escape/reg-exp/escape-reg-exp.js";
@@ -72,6 +81,8 @@ export * from "./to-string/to-string.js";
72
81
  export * from "./trim/trim.js";
73
82
  export * from "./dec/dec.js";
74
83
  export * from "./inc/inc.js";
84
+ export * from "./max/max.js";
85
+ export * from "./min/min.js";
75
86
  export * from "./sum/sum.js";
76
87
  export * from "./is/is.js";
77
88
  /**
package/dist/src/index.js CHANGED
@@ -22,6 +22,7 @@ export * from "./count/count.js";
22
22
  export * from "./count/with/count-with.js";
23
23
  export * from "./distinct/by/distinct-by.js";
24
24
  export * from "./distinct/distinct.js";
25
+ export * from "./drop/drop.js";
25
26
  export * from "./drop/last/drop-last.js";
26
27
  export * from "./evolve/many/evolve-many.js";
27
28
  export * from "./evolve/with/evolve-with.js";
@@ -35,10 +36,15 @@ export * from "./first/first.js";
35
36
  export * from "./flatten/flatten.js";
36
37
  export * from "./for-each/for-each.js";
37
38
  export * from "./group/group-by.js";
39
+ export * from "./index-by/index-by.js";
40
+ export * from "./intersect/by/intersect-by.js";
41
+ export * from "./intersect/intersect.js";
38
42
  export * from "./last/last.js";
39
43
  export * from "./map/indexed/map-indexed.js";
40
44
  export * from "./map/map.js";
41
45
  export * from "./map/matrix/map-matrix.js";
46
+ export * from "./max/by/max-by.js";
47
+ export * from "./min/by/min-by.js";
42
48
  export * from "./partition/partition.js";
43
49
  export * from "./partition/with/partition-with.js";
44
50
  export * from "./push/push.js";
@@ -49,13 +55,14 @@ export * from "./sort/by/sort-by.js";
49
55
  export * from "./sort/sort.js";
50
56
  export * from "./sort/with/sort-with.js";
51
57
  export * from "./split-in-groups-of/split-in-groups-of.js";
58
+ export * from "./take/last/take-last.js";
59
+ export * from "./take/take.js";
52
60
  export * from "./unite/by/unite-by.js";
53
61
  export * from "./unite/unite.js";
54
62
  export * from "./update/with/update-with.js";
55
63
  export * from "./upsert/with/upsert-with.js";
56
64
  export * from "./zip-to-object/zip-to-object.js";
57
65
  // Object
58
- export * from "./evolve/deep-by/evolve-deep-by.js";
59
66
  export * from "./evolve/evolve.js";
60
67
  export * from "./is-match/is-match.js";
61
68
  export * from "./keys/keys.js";
@@ -65,6 +72,8 @@ export * from "./pick/pick.js";
65
72
  export * from "./read/read.js";
66
73
  export * from "./unflatten/unflatten.js";
67
74
  export * from "./update/update.js";
75
+ export * from "./values/values.js";
76
+ export * from "./write/write.js";
68
77
  // String
69
78
  export * from "./escape/escape.js";
70
79
  export * from "./escape/html/escape-html.js";
@@ -78,12 +87,13 @@ export * from "./trim/trim.js";
78
87
  // Number
79
88
  export * from "./dec/dec.js";
80
89
  export * from "./inc/inc.js";
90
+ export * from "./max/max.js";
91
+ export * from "./min/min.js";
81
92
  export * from "./sum/sum.js";
82
93
  // Type guard
83
94
  export * from "./is/is.js"; /**
84
95
  * MISSING
85
96
  */ // export { elapsedTime } from "./elapsed-time/elapsed-time.js"
86
- // export { indexBy } from "./index-by/index-by.js"
87
97
  // export { hist } from "./hist/hist.js"
88
98
  // export { protoChain } from "./proto-chain/proto-chain.js"
89
99
  // export { sequence, sequenceWhile } from "./sequence/sequence.js"
@@ -93,20 +103,13 @@ export * from "./is/is.js"; /**
93
103
  // export { isBetween, isBetween as between } from "./is-between/is-between.js"
94
104
  // export { cases } from "./cases/cases.js"
95
105
  // export { pluck } from "./pluck/pluck.js"
96
- // export { values } from "./values/values.js"
97
106
  // export { rename as renameKeys, rename, renameMany } from "./rename/rename.js"
98
- // export { top } from "./top/top.js"
99
- // export { bottom } from "./bottom/bottom.js"
100
- // export { page } from "./page/page.js"
101
- // export { max, maxBy } from "./max/max.js"
102
- // export { min, minBy } from "./min/min.js"
103
107
  // export { move } from "./move/move.js"
104
108
  // export { append, append as concat } from "./append/append.js"
105
109
  // export { prepend } from "./prepend/prepend.js"
106
110
  // export { toggle } from "./toggle/toggle.js"
107
111
  // export { replace, replaceWith } from "./replace/replace.js"
108
112
  // export { hasKey } from "./has-key/has-key.js"
109
- // export { intersect, intersectBy } from "./intersect/intersect.js"
110
113
  // export { gt } from "./gt/gt.js"
111
114
  // export { lt } from "./lt/lt.js"
112
115
  // export { random } from "./random/random.js"
@@ -0,0 +1,48 @@
1
+ import type { EqualityFn } from "../../distinct/by/distinct-by.js";
2
+ type IntersectByFn = {
3
+ <T>(fn: EqualityFn<T>, a: readonly T[], b: readonly T[]): T[];
4
+ <T>(fn: EqualityFn<T>, a: readonly T[]): (b: readonly T[]) => T[];
5
+ };
6
+ /**
7
+ * Set intersection of two arrays, with your own equality function. Returns a
8
+ * new array of the items of the first array that have an equal item in the
9
+ * second array, where the first of each group of equal items wins. Order is
10
+ * preserved - items appear in the order of the first array.
11
+ *
12
+ * Compares each item with each kept item, so the time grows with the square
13
+ * of the input. To compare with SameValueZero, use `intersect`, which is linear.
14
+ *
15
+ * Input is never mutated - always returns a new array.
16
+ *
17
+ * @param {Function} fn - `(item, other) => boolean`, true when equal. A pair
18
+ * is equal only when `fn` returns `true`
19
+ * @param {Array} a - First array
20
+ * @param {Array} [b] - Second array
21
+ *
22
+ * @returns {Array} New array of the items of the first array found in both
23
+ *
24
+ * @example
25
+ * const byId = (a, b) => a.id === b.id
26
+ *
27
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }], [{ id: 2, v: "c" }])
28
+ * // => [{ id: 2, v: "b" }]
29
+ *
30
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])([{ id: 2, v: "c" }])
31
+ * // => [{ id: 2, v: "b" }]
32
+ *
33
+ * // In a pipe - keep the known users among the fetched ones
34
+ * pipe(
35
+ * read("users"),
36
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])
37
+ * )({ users: [{ id: 2, v: "c" }, { id: 3, v: "d" }] })
38
+ * // => [{ id: 2, v: "b" }]
39
+ *
40
+ * @name intersectBy
41
+ * @tag Array
42
+ *
43
+ * @see {@link intersect}
44
+ * @see {@link uniteBy}
45
+ */
46
+ declare const intersectBy: IntersectByFn;
47
+ export { intersectBy };
48
+ export type { IntersectByFn };
@@ -0,0 +1,75 @@
1
+ import { type } from "../../type/type.js";
2
+ const _isEqualPair = (fn, x, y)=>{
3
+ const isEqual = fn(x, y);
4
+ return isEqual === true;
5
+ };
6
+ const _intersectBy = (fn, a, b)=>{
7
+ if (!Array.isArray(b)) {
8
+ throw new TypeError(`@asd14/m/intersectBy: expected 'b' to be 'Array', got '${type(b)}'`);
9
+ }
10
+ const other = b;
11
+ const result = [];
12
+ for(let index = 0, length = a.length; index < length; index++){
13
+ const item = a[index];
14
+ if (other.some((candidate)=>_isEqualPair(fn, item, candidate)) && !result.some((kept)=>_isEqualPair(fn, item, kept))) {
15
+ result.push(item);
16
+ }
17
+ }
18
+ return result;
19
+ };
20
+ /**
21
+ * Set intersection of two arrays, with your own equality function. Returns a
22
+ * new array of the items of the first array that have an equal item in the
23
+ * second array, where the first of each group of equal items wins. Order is
24
+ * preserved - items appear in the order of the first array.
25
+ *
26
+ * Compares each item with each kept item, so the time grows with the square
27
+ * of the input. To compare with SameValueZero, use `intersect`, which is linear.
28
+ *
29
+ * Input is never mutated - always returns a new array.
30
+ *
31
+ * @param {Function} fn - `(item, other) => boolean`, true when equal. A pair
32
+ * is equal only when `fn` returns `true`
33
+ * @param {Array} a - First array
34
+ * @param {Array} [b] - Second array
35
+ *
36
+ * @returns {Array} New array of the items of the first array found in both
37
+ *
38
+ * @example
39
+ * const byId = (a, b) => a.id === b.id
40
+ *
41
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }], [{ id: 2, v: "c" }])
42
+ * // => [{ id: 2, v: "b" }]
43
+ *
44
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])([{ id: 2, v: "c" }])
45
+ * // => [{ id: 2, v: "b" }]
46
+ *
47
+ * // In a pipe - keep the known users among the fetched ones
48
+ * pipe(
49
+ * read("users"),
50
+ * intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])
51
+ * )({ users: [{ id: 2, v: "c" }, { id: 3, v: "d" }] })
52
+ * // => [{ id: 2, v: "b" }]
53
+ *
54
+ * @name intersectBy
55
+ * @tag Array
56
+ *
57
+ * @see {@link intersect}
58
+ * @see {@link uniteBy}
59
+ */ const intersectBy = (...params)=>{
60
+ const fn = params[0];
61
+ const a = params[1];
62
+ if (typeof fn !== "function") {
63
+ throw new TypeError(`@asd14/m/intersectBy: expected 'fn' to be 'Function', got '${type(fn)}'`);
64
+ }
65
+ if (!Array.isArray(a)) {
66
+ throw new TypeError(`@asd14/m/intersectBy: expected 'a' to be 'Array', got '${type(a)}'`);
67
+ }
68
+ // @signature (fn, a) => (b)
69
+ if (params.length === 2) {
70
+ return (b)=>_intersectBy(fn, a, b);
71
+ }
72
+ // @signature (fn, a, b)
73
+ return _intersectBy(fn, a, params[2]);
74
+ };
75
+ export { intersectBy };
@@ -0,0 +1,42 @@
1
+ type IntersectFn = {
2
+ <T>(a: readonly T[], b: readonly T[]): T[];
3
+ <T>(a: readonly T[]): (b: readonly T[]) => T[];
4
+ };
5
+ /**
6
+ * Set intersection of two arrays. Returns a new array of the distinct items
7
+ * present in both arrays, compared with SameValueZero (`NaN` equals `NaN`).
8
+ * Order is preserved - items appear in the order of the first array.
9
+ *
10
+ * Input is never mutated - always returns a new array.
11
+ *
12
+ * @param {Array} a - First array
13
+ * @param {Array} b - Second array
14
+ *
15
+ * @returns {Array} New array of the distinct items present in both arrays
16
+ *
17
+ * @example
18
+ * intersect([1, 2, 3], [3, 2, 5])
19
+ * // => [2, 3]
20
+ *
21
+ * intersect([1, 1, 2])([2, 1])
22
+ * // => [1, 2]
23
+ *
24
+ * intersect([NaN, 1], [NaN, 2])
25
+ * // => [NaN]
26
+ *
27
+ * // In a pipe - keep the known ids among the ids of new items
28
+ * pipe(
29
+ * map(read("id")),
30
+ * intersect([1, 2])
31
+ * )([{ id: 2 }, { id: 3 }])
32
+ * // => [2]
33
+ *
34
+ * @name intersect
35
+ * @tag Array
36
+ *
37
+ * @see {@link intersectBy}
38
+ * @see {@link unite}
39
+ */
40
+ declare const intersect: IntersectFn;
41
+ export { intersect };
42
+ export type { IntersectFn };
@@ -0,0 +1,63 @@
1
+ import { type } from "../type/type.js";
2
+ const _intersect = (a, b)=>{
3
+ if (!Array.isArray(b)) {
4
+ throw new TypeError(`@asd14/m/intersect: expected 'b' to be 'Array', got '${type(b)}'`);
5
+ }
6
+ const inB = new Set(b);
7
+ const seen = new Set();
8
+ const result = [];
9
+ for(let index = 0, length = a.length; index < length; index++){
10
+ const item = a[index];
11
+ if (inB.has(item) && !seen.has(item)) {
12
+ seen.add(item);
13
+ result.push(item);
14
+ }
15
+ }
16
+ return result;
17
+ };
18
+ /**
19
+ * Set intersection of two arrays. Returns a new array of the distinct items
20
+ * present in both arrays, compared with SameValueZero (`NaN` equals `NaN`).
21
+ * Order is preserved - items appear in the order of the first array.
22
+ *
23
+ * Input is never mutated - always returns a new array.
24
+ *
25
+ * @param {Array} a - First array
26
+ * @param {Array} b - Second array
27
+ *
28
+ * @returns {Array} New array of the distinct items present in both arrays
29
+ *
30
+ * @example
31
+ * intersect([1, 2, 3], [3, 2, 5])
32
+ * // => [2, 3]
33
+ *
34
+ * intersect([1, 1, 2])([2, 1])
35
+ * // => [1, 2]
36
+ *
37
+ * intersect([NaN, 1], [NaN, 2])
38
+ * // => [NaN]
39
+ *
40
+ * // In a pipe - keep the known ids among the ids of new items
41
+ * pipe(
42
+ * map(read("id")),
43
+ * intersect([1, 2])
44
+ * )([{ id: 2 }, { id: 3 }])
45
+ * // => [2]
46
+ *
47
+ * @name intersect
48
+ * @tag Array
49
+ *
50
+ * @see {@link intersectBy}
51
+ * @see {@link unite}
52
+ */ const intersect = (...params)=>{
53
+ if (!Array.isArray(params[0])) {
54
+ throw new TypeError(`@asd14/m/intersect: expected 'a' to be 'Array', got '${type(params[0])}'`);
55
+ }
56
+ // @signature (a) => (b)
57
+ if (params.length === 1) {
58
+ return (b)=>_intersect(params[0], b);
59
+ }
60
+ // @signature (a, b)
61
+ return _intersect(params[0], params[1]);
62
+ };
63
+ export { intersect };
@@ -3,11 +3,26 @@ type IsDeepEqualFn = {
3
3
  (left: unknown): (right: unknown) => boolean;
4
4
  };
5
5
  /**
6
- * Check two values for deep equality: arrays and typed arrays by items, `Map`
7
- * by entries, `Set` by keys, `RegExp` by source and flags, a `Date` or any
8
- * object with its own `valueOf` or `toString` by that result, other objects by
9
- * own enumerable keys. Objects of different constructors are never equal. A
10
- * React element skips its `_owner`, which is circular. Two `NaN` are equal.
6
+ * Check two values for deep equality: arrays and typed arrays by items, an
7
+ * `ArrayBuffer`, `SharedArrayBuffer` or `DataView` by bytes, `Map` by entries,
8
+ * `Set` by keys, `RegExp` by source and flags, a `Date` or any object with its
9
+ * own `valueOf` or `toString` by that result, other objects by own enumerable
10
+ * keys. Objects of different constructors are never equal. A React element
11
+ * skips its `_owner`, which is circular. Two `NaN` are equal.
12
+ *
13
+ * State a JS program cannot read is not compared, eg. the contents of a `Blob`,
14
+ * the value of a `Promise` or a `#private` field.
15
+ *
16
+ * To compare secrets, eg. two signatures, use `crypto.timingSafeEqual`:
17
+ * `isDeepEqual` returns at the first difference, so its time leaks where the
18
+ * values differ.
19
+ *
20
+ * It recurses, so the stack limits the depth: deeply nested input throws a
21
+ * `RangeError`. The limit depends on V8's stack size and on whether V8 has
22
+ * optimized the function yet. Run `src/is-deep-equal/bench/depth.ts` to find
23
+ * it on your machine, next to Node's, lodash's and underscore's. `JSON.parse`
24
+ * accepts any depth, so check the depth of untrusted input first, if it can
25
+ * be that deep.
11
26
  *
12
27
  * Input is never mutated.
13
28
  *
@@ -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
- */ const _isDeepEqual = (left, right)=>{
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
- const isReactElement = "$$typeof" in left;
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, `Map`
104
- * by entries, `Set` by keys, `RegExp` by source and flags, a `Date` or any
105
- * object with its own `valueOf` or `toString` by that result, other objects by
106
- * own enumerable keys. Objects of different constructors are never equal. A
107
- * React element skips its `_owner`, which is circular. Two `NaN` are equal.
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
  *