@asd14/m 9.0.0 → 9.1.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/drop/drop.d.ts +38 -0
- package/dist/src/drop/drop.js +56 -0
- package/dist/src/group/group-by.d.ts +16 -7
- package/dist/src/group/group-by.js +10 -1
- package/dist/src/index-by/index-by.d.ts +67 -0
- package/dist/src/index-by/index-by.js +73 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.js +12 -8
- package/dist/src/intersect/by/intersect-by.d.ts +45 -0
- package/dist/src/intersect/by/intersect-by.js +72 -0
- package/dist/src/intersect/intersect.d.ts +42 -0
- package/dist/src/intersect/intersect.js +63 -0
- package/dist/src/max/by/max-by.d.ts +52 -0
- package/dist/src/max/by/max-by.js +69 -0
- package/dist/src/max/max.d.ts +34 -0
- package/dist/src/max/max.js +49 -0
- package/dist/src/min/by/min-by.d.ts +52 -0
- package/dist/src/min/by/min-by.js +69 -0
- package/dist/src/min/min.d.ts +34 -0
- package/dist/src/min/min.js +49 -0
- package/dist/src/read/read.d.ts +1 -0
- package/dist/src/read/read.js +1 -0
- package/dist/src/take/last/take-last.d.ts +38 -0
- package/dist/src/take/last/take-last.js +56 -0
- package/dist/src/take/take.d.ts +38 -0
- package/dist/src/take/take.js +56 -0
- package/dist/src/values/values.d.ts +42 -0
- package/dist/src/values/values.js +46 -0
- package/dist/src/write/write.d.ts +53 -0
- package/dist/src/write/write.js +104 -0
- package/dist/types/object.d.ts +45 -0
- package/docs/SPEC.md +2 -1
- package/package.json +2 -2
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type DropFn = {
|
|
2
|
+
<T>(count: number, input: readonly T[]): T[];
|
|
3
|
+
(count: number): <T>(input: readonly T[]) => T[];
|
|
4
|
+
<T>(input: readonly T[]): T[];
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Remove elements from the start of an array. When called with only an array,
|
|
8
|
+
* removes the first element (count defaults to 1).
|
|
9
|
+
*
|
|
10
|
+
* Input is never mutated - always returns a new array.
|
|
11
|
+
*
|
|
12
|
+
* @param {number} count - Number of elements, a non-negative integer
|
|
13
|
+
* @param {Array} input - Source array
|
|
14
|
+
*
|
|
15
|
+
* @returns {Array} New array with elements removed from the start
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* drop([1, 2, 3])
|
|
19
|
+
* // => [2, 3]
|
|
20
|
+
*
|
|
21
|
+
* drop(2, [1, 2, 3])
|
|
22
|
+
* // => [3]
|
|
23
|
+
*
|
|
24
|
+
* drop(2)([1, 2, 3])
|
|
25
|
+
* // => [3]
|
|
26
|
+
*
|
|
27
|
+
* pipe(drop(1), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
28
|
+
* // => [2, 3]
|
|
29
|
+
*
|
|
30
|
+
* @name drop
|
|
31
|
+
* @tag Array
|
|
32
|
+
*
|
|
33
|
+
* @see {@link dropLast}
|
|
34
|
+
* @see {@link take}
|
|
35
|
+
*/
|
|
36
|
+
declare const drop: DropFn;
|
|
37
|
+
export { drop };
|
|
38
|
+
export type { DropFn };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type } from "../type/type.js";
|
|
2
|
+
const _drop = (count, input)=>{
|
|
3
|
+
if (!Array.isArray(input)) {
|
|
4
|
+
throw new TypeError(`@asd14/m/drop: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
5
|
+
}
|
|
6
|
+
return count >= input.length ? [] : input.slice(count);
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Remove elements from the start of an array. When called with only an array,
|
|
10
|
+
* removes the first element (count defaults to 1).
|
|
11
|
+
*
|
|
12
|
+
* Input is never mutated - always returns a new array.
|
|
13
|
+
*
|
|
14
|
+
* @param {number} count - Number of elements, a non-negative integer
|
|
15
|
+
* @param {Array} input - Source array
|
|
16
|
+
*
|
|
17
|
+
* @returns {Array} New array with elements removed from the start
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* drop([1, 2, 3])
|
|
21
|
+
* // => [2, 3]
|
|
22
|
+
*
|
|
23
|
+
* drop(2, [1, 2, 3])
|
|
24
|
+
* // => [3]
|
|
25
|
+
*
|
|
26
|
+
* drop(2)([1, 2, 3])
|
|
27
|
+
* // => [3]
|
|
28
|
+
*
|
|
29
|
+
* pipe(drop(1), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
30
|
+
* // => [2, 3]
|
|
31
|
+
*
|
|
32
|
+
* @name drop
|
|
33
|
+
* @tag Array
|
|
34
|
+
*
|
|
35
|
+
* @see {@link dropLast}
|
|
36
|
+
* @see {@link take}
|
|
37
|
+
*/ const drop = (...params)=>{
|
|
38
|
+
// @signature (input)
|
|
39
|
+
if (Array.isArray(params[0])) {
|
|
40
|
+
return _drop(1, params[0]);
|
|
41
|
+
}
|
|
42
|
+
const count = params[0];
|
|
43
|
+
if (typeof count !== "number") {
|
|
44
|
+
throw new TypeError(`@asd14/m/drop: expected 'count' to be 'Number', got '${type(count)}'`);
|
|
45
|
+
}
|
|
46
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
47
|
+
throw new TypeError(`@asd14/m/drop: expected 'count' to be 'NonNegativeInteger', got '${String(count)}'`);
|
|
48
|
+
}
|
|
49
|
+
// @signature (count, input)
|
|
50
|
+
if (params.length === 2) {
|
|
51
|
+
return _drop(count, params[1]);
|
|
52
|
+
}
|
|
53
|
+
// @signature (count) => (input)
|
|
54
|
+
return (input)=>_drop(count, input);
|
|
55
|
+
};
|
|
56
|
+
export { drop };
|
|
@@ -3,22 +3,22 @@ type GroupByFn = {
|
|
|
3
3
|
<T1, T2, T3>(fns: [
|
|
4
4
|
UnaryMorphism<T1, T2>,
|
|
5
5
|
UnaryMorphism<T2, T3>,
|
|
6
|
-
UnaryMorphism<T3, string>
|
|
6
|
+
UnaryMorphism<T3, string | number>
|
|
7
7
|
], input: readonly T1[]): Record<string, T1[]>;
|
|
8
8
|
<T1, T2, T3>(fns: [
|
|
9
9
|
UnaryMorphism<T1, T2>,
|
|
10
10
|
UnaryMorphism<T2, T3>,
|
|
11
|
-
UnaryMorphism<T3, string>
|
|
11
|
+
UnaryMorphism<T3, string | number>
|
|
12
12
|
]): (input: readonly T1[]) => Record<string, T1[]>;
|
|
13
|
-
<T1, T2>(fns: [UnaryMorphism<T1, T2>, UnaryMorphism<T2, string>], input: readonly T1[]): Record<string, T1[]>;
|
|
14
|
-
<T1, T2>(fns: [UnaryMorphism<T1, T2>, UnaryMorphism<T2, string>]): (input: readonly T1[]) => Record<string, T1[]>;
|
|
15
|
-
<T>(fn: (item: T) => string, input: readonly T[]): Record<string, T[]>;
|
|
16
|
-
<T>(fn: (item: T) => string): (input: readonly T[]) => Record<string, T[]>;
|
|
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
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Group the items of an array by the key a function returns for each. Returns
|
|
20
20
|
* an object where each key holds the array of its items, in input order. The
|
|
21
|
-
*
|
|
21
|
+
* function returns a string or a number, and the key is its string form.
|
|
22
22
|
*
|
|
23
23
|
* Only own keys count, so `"constructor"`, `"toString"` and `"__proto__"` work
|
|
24
24
|
* as group keys.
|
|
@@ -44,6 +44,15 @@ type GroupByFn = {
|
|
|
44
44
|
* groupBy(n => n % 2 === 0 ? "even" : "odd")([1, 2, 3, 4, 5])
|
|
45
45
|
* // => { odd: [1, 3, 5], even: [2, 4] }
|
|
46
46
|
*
|
|
47
|
+
* groupBy(read("level"), [{ level: 1 }, { level: 2 }, { level: 1 }])
|
|
48
|
+
* // => { "1": [{ level: 1 }, { level: 1 }], "2": [{ level: 2 }] }
|
|
49
|
+
*
|
|
50
|
+
* // A String step turns any key into a string. To split by a boolean,
|
|
51
|
+
* // `partition(read("active"))` is the intended tool: it returns both
|
|
52
|
+
* // groups as a pair
|
|
53
|
+
* groupBy([read("active"), String], [{ active: true }, { active: false }])
|
|
54
|
+
* // => { true: [{ active: true }], false: [{ active: false }] }
|
|
55
|
+
*
|
|
47
56
|
* // A pipe array builds the key in steps
|
|
48
57
|
* groupBy([read("role"), role => role.toUpperCase()], users)
|
|
49
58
|
* // => { ADMIN: [...], USER: [...] }
|
|
@@ -22,7 +22,7 @@ const _groupBy = (fn, input)=>{
|
|
|
22
22
|
/**
|
|
23
23
|
* Group the items of an array by the key a function returns for each. Returns
|
|
24
24
|
* an object where each key holds the array of its items, in input order. The
|
|
25
|
-
*
|
|
25
|
+
* function returns a string or a number, and the key is its string form.
|
|
26
26
|
*
|
|
27
27
|
* Only own keys count, so `"constructor"`, `"toString"` and `"__proto__"` work
|
|
28
28
|
* as group keys.
|
|
@@ -48,6 +48,15 @@ const _groupBy = (fn, input)=>{
|
|
|
48
48
|
* groupBy(n => n % 2 === 0 ? "even" : "odd")([1, 2, 3, 4, 5])
|
|
49
49
|
* // => { odd: [1, 3, 5], even: [2, 4] }
|
|
50
50
|
*
|
|
51
|
+
* groupBy(read("level"), [{ level: 1 }, { level: 2 }, { level: 1 }])
|
|
52
|
+
* // => { "1": [{ level: 1 }, { level: 1 }], "2": [{ level: 2 }] }
|
|
53
|
+
*
|
|
54
|
+
* // A String step turns any key into a string. To split by a boolean,
|
|
55
|
+
* // `partition(read("active"))` is the intended tool: it returns both
|
|
56
|
+
* // groups as a pair
|
|
57
|
+
* groupBy([read("active"), String], [{ active: true }, { active: false }])
|
|
58
|
+
* // => { true: [{ active: true }], false: [{ active: false }] }
|
|
59
|
+
*
|
|
51
60
|
* // A pipe array builds the key in steps
|
|
52
61
|
* groupBy([read("role"), role => role.toUpperCase()], users)
|
|
53
62
|
* // => { ADMIN: [...], USER: [...] }
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
* @example
|
|
36
|
+
* indexBy(user => user.id, [
|
|
37
|
+
* { id: 1, name: "alice" },
|
|
38
|
+
* { id: 2, name: "bob" }
|
|
39
|
+
* ])
|
|
40
|
+
* // => { 1: { id: 1, name: "alice" }, 2: { id: 2, name: "bob" } }
|
|
41
|
+
*
|
|
42
|
+
* // The last item wins on a duplicate key
|
|
43
|
+
* indexBy(user => user.role)([
|
|
44
|
+
* { name: "alice", role: "admin" },
|
|
45
|
+
* { name: "carol", role: "admin" }
|
|
46
|
+
* ])
|
|
47
|
+
* // => { admin: { name: "carol", role: "admin" } }
|
|
48
|
+
*
|
|
49
|
+
* // A pipe array builds the key in steps
|
|
50
|
+
* indexBy([read("name"), name => name.toUpperCase()], users)
|
|
51
|
+
* // => { ALICE: {...}, BOB: {...} }
|
|
52
|
+
*
|
|
53
|
+
* // In a pipe - list the keys
|
|
54
|
+
* pipe(
|
|
55
|
+
* indexBy(n => n % 3),
|
|
56
|
+
* keys
|
|
57
|
+
* )([1, 2, 3, 4, 5])
|
|
58
|
+
* // => ["0", "1", "2"]
|
|
59
|
+
*
|
|
60
|
+
* @name indexBy
|
|
61
|
+
* @tag Array
|
|
62
|
+
*
|
|
63
|
+
* @see {@link groupBy}
|
|
64
|
+
*/
|
|
65
|
+
declare const indexBy: IndexByFn;
|
|
66
|
+
export { indexBy };
|
|
67
|
+
export type { IndexByFn };
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
_setOwn(result, String(fn(item)), item);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Index the items of an array by the key a function returns for each. Returns
|
|
17
|
+
* an object where each key holds one item. The function returns a string or a
|
|
18
|
+
* number, and the key is its string form.
|
|
19
|
+
*
|
|
20
|
+
* On a duplicate key the last item wins.
|
|
21
|
+
*
|
|
22
|
+
* Only own keys count, so `"constructor"`, `"toString"` and `"__proto__"` work
|
|
23
|
+
* as keys.
|
|
24
|
+
*
|
|
25
|
+
* Input is never mutated - always returns a new object.
|
|
26
|
+
*
|
|
27
|
+
* @param {Function|Array} fn - `(item) => key`, or a pipe array of functions
|
|
28
|
+
* @param {Array} [input] - Array to index
|
|
29
|
+
*
|
|
30
|
+
* @returns {Object} Object of items, one for each key
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* indexBy(user => user.id, [
|
|
34
|
+
* { id: 1, name: "alice" },
|
|
35
|
+
* { id: 2, name: "bob" }
|
|
36
|
+
* ])
|
|
37
|
+
* // => { 1: { id: 1, name: "alice" }, 2: { id: 2, name: "bob" } }
|
|
38
|
+
*
|
|
39
|
+
* // The last item wins on a duplicate key
|
|
40
|
+
* indexBy(user => user.role)([
|
|
41
|
+
* { name: "alice", role: "admin" },
|
|
42
|
+
* { name: "carol", role: "admin" }
|
|
43
|
+
* ])
|
|
44
|
+
* // => { admin: { name: "carol", role: "admin" } }
|
|
45
|
+
*
|
|
46
|
+
* // A pipe array builds the key in steps
|
|
47
|
+
* indexBy([read("name"), name => name.toUpperCase()], users)
|
|
48
|
+
* // => { ALICE: {...}, BOB: {...} }
|
|
49
|
+
*
|
|
50
|
+
* // In a pipe - list the keys
|
|
51
|
+
* pipe(
|
|
52
|
+
* indexBy(n => n % 3),
|
|
53
|
+
* keys
|
|
54
|
+
* )([1, 2, 3, 4, 5])
|
|
55
|
+
* // => ["0", "1", "2"]
|
|
56
|
+
*
|
|
57
|
+
* @name indexBy
|
|
58
|
+
* @tag Array
|
|
59
|
+
*
|
|
60
|
+
* @see {@link groupBy}
|
|
61
|
+
*/ const indexBy = (...params)=>{
|
|
62
|
+
const fn = _toFunction(params[0], {
|
|
63
|
+
fnName: "indexBy",
|
|
64
|
+
paramName: "fn"
|
|
65
|
+
});
|
|
66
|
+
// @signature (fn) => (input)
|
|
67
|
+
if (params.length === 1) {
|
|
68
|
+
return (input)=>_indexBy(fn, input);
|
|
69
|
+
}
|
|
70
|
+
// @signature (fn, input)
|
|
71
|
+
return _indexBy(fn, params[1]);
|
|
72
|
+
};
|
|
73
|
+
export { indexBy };
|
package/dist/src/index.d.ts
CHANGED
|
@@ -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,6 +52,8 @@ 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";
|
|
@@ -61,6 +69,8 @@ export * from "./pick/pick.js";
|
|
|
61
69
|
export * from "./read/read.js";
|
|
62
70
|
export * from "./unflatten/unflatten.js";
|
|
63
71
|
export * from "./update/update.js";
|
|
72
|
+
export * from "./values/values.js";
|
|
73
|
+
export * from "./write/write.js";
|
|
64
74
|
export * from "./escape/escape.js";
|
|
65
75
|
export * from "./escape/html/escape-html.js";
|
|
66
76
|
export * from "./escape/reg-exp/escape-reg-exp.js";
|
|
@@ -72,6 +82,8 @@ export * from "./to-string/to-string.js";
|
|
|
72
82
|
export * from "./trim/trim.js";
|
|
73
83
|
export * from "./dec/dec.js";
|
|
74
84
|
export * from "./inc/inc.js";
|
|
85
|
+
export * from "./max/max.js";
|
|
86
|
+
export * from "./min/min.js";
|
|
75
87
|
export * from "./sum/sum.js";
|
|
76
88
|
export * from "./is/is.js";
|
|
77
89
|
/**
|
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,6 +55,8 @@ 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";
|
|
@@ -65,6 +73,8 @@ export * from "./pick/pick.js";
|
|
|
65
73
|
export * from "./read/read.js";
|
|
66
74
|
export * from "./unflatten/unflatten.js";
|
|
67
75
|
export * from "./update/update.js";
|
|
76
|
+
export * from "./values/values.js";
|
|
77
|
+
export * from "./write/write.js";
|
|
68
78
|
// String
|
|
69
79
|
export * from "./escape/escape.js";
|
|
70
80
|
export * from "./escape/html/escape-html.js";
|
|
@@ -78,12 +88,13 @@ export * from "./trim/trim.js";
|
|
|
78
88
|
// Number
|
|
79
89
|
export * from "./dec/dec.js";
|
|
80
90
|
export * from "./inc/inc.js";
|
|
91
|
+
export * from "./max/max.js";
|
|
92
|
+
export * from "./min/min.js";
|
|
81
93
|
export * from "./sum/sum.js";
|
|
82
94
|
// Type guard
|
|
83
95
|
export * from "./is/is.js"; /**
|
|
84
96
|
* MISSING
|
|
85
97
|
*/ // export { elapsedTime } from "./elapsed-time/elapsed-time.js"
|
|
86
|
-
// export { indexBy } from "./index-by/index-by.js"
|
|
87
98
|
// export { hist } from "./hist/hist.js"
|
|
88
99
|
// export { protoChain } from "./proto-chain/proto-chain.js"
|
|
89
100
|
// export { sequence, sequenceWhile } from "./sequence/sequence.js"
|
|
@@ -93,20 +104,13 @@ export * from "./is/is.js"; /**
|
|
|
93
104
|
// export { isBetween, isBetween as between } from "./is-between/is-between.js"
|
|
94
105
|
// export { cases } from "./cases/cases.js"
|
|
95
106
|
// export { pluck } from "./pluck/pluck.js"
|
|
96
|
-
// export { values } from "./values/values.js"
|
|
97
107
|
// 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
108
|
// export { move } from "./move/move.js"
|
|
104
109
|
// export { append, append as concat } from "./append/append.js"
|
|
105
110
|
// export { prepend } from "./prepend/prepend.js"
|
|
106
111
|
// export { toggle } from "./toggle/toggle.js"
|
|
107
112
|
// export { replace, replaceWith } from "./replace/replace.js"
|
|
108
113
|
// export { hasKey } from "./has-key/has-key.js"
|
|
109
|
-
// export { intersect, intersectBy } from "./intersect/intersect.js"
|
|
110
114
|
// export { gt } from "./gt/gt.js"
|
|
111
115
|
// export { lt } from "./lt/lt.js"
|
|
112
116
|
// export { random } from "./random/random.js"
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
* Input is never mutated - always returns a new array.
|
|
13
|
+
*
|
|
14
|
+
* @param {Function} fn - `(item, other) => boolean`, true when equal. A pair
|
|
15
|
+
* is equal only when `fn` returns `true`
|
|
16
|
+
* @param {Array} a - First array
|
|
17
|
+
* @param {Array} [b] - Second array
|
|
18
|
+
*
|
|
19
|
+
* @returns {Array} New array of the items of the first array found in both
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const byId = (a, b) => a.id === b.id
|
|
23
|
+
*
|
|
24
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }], [{ id: 2, v: "c" }])
|
|
25
|
+
* // => [{ id: 2, v: "b" }]
|
|
26
|
+
*
|
|
27
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])([{ id: 2, v: "c" }])
|
|
28
|
+
* // => [{ id: 2, v: "b" }]
|
|
29
|
+
*
|
|
30
|
+
* // In a pipe - keep the known users among the fetched ones
|
|
31
|
+
* pipe(
|
|
32
|
+
* read("users"),
|
|
33
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])
|
|
34
|
+
* )({ users: [{ id: 2, v: "c" }, { id: 3, v: "d" }] })
|
|
35
|
+
* // => [{ id: 2, v: "b" }]
|
|
36
|
+
*
|
|
37
|
+
* @name intersectBy
|
|
38
|
+
* @tag Array
|
|
39
|
+
*
|
|
40
|
+
* @see {@link intersect}
|
|
41
|
+
* @see {@link uniteBy}
|
|
42
|
+
*/
|
|
43
|
+
declare const intersectBy: IntersectByFn;
|
|
44
|
+
export { intersectBy };
|
|
45
|
+
export type { IntersectByFn };
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
* Input is never mutated - always returns a new array.
|
|
27
|
+
*
|
|
28
|
+
* @param {Function} fn - `(item, other) => boolean`, true when equal. A pair
|
|
29
|
+
* is equal only when `fn` returns `true`
|
|
30
|
+
* @param {Array} a - First array
|
|
31
|
+
* @param {Array} [b] - Second array
|
|
32
|
+
*
|
|
33
|
+
* @returns {Array} New array of the items of the first array found in both
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const byId = (a, b) => a.id === b.id
|
|
37
|
+
*
|
|
38
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }], [{ id: 2, v: "c" }])
|
|
39
|
+
* // => [{ id: 2, v: "b" }]
|
|
40
|
+
*
|
|
41
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])([{ id: 2, v: "c" }])
|
|
42
|
+
* // => [{ id: 2, v: "b" }]
|
|
43
|
+
*
|
|
44
|
+
* // In a pipe - keep the known users among the fetched ones
|
|
45
|
+
* pipe(
|
|
46
|
+
* read("users"),
|
|
47
|
+
* intersectBy(byId, [{ id: 1, v: "a" }, { id: 2, v: "b" }])
|
|
48
|
+
* )({ users: [{ id: 2, v: "c" }, { id: 3, v: "d" }] })
|
|
49
|
+
* // => [{ id: 2, v: "b" }]
|
|
50
|
+
*
|
|
51
|
+
* @name intersectBy
|
|
52
|
+
* @tag Array
|
|
53
|
+
*
|
|
54
|
+
* @see {@link intersect}
|
|
55
|
+
* @see {@link uniteBy}
|
|
56
|
+
*/ const intersectBy = (...params)=>{
|
|
57
|
+
const fn = params[0];
|
|
58
|
+
const a = params[1];
|
|
59
|
+
if (typeof fn !== "function") {
|
|
60
|
+
throw new TypeError(`@asd14/m/intersectBy: expected 'fn' to be 'Function', got '${type(fn)}'`);
|
|
61
|
+
}
|
|
62
|
+
if (!Array.isArray(a)) {
|
|
63
|
+
throw new TypeError(`@asd14/m/intersectBy: expected 'a' to be 'Array', got '${type(a)}'`);
|
|
64
|
+
}
|
|
65
|
+
// @signature (fn, a) => (b)
|
|
66
|
+
if (params.length === 2) {
|
|
67
|
+
return (b)=>_intersectBy(fn, a, b);
|
|
68
|
+
}
|
|
69
|
+
// @signature (fn, a, b)
|
|
70
|
+
return _intersectBy(fn, a, params[2]);
|
|
71
|
+
};
|
|
72
|
+
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 };
|