@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,52 @@
|
|
|
1
|
+
import type { UnaryMorphism } from "../../../types/function.js";
|
|
2
|
+
import type { PipeMorphisms1, PipeMorphisms2, PipeMorphisms3, PipeMorphisms4 } from "../../pipe/pipe.js";
|
|
3
|
+
type MaxByFn = {
|
|
4
|
+
<T1, T2, T3, T4>(fns: PipeMorphisms4<T1, T2, T3, T4, number>, input: readonly T1[]): T1 | undefined;
|
|
5
|
+
<T1, T2, T3, T4>(fns: PipeMorphisms4<T1, T2, T3, T4, number>): (input: readonly T1[]) => T1 | undefined;
|
|
6
|
+
<T1, T2, T3>(fns: PipeMorphisms3<T1, T2, T3, number>, input: readonly T1[]): T1 | undefined;
|
|
7
|
+
<T1, T2, T3>(fns: PipeMorphisms3<T1, T2, T3, number>): (input: readonly T1[]) => T1 | undefined;
|
|
8
|
+
<T1, T2>(fns: PipeMorphisms2<T1, T2, number>, input: readonly T1[]): T1 | undefined;
|
|
9
|
+
<T1, T2>(fns: PipeMorphisms2<T1, T2, number>): (input: readonly T1[]) => T1 | undefined;
|
|
10
|
+
<T1>(fns: PipeMorphisms1<T1, number>, input: readonly T1[]): T1 | undefined;
|
|
11
|
+
<T1>(fns: PipeMorphisms1<T1, number>): (input: readonly T1[]) => T1 | undefined;
|
|
12
|
+
<T>(fn: UnaryMorphism<T, number>, input: readonly T[]): T | undefined;
|
|
13
|
+
<T>(fn: UnaryMorphism<T, number>): (input: readonly T[]) => T | undefined;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Find the item whose `fn` result is the largest. The first item wins a tie.
|
|
17
|
+
* Skips an item whose result is `NaN`, and returns `undefined` when no item is
|
|
18
|
+
* left. Throws on a result that is not a number.
|
|
19
|
+
*
|
|
20
|
+
* Input is never mutated.
|
|
21
|
+
*
|
|
22
|
+
* @param {Function|Array} fn - Function or pipe chain that returns a number
|
|
23
|
+
* @param {readonly Array} input - Array to search
|
|
24
|
+
*
|
|
25
|
+
* @returns {*|undefined} The item with the largest result, or `undefined`
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* maxBy(user => user.age, [{ age: 30 }, { age: 41 }, { age: 25 }])
|
|
29
|
+
* // => { age: 41 }
|
|
30
|
+
*
|
|
31
|
+
* maxBy(user => user.age)([])
|
|
32
|
+
* // => undefined
|
|
33
|
+
*
|
|
34
|
+
* maxBy([read("stats"), read("score")], [{ stats: { score: 1 } }, { stats: { score: 9 } }])
|
|
35
|
+
* // => { stats: { score: 9 } }
|
|
36
|
+
*
|
|
37
|
+
* pipe(
|
|
38
|
+
* filter(user => user.isActive),
|
|
39
|
+
* maxBy(user => user.age)
|
|
40
|
+
* )(users)
|
|
41
|
+
* // => { isActive: true, age: 41 }
|
|
42
|
+
*
|
|
43
|
+
* @name maxBy
|
|
44
|
+
* @tag Array
|
|
45
|
+
*
|
|
46
|
+
* @see {@link max}
|
|
47
|
+
* @see {@link minBy}
|
|
48
|
+
* @see {@link sortBy}
|
|
49
|
+
*/
|
|
50
|
+
declare const maxBy: MaxByFn;
|
|
51
|
+
export { maxBy };
|
|
52
|
+
export type { MaxByFn };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type } from "../../type/type.js";
|
|
2
|
+
import { _toFunction } from "../../utils/to-function.js";
|
|
3
|
+
const _maxBy = (fn, input)=>{
|
|
4
|
+
if (!Array.isArray(input)) {
|
|
5
|
+
throw new TypeError(`@asd14/m/maxBy: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
6
|
+
}
|
|
7
|
+
let result;
|
|
8
|
+
let best;
|
|
9
|
+
for(let i = 0, length = input.length; i < length; i++){
|
|
10
|
+
const item = input[i];
|
|
11
|
+
const score = fn(item);
|
|
12
|
+
if (typeof score !== "number") {
|
|
13
|
+
throw new TypeError(`@asd14/m/maxBy: expected 'fn(input[${i}])' to be 'Number', got '${type(score)}'`);
|
|
14
|
+
}
|
|
15
|
+
// NaN cannot be ordered, so it never wins. The first item wins a tie
|
|
16
|
+
if (!Number.isNaN(score) && (best === undefined || score > best)) {
|
|
17
|
+
best = score;
|
|
18
|
+
result = item;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Find the item whose `fn` result is the largest. The first item wins a tie.
|
|
25
|
+
* Skips an item whose result is `NaN`, and returns `undefined` when no item is
|
|
26
|
+
* left. Throws on a result that is not a number.
|
|
27
|
+
*
|
|
28
|
+
* Input is never mutated.
|
|
29
|
+
*
|
|
30
|
+
* @param {Function|Array} fn - Function or pipe chain that returns a number
|
|
31
|
+
* @param {readonly Array} input - Array to search
|
|
32
|
+
*
|
|
33
|
+
* @returns {*|undefined} The item with the largest result, or `undefined`
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* maxBy(user => user.age, [{ age: 30 }, { age: 41 }, { age: 25 }])
|
|
37
|
+
* // => { age: 41 }
|
|
38
|
+
*
|
|
39
|
+
* maxBy(user => user.age)([])
|
|
40
|
+
* // => undefined
|
|
41
|
+
*
|
|
42
|
+
* maxBy([read("stats"), read("score")], [{ stats: { score: 1 } }, { stats: { score: 9 } }])
|
|
43
|
+
* // => { stats: { score: 9 } }
|
|
44
|
+
*
|
|
45
|
+
* pipe(
|
|
46
|
+
* filter(user => user.isActive),
|
|
47
|
+
* maxBy(user => user.age)
|
|
48
|
+
* )(users)
|
|
49
|
+
* // => { isActive: true, age: 41 }
|
|
50
|
+
*
|
|
51
|
+
* @name maxBy
|
|
52
|
+
* @tag Array
|
|
53
|
+
*
|
|
54
|
+
* @see {@link max}
|
|
55
|
+
* @see {@link minBy}
|
|
56
|
+
* @see {@link sortBy}
|
|
57
|
+
*/ const maxBy = (...params)=>{
|
|
58
|
+
const fn = _toFunction(params[0], {
|
|
59
|
+
fnName: "maxBy",
|
|
60
|
+
paramName: "fn"
|
|
61
|
+
});
|
|
62
|
+
// @signature (fn) => (input)
|
|
63
|
+
if (params.length === 1) {
|
|
64
|
+
return (input)=>_maxBy(fn, input);
|
|
65
|
+
}
|
|
66
|
+
// @signature (fn, input)
|
|
67
|
+
return _maxBy(fn, params[1]);
|
|
68
|
+
};
|
|
69
|
+
export { maxBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type MaxFn = (input: readonly number[]) => number | undefined;
|
|
2
|
+
/**
|
|
3
|
+
* Find the largest number of an array. Skips a `NaN` item, and returns
|
|
4
|
+
* `undefined` when no number is left. Throws on an item that is not a number.
|
|
5
|
+
*
|
|
6
|
+
* Input is never mutated.
|
|
7
|
+
*
|
|
8
|
+
* @param {readonly Array} input - Array of numbers
|
|
9
|
+
*
|
|
10
|
+
* @returns {number|undefined} The largest number, or `undefined`
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* max([3, 1, 4, 1, 5])
|
|
14
|
+
* // => 5
|
|
15
|
+
*
|
|
16
|
+
* max([])
|
|
17
|
+
* // => undefined
|
|
18
|
+
*
|
|
19
|
+
* max([1, NaN, 2])
|
|
20
|
+
* // => 2
|
|
21
|
+
*
|
|
22
|
+
* pipe(map(read("score")), max)([{ score: 1 }, { score: 7 }])
|
|
23
|
+
* // => 7
|
|
24
|
+
*
|
|
25
|
+
* @name max
|
|
26
|
+
* @tag Number
|
|
27
|
+
*
|
|
28
|
+
* @see {@link min}
|
|
29
|
+
* @see {@link maxBy}
|
|
30
|
+
* @see {@link sum}
|
|
31
|
+
*/
|
|
32
|
+
declare const max: MaxFn;
|
|
33
|
+
export { max };
|
|
34
|
+
export type { MaxFn };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type } from "../type/type.js";
|
|
2
|
+
const _max = (input)=>{
|
|
3
|
+
if (!Array.isArray(input)) {
|
|
4
|
+
throw new TypeError(`@asd14/m/max: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
5
|
+
}
|
|
6
|
+
let result;
|
|
7
|
+
for(let i = 0, length = input.length; i < length; i++){
|
|
8
|
+
const item = input[i];
|
|
9
|
+
if (typeof item !== "number") {
|
|
10
|
+
throw new TypeError(`@asd14/m/max: expected 'input[${i}]' to be 'Number', got '${type(item)}'`);
|
|
11
|
+
}
|
|
12
|
+
// NaN cannot be ordered, so it never wins
|
|
13
|
+
if (!Number.isNaN(item) && (result === undefined || item > result)) {
|
|
14
|
+
result = item;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Find the largest number of an array. Skips a `NaN` item, and returns
|
|
21
|
+
* `undefined` when no number is left. Throws on an item that is not a number.
|
|
22
|
+
*
|
|
23
|
+
* Input is never mutated.
|
|
24
|
+
*
|
|
25
|
+
* @param {readonly Array} input - Array of numbers
|
|
26
|
+
*
|
|
27
|
+
* @returns {number|undefined} The largest number, or `undefined`
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* max([3, 1, 4, 1, 5])
|
|
31
|
+
* // => 5
|
|
32
|
+
*
|
|
33
|
+
* max([])
|
|
34
|
+
* // => undefined
|
|
35
|
+
*
|
|
36
|
+
* max([1, NaN, 2])
|
|
37
|
+
* // => 2
|
|
38
|
+
*
|
|
39
|
+
* pipe(map(read("score")), max)([{ score: 1 }, { score: 7 }])
|
|
40
|
+
* // => 7
|
|
41
|
+
*
|
|
42
|
+
* @name max
|
|
43
|
+
* @tag Number
|
|
44
|
+
*
|
|
45
|
+
* @see {@link min}
|
|
46
|
+
* @see {@link maxBy}
|
|
47
|
+
* @see {@link sum}
|
|
48
|
+
*/ const max = _max;
|
|
49
|
+
export { max };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { UnaryMorphism } from "../../../types/function.js";
|
|
2
|
+
import type { PipeMorphisms1, PipeMorphisms2, PipeMorphisms3, PipeMorphisms4 } from "../../pipe/pipe.js";
|
|
3
|
+
type MinByFn = {
|
|
4
|
+
<T1, T2, T3, T4>(fns: PipeMorphisms4<T1, T2, T3, T4, number>, input: readonly T1[]): T1 | undefined;
|
|
5
|
+
<T1, T2, T3, T4>(fns: PipeMorphisms4<T1, T2, T3, T4, number>): (input: readonly T1[]) => T1 | undefined;
|
|
6
|
+
<T1, T2, T3>(fns: PipeMorphisms3<T1, T2, T3, number>, input: readonly T1[]): T1 | undefined;
|
|
7
|
+
<T1, T2, T3>(fns: PipeMorphisms3<T1, T2, T3, number>): (input: readonly T1[]) => T1 | undefined;
|
|
8
|
+
<T1, T2>(fns: PipeMorphisms2<T1, T2, number>, input: readonly T1[]): T1 | undefined;
|
|
9
|
+
<T1, T2>(fns: PipeMorphisms2<T1, T2, number>): (input: readonly T1[]) => T1 | undefined;
|
|
10
|
+
<T1>(fns: PipeMorphisms1<T1, number>, input: readonly T1[]): T1 | undefined;
|
|
11
|
+
<T1>(fns: PipeMorphisms1<T1, number>): (input: readonly T1[]) => T1 | undefined;
|
|
12
|
+
<T>(fn: UnaryMorphism<T, number>, input: readonly T[]): T | undefined;
|
|
13
|
+
<T>(fn: UnaryMorphism<T, number>): (input: readonly T[]) => T | undefined;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Find the item whose `fn` result is the smallest. The first item wins a tie.
|
|
17
|
+
* Skips an item whose result is `NaN`, and returns `undefined` when no item is
|
|
18
|
+
* left. Throws on a result that is not a number.
|
|
19
|
+
*
|
|
20
|
+
* Input is never mutated.
|
|
21
|
+
*
|
|
22
|
+
* @param {Function|Array} fn - Function or pipe chain that returns a number
|
|
23
|
+
* @param {readonly Array} input - Array to search
|
|
24
|
+
*
|
|
25
|
+
* @returns {*|undefined} The item with the smallest result, or `undefined`
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* minBy(user => user.age, [{ age: 30 }, { age: 25 }, { age: 41 }])
|
|
29
|
+
* // => { age: 25 }
|
|
30
|
+
*
|
|
31
|
+
* minBy(user => user.age)([])
|
|
32
|
+
* // => undefined
|
|
33
|
+
*
|
|
34
|
+
* minBy([read("stats"), read("score")], [{ stats: { score: 1 } }, { stats: { score: 9 } }])
|
|
35
|
+
* // => { stats: { score: 1 } }
|
|
36
|
+
*
|
|
37
|
+
* pipe(
|
|
38
|
+
* filter(user => user.isActive),
|
|
39
|
+
* minBy(user => user.age)
|
|
40
|
+
* )(users)
|
|
41
|
+
* // => { isActive: true, age: 25 }
|
|
42
|
+
*
|
|
43
|
+
* @name minBy
|
|
44
|
+
* @tag Array
|
|
45
|
+
*
|
|
46
|
+
* @see {@link max}
|
|
47
|
+
* @see {@link maxBy}
|
|
48
|
+
* @see {@link sortBy}
|
|
49
|
+
*/
|
|
50
|
+
declare const minBy: MinByFn;
|
|
51
|
+
export { minBy };
|
|
52
|
+
export type { MinByFn };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type } from "../../type/type.js";
|
|
2
|
+
import { _toFunction } from "../../utils/to-function.js";
|
|
3
|
+
const _minBy = (fn, input)=>{
|
|
4
|
+
if (!Array.isArray(input)) {
|
|
5
|
+
throw new TypeError(`@asd14/m/minBy: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
6
|
+
}
|
|
7
|
+
let result;
|
|
8
|
+
let best;
|
|
9
|
+
for(let i = 0, length = input.length; i < length; i++){
|
|
10
|
+
const item = input[i];
|
|
11
|
+
const score = fn(item);
|
|
12
|
+
if (typeof score !== "number") {
|
|
13
|
+
throw new TypeError(`@asd14/m/minBy: expected 'fn(input[${i}])' to be 'Number', got '${type(score)}'`);
|
|
14
|
+
}
|
|
15
|
+
// NaN cannot be ordered, so it never wins. The first item wins a tie
|
|
16
|
+
if (!Number.isNaN(score) && (best === undefined || score < best)) {
|
|
17
|
+
best = score;
|
|
18
|
+
result = item;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Find the item whose `fn` result is the smallest. The first item wins a tie.
|
|
25
|
+
* Skips an item whose result is `NaN`, and returns `undefined` when no item is
|
|
26
|
+
* left. Throws on a result that is not a number.
|
|
27
|
+
*
|
|
28
|
+
* Input is never mutated.
|
|
29
|
+
*
|
|
30
|
+
* @param {Function|Array} fn - Function or pipe chain that returns a number
|
|
31
|
+
* @param {readonly Array} input - Array to search
|
|
32
|
+
*
|
|
33
|
+
* @returns {*|undefined} The item with the smallest result, or `undefined`
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* minBy(user => user.age, [{ age: 30 }, { age: 25 }, { age: 41 }])
|
|
37
|
+
* // => { age: 25 }
|
|
38
|
+
*
|
|
39
|
+
* minBy(user => user.age)([])
|
|
40
|
+
* // => undefined
|
|
41
|
+
*
|
|
42
|
+
* minBy([read("stats"), read("score")], [{ stats: { score: 1 } }, { stats: { score: 9 } }])
|
|
43
|
+
* // => { stats: { score: 1 } }
|
|
44
|
+
*
|
|
45
|
+
* pipe(
|
|
46
|
+
* filter(user => user.isActive),
|
|
47
|
+
* minBy(user => user.age)
|
|
48
|
+
* )(users)
|
|
49
|
+
* // => { isActive: true, age: 25 }
|
|
50
|
+
*
|
|
51
|
+
* @name minBy
|
|
52
|
+
* @tag Array
|
|
53
|
+
*
|
|
54
|
+
* @see {@link max}
|
|
55
|
+
* @see {@link maxBy}
|
|
56
|
+
* @see {@link sortBy}
|
|
57
|
+
*/ const minBy = (...params)=>{
|
|
58
|
+
const fn = _toFunction(params[0], {
|
|
59
|
+
fnName: "minBy",
|
|
60
|
+
paramName: "fn"
|
|
61
|
+
});
|
|
62
|
+
// @signature (fn) => (input)
|
|
63
|
+
if (params.length === 1) {
|
|
64
|
+
return (input)=>_minBy(fn, input);
|
|
65
|
+
}
|
|
66
|
+
// @signature (fn, input)
|
|
67
|
+
return _minBy(fn, params[1]);
|
|
68
|
+
};
|
|
69
|
+
export { minBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type MinFn = (input: readonly number[]) => number | undefined;
|
|
2
|
+
/**
|
|
3
|
+
* Find the smallest number of an array. Skips a `NaN` item, and returns
|
|
4
|
+
* `undefined` when no number is left. Throws on an item that is not a number.
|
|
5
|
+
*
|
|
6
|
+
* Input is never mutated.
|
|
7
|
+
*
|
|
8
|
+
* @param {readonly Array} input - Array of numbers
|
|
9
|
+
*
|
|
10
|
+
* @returns {number|undefined} The smallest number, or `undefined`
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* min([3, 1, 4, 1, 5])
|
|
14
|
+
* // => 1
|
|
15
|
+
*
|
|
16
|
+
* min([])
|
|
17
|
+
* // => undefined
|
|
18
|
+
*
|
|
19
|
+
* min([1, NaN, 2])
|
|
20
|
+
* // => 1
|
|
21
|
+
*
|
|
22
|
+
* pipe(map(read("score")), min)([{ score: 1 }, { score: 7 }])
|
|
23
|
+
* // => 1
|
|
24
|
+
*
|
|
25
|
+
* @name min
|
|
26
|
+
* @tag Number
|
|
27
|
+
*
|
|
28
|
+
* @see {@link max}
|
|
29
|
+
* @see {@link minBy}
|
|
30
|
+
* @see {@link sum}
|
|
31
|
+
*/
|
|
32
|
+
declare const min: MinFn;
|
|
33
|
+
export { min };
|
|
34
|
+
export type { MinFn };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type } from "../type/type.js";
|
|
2
|
+
const _min = (input)=>{
|
|
3
|
+
if (!Array.isArray(input)) {
|
|
4
|
+
throw new TypeError(`@asd14/m/min: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
5
|
+
}
|
|
6
|
+
let result;
|
|
7
|
+
for(let i = 0, length = input.length; i < length; i++){
|
|
8
|
+
const item = input[i];
|
|
9
|
+
if (typeof item !== "number") {
|
|
10
|
+
throw new TypeError(`@asd14/m/min: expected 'input[${i}]' to be 'Number', got '${type(item)}'`);
|
|
11
|
+
}
|
|
12
|
+
// NaN cannot be ordered, so it never wins
|
|
13
|
+
if (!Number.isNaN(item) && (result === undefined || item < result)) {
|
|
14
|
+
result = item;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Find the smallest number of an array. Skips a `NaN` item, and returns
|
|
21
|
+
* `undefined` when no number is left. Throws on an item that is not a number.
|
|
22
|
+
*
|
|
23
|
+
* Input is never mutated.
|
|
24
|
+
*
|
|
25
|
+
* @param {readonly Array} input - Array of numbers
|
|
26
|
+
*
|
|
27
|
+
* @returns {number|undefined} The smallest number, or `undefined`
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* min([3, 1, 4, 1, 5])
|
|
31
|
+
* // => 1
|
|
32
|
+
*
|
|
33
|
+
* min([])
|
|
34
|
+
* // => undefined
|
|
35
|
+
*
|
|
36
|
+
* min([1, NaN, 2])
|
|
37
|
+
* // => 1
|
|
38
|
+
*
|
|
39
|
+
* pipe(map(read("score")), min)([{ score: 1 }, { score: 7 }])
|
|
40
|
+
* // => 1
|
|
41
|
+
*
|
|
42
|
+
* @name min
|
|
43
|
+
* @tag Number
|
|
44
|
+
*
|
|
45
|
+
* @see {@link max}
|
|
46
|
+
* @see {@link minBy}
|
|
47
|
+
* @see {@link sum}
|
|
48
|
+
*/ const min = _min;
|
|
49
|
+
export { min };
|
package/dist/src/read/read.d.ts
CHANGED
package/dist/src/read/read.js
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type TakeLastFn = {
|
|
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
|
+
* Take elements from the end of an array. When called with only an array,
|
|
8
|
+
* takes the last 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 the last `count` elements
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* takeLast([1, 2, 3])
|
|
19
|
+
* // => [3]
|
|
20
|
+
*
|
|
21
|
+
* takeLast(2, [1, 2, 3])
|
|
22
|
+
* // => [2, 3]
|
|
23
|
+
*
|
|
24
|
+
* takeLast(2)([1, 2, 3])
|
|
25
|
+
* // => [2, 3]
|
|
26
|
+
*
|
|
27
|
+
* pipe(takeLast(2), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
28
|
+
* // => [2, 3]
|
|
29
|
+
*
|
|
30
|
+
* @name takeLast
|
|
31
|
+
* @tag Array
|
|
32
|
+
*
|
|
33
|
+
* @see {@link last}
|
|
34
|
+
* @see {@link take}
|
|
35
|
+
*/
|
|
36
|
+
declare const takeLast: TakeLastFn;
|
|
37
|
+
export { takeLast };
|
|
38
|
+
export type { TakeLastFn };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type } from "../../type/type.js";
|
|
2
|
+
const _takeLast = (count, input)=>{
|
|
3
|
+
if (!Array.isArray(input)) {
|
|
4
|
+
throw new TypeError(`@asd14/m/takeLast: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
5
|
+
}
|
|
6
|
+
return count === 0 ? [] : input.slice(-count);
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Take elements from the end of an array. When called with only an array,
|
|
10
|
+
* takes the last 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 the last `count` elements
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* takeLast([1, 2, 3])
|
|
21
|
+
* // => [3]
|
|
22
|
+
*
|
|
23
|
+
* takeLast(2, [1, 2, 3])
|
|
24
|
+
* // => [2, 3]
|
|
25
|
+
*
|
|
26
|
+
* takeLast(2)([1, 2, 3])
|
|
27
|
+
* // => [2, 3]
|
|
28
|
+
*
|
|
29
|
+
* pipe(takeLast(2), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
30
|
+
* // => [2, 3]
|
|
31
|
+
*
|
|
32
|
+
* @name takeLast
|
|
33
|
+
* @tag Array
|
|
34
|
+
*
|
|
35
|
+
* @see {@link last}
|
|
36
|
+
* @see {@link take}
|
|
37
|
+
*/ const takeLast = (...params)=>{
|
|
38
|
+
// @signature (input)
|
|
39
|
+
if (Array.isArray(params[0])) {
|
|
40
|
+
return _takeLast(1, params[0]);
|
|
41
|
+
}
|
|
42
|
+
const count = params[0];
|
|
43
|
+
if (typeof count !== "number") {
|
|
44
|
+
throw new TypeError(`@asd14/m/takeLast: expected 'count' to be 'Number', got '${type(count)}'`);
|
|
45
|
+
}
|
|
46
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
47
|
+
throw new TypeError(`@asd14/m/takeLast: expected 'count' to be 'NonNegativeInteger', got '${String(count)}'`);
|
|
48
|
+
}
|
|
49
|
+
// @signature (count, input)
|
|
50
|
+
if (params.length === 2) {
|
|
51
|
+
return _takeLast(count, params[1]);
|
|
52
|
+
}
|
|
53
|
+
// @signature (count) => (input)
|
|
54
|
+
return (input)=>_takeLast(count, input);
|
|
55
|
+
};
|
|
56
|
+
export { takeLast };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type TakeFn = {
|
|
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
|
+
* Take elements from the start of an array. When called with only an array,
|
|
8
|
+
* takes 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 the first `count` elements
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* take([1, 2, 3])
|
|
19
|
+
* // => [1]
|
|
20
|
+
*
|
|
21
|
+
* take(2, [1, 2, 3])
|
|
22
|
+
* // => [1, 2]
|
|
23
|
+
*
|
|
24
|
+
* take(2)([1, 2, 3])
|
|
25
|
+
* // => [1, 2]
|
|
26
|
+
*
|
|
27
|
+
* pipe(take(2), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
28
|
+
* // => [1, 2]
|
|
29
|
+
*
|
|
30
|
+
* @name take
|
|
31
|
+
* @tag Array
|
|
32
|
+
*
|
|
33
|
+
* @see {@link first}
|
|
34
|
+
* @see {@link takeLast}
|
|
35
|
+
*/
|
|
36
|
+
declare const take: TakeFn;
|
|
37
|
+
export { take };
|
|
38
|
+
export type { TakeFn };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type } from "../type/type.js";
|
|
2
|
+
const _take = (count, input)=>{
|
|
3
|
+
if (!Array.isArray(input)) {
|
|
4
|
+
throw new TypeError(`@asd14/m/take: expected 'input' to be 'Array', got '${type(input)}'`);
|
|
5
|
+
}
|
|
6
|
+
return input.slice(0, count);
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Take elements from the start of an array. When called with only an array,
|
|
10
|
+
* takes 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 the first `count` elements
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* take([1, 2, 3])
|
|
21
|
+
* // => [1]
|
|
22
|
+
*
|
|
23
|
+
* take(2, [1, 2, 3])
|
|
24
|
+
* // => [1, 2]
|
|
25
|
+
*
|
|
26
|
+
* take(2)([1, 2, 3])
|
|
27
|
+
* // => [1, 2]
|
|
28
|
+
*
|
|
29
|
+
* pipe(take(2), map(read("id")))([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
30
|
+
* // => [1, 2]
|
|
31
|
+
*
|
|
32
|
+
* @name take
|
|
33
|
+
* @tag Array
|
|
34
|
+
*
|
|
35
|
+
* @see {@link first}
|
|
36
|
+
* @see {@link takeLast}
|
|
37
|
+
*/ const take = (...params)=>{
|
|
38
|
+
// @signature (input)
|
|
39
|
+
if (Array.isArray(params[0])) {
|
|
40
|
+
return _take(1, params[0]);
|
|
41
|
+
}
|
|
42
|
+
const count = params[0];
|
|
43
|
+
if (typeof count !== "number") {
|
|
44
|
+
throw new TypeError(`@asd14/m/take: expected 'count' to be 'Number', got '${type(count)}'`);
|
|
45
|
+
}
|
|
46
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
47
|
+
throw new TypeError(`@asd14/m/take: expected 'count' to be 'NonNegativeInteger', got '${String(count)}'`);
|
|
48
|
+
}
|
|
49
|
+
// @signature (count, input)
|
|
50
|
+
if (params.length === 2) {
|
|
51
|
+
return _take(count, params[1]);
|
|
52
|
+
}
|
|
53
|
+
// @signature (count) => (input)
|
|
54
|
+
return (input)=>_take(count, input);
|
|
55
|
+
};
|
|
56
|
+
export { take };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type ValuesFn = {
|
|
2
|
+
<T>(input: readonly T[]): T[];
|
|
3
|
+
<T extends Record<string, unknown>>(input: T): T[keyof T][];
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Get list of own enumerable property values.
|
|
7
|
+
*
|
|
8
|
+
* Returns the items of an array, the values of a plain object in key order.
|
|
9
|
+
* Any other object throws, eg. a `Map`: read its values with
|
|
10
|
+
* `[...map.values()]`.
|
|
11
|
+
*
|
|
12
|
+
* @param {Array|Object} input - Array or Object to extract values from
|
|
13
|
+
*
|
|
14
|
+
* @returns {Array} List of values
|
|
15
|
+
*
|
|
16
|
+
* @throws {TypeError} If input is not an Array or Object
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* values(["lorem", "ipsum"])
|
|
20
|
+
* // => ["lorem", "ipsum"]
|
|
21
|
+
*
|
|
22
|
+
* values({ foo: "bar", lorem: "ipsum" })
|
|
23
|
+
* // => ["bar", "ipsum"]
|
|
24
|
+
*
|
|
25
|
+
* values(new Map([["foo", "bar"]]))
|
|
26
|
+
* // throws "@asd14/m/values: expected 'input' to be 'Array' or 'Object', got 'Map'"
|
|
27
|
+
*
|
|
28
|
+
* // In a pipe - sum the values of an object
|
|
29
|
+
* pipe(
|
|
30
|
+
* values,
|
|
31
|
+
* sum
|
|
32
|
+
* )({ a: 1, b: 2, c: 3 })
|
|
33
|
+
* // => 6
|
|
34
|
+
*
|
|
35
|
+
* @name values
|
|
36
|
+
* @tag Object
|
|
37
|
+
*
|
|
38
|
+
* @see {@link keys}
|
|
39
|
+
*/
|
|
40
|
+
declare const values: ValuesFn;
|
|
41
|
+
export { values };
|
|
42
|
+
export type { ValuesFn };
|