@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
@@ -12,9 +12,11 @@ const _buildURIRegExp = ()=>{
12
12
  const hexDigitOnly = `[${hexDigit}]`;
13
13
  const unreserved = `${alpha}${digit}-\\._~`;
14
14
  const subDelims = "!\\$&'\\(\\)\\*\\+,;=";
15
- const pctEncoded = `%${hexDigit}`;
16
- const pchar = `${unreserved}${pctEncoded}${subDelims}:@`;
17
- const pcharOnly = `[${pchar}]`;
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
- const scheme = `[${alpha}][${alpha}${digit}+-\\.]*`;
36
- const userinfo = `[${unreserved}${pctEncoded}${subDelims}:]*`;
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}${pctEncoded}${subDelims}]{0,255}`;
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 = `[${pchar}\\/\\?]*(?=#|$)`;
49
- const fragment = `[${pchar}\\/\\?]*`;
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
@@ -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 };
@@ -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, and a `"__proto__"` key stays an own key.
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).
@@ -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
- let result = {};
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
- result = {
7
- ...result,
8
- ...object
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, and a `"__proto__"` key stays an own key.
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).
@@ -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 };
@@ -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);
@@ -49,6 +49,7 @@ type ReadFn = {
49
49
  *
50
50
  * @see {@link pick}
51
51
  * @see {@link update}
52
+ * @see {@link write}
52
53
  */
53
54
  declare const read: ReadFn;
54
55
  export { read, read as get };
@@ -60,6 +60,7 @@ const _read = (path, defaultValue, input)=>{
60
60
  *
61
61
  * @see {@link pick}
62
62
  * @see {@link update}
63
+ * @see {@link write}
63
64
  */ const read = (...params)=>{
64
65
  const path = params[0];
65
66
  if (typeof path !== "string" && !Array.isArray(path)) {
@@ -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 };