@oscarpalmer/atoms 0.217.0 → 0.219.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 (85) hide show
  1. package/dist/array/filter.d.mts +38 -4
  2. package/dist/array/find.d.mts +34 -0
  3. package/dist/array/first.d.mts +40 -4
  4. package/dist/array/insert.mjs +1 -1
  5. package/dist/array/last.d.mts +46 -10
  6. package/dist/array/partition.d.mts +24 -8
  7. package/dist/array/reverse.d.mts +6 -0
  8. package/dist/array/reverse.mjs +6 -0
  9. package/dist/array/select.d.mts +97 -116
  10. package/dist/array/select.mjs +19 -15
  11. package/dist/array/single.d.mts +22 -0
  12. package/dist/array/single.mjs +1 -1
  13. package/dist/array/slice.mjs +27 -14
  14. package/dist/array/sort.d.mts +54 -0
  15. package/dist/array/sort.mjs +2 -1
  16. package/dist/color/misc/get.mjs +3 -1
  17. package/dist/color/space/hex.d.mts +8 -0
  18. package/dist/color/space/hex.mjs +9 -1
  19. package/dist/function/retry.d.mts +1 -1
  20. package/dist/function/retry.mjs +5 -2
  21. package/dist/index.d.mts +879 -324
  22. package/dist/index.mjs +480 -173
  23. package/dist/internal/array/callbacks.mjs +1 -1
  24. package/dist/internal/array/find.d.mts +3 -3
  25. package/dist/internal/array/find.mjs +90 -48
  26. package/dist/internal/array/insert.mjs +1 -1
  27. package/dist/internal/array/match.mjs +21 -17
  28. package/dist/internal/array/sets.mjs +7 -1
  29. package/dist/internal/array/shuffle.d.mts +5 -0
  30. package/dist/internal/array/shuffle.mjs +5 -0
  31. package/dist/internal/math/aggregate.d.mts +69 -21
  32. package/dist/internal/math/aggregate.mjs +4 -1
  33. package/dist/internal/math/misc.d.mts +14 -0
  34. package/dist/internal/math/misc.mjs +14 -0
  35. package/dist/internal/number.d.mts +19 -1
  36. package/dist/internal/number.mjs +22 -2
  37. package/dist/internal/plan/plan.generate.mjs +3 -3
  38. package/dist/internal/random.d.mts +6 -0
  39. package/dist/internal/random.mjs +13 -2
  40. package/dist/internal/result/attempt.mjs +4 -4
  41. package/dist/internal/string/case.d.mts +63 -0
  42. package/dist/internal/string/case.mjs +63 -0
  43. package/dist/internal/string/misc.d.mts +20 -0
  44. package/dist/internal/string/misc.mjs +22 -1
  45. package/dist/internal/subscription.mjs +1 -1
  46. package/dist/internal/value/equal.mjs +2 -1
  47. package/dist/internal/value/partial.mjs +3 -1
  48. package/dist/logger.d.mts +12 -0
  49. package/dist/logger.mjs +12 -0
  50. package/dist/math.d.mts +90 -0
  51. package/dist/math.mjs +7 -0
  52. package/dist/models/array/array.find.model.d.mts +26 -0
  53. package/dist/models/array/array.misc.model.d.mts +2 -4
  54. package/dist/models/index.d.mts +1 -1
  55. package/dist/models/plan.model.d.mts +2 -0
  56. package/dist/models/plan.model.mjs +3 -1
  57. package/dist/models/string/string.fuzzy.model.d.mts +1 -1
  58. package/dist/models/string/string.misc.model.mjs +1 -1
  59. package/dist/models/value/value.merge.model.d.mts +4 -4
  60. package/dist/plan/index.d.mts +1 -4
  61. package/dist/plan/index.mjs +2 -3
  62. package/dist/plan/use.mjs +4 -4
  63. package/dist/promise/index.d.mts +6 -4
  64. package/dist/promise/index.mjs +4 -4
  65. package/dist/promise/misc.d.mts +6 -0
  66. package/dist/promise/misc.mjs +11 -2
  67. package/dist/promise/timed.d.mts +3 -0
  68. package/dist/promise/timed.mjs +5 -2
  69. package/dist/queue.d.mts +0 -1
  70. package/dist/queue.mjs +5 -6
  71. package/dist/random.d.mts +2 -2
  72. package/dist/random.mjs +3 -3
  73. package/dist/result/match.mjs +2 -2
  74. package/dist/result/misc.mjs +3 -10
  75. package/dist/result/work/pipe.d.mts +24 -22
  76. package/dist/result/work/pipe.mjs +7 -3
  77. package/dist/string/fuzzy.mjs +1 -1
  78. package/dist/string/index.d.mts +15 -1
  79. package/dist/string/index.mjs +16 -2
  80. package/dist/string/match.d.mts +24 -0
  81. package/dist/string/match.mjs +24 -0
  82. package/dist/string/template.d.mts +4 -0
  83. package/dist/value/clone.mjs +1 -1
  84. package/dist/value/merge.mjs +17 -4
  85. package/package.json +1 -1
@@ -3,7 +3,7 @@ function getArrayCallback(value) {
3
3
  switch (typeof value) {
4
4
  case "function": return value;
5
5
  case "number":
6
- case "string": return typeof value === "string" && value.includes(".") ? void 0 : (obj) => obj[value];
6
+ case "string": return (obj) => obj?.[value];
7
7
  default: return;
8
8
  }
9
9
  }
@@ -1,8 +1,8 @@
1
- import { ArrayFindMapper, ArrayFindValueType, ArrayFindValuesResult, ArrayFindValuesType, ArrayParameters } from "../../models/array/array.find.model.mjs";
1
+ import { ArrayFindOptions, ArrayFindValueType, ArrayFindValuesResult, ArrayFindValuesType, ArrayParameters } from "../../models/array/array.find.model.mjs";
2
2
  //#region src/internal/array/find.d.ts
3
3
  export declare function createFindParameters(original: unknown[]): ArrayParameters;
4
+ export declare function findAbsoluteValueOrDefault(array: unknown[], parameters: unknown[], defaultValue: unknown, useDefaultValue: boolean, reversed: boolean): unknown;
4
5
  export declare function findValue(type: Exclude<ArrayFindValueType, "item">, array: unknown[], parameters: unknown[], reversed: boolean): number;
5
6
  export declare function findValue(type: Exclude<ArrayFindValueType, "index">, array: unknown[], parameters: unknown[], reversed: boolean): unknown;
6
- export declare function findAbsoluteValueOrDefault(array: unknown[], parameters: unknown[], defaultValue: unknown, useDefaultValue: boolean, reversed: boolean): unknown;
7
- export declare function findValues(type: ArrayFindValuesType, array: unknown[], parameters: unknown[], mapper?: ArrayFindMapper): ArrayFindValuesResult;
7
+ export declare function findValues(type: ArrayFindValuesType, array: unknown[], parameters: unknown[], options?: ArrayFindOptions): ArrayFindValuesResult;
8
8
  //#endregion
@@ -1,6 +1,15 @@
1
1
  import { ARRAY_FIND_VALUE_INDEX } from "../../models/array/array.find.model.mjs";
2
2
  import { getArrayCallback, getArrayCallbacks } from "./callbacks.mjs";
3
3
  //#region src/internal/array/find.ts
4
+ function addResult(result, mapped, flat) {
5
+ if (!flat) {
6
+ result.matched.push(mapped);
7
+ return;
8
+ }
9
+ const array = Array.isArray(mapped) ? mapped : [mapped];
10
+ const { length } = array;
11
+ for (let index = 0; index < length; index += 1) result.matched.push(array[index]);
12
+ }
4
13
  function createFindParameters(original) {
5
14
  const { length } = original;
6
15
  return {
@@ -9,15 +18,65 @@ function createFindParameters(original) {
9
18
  value: length === 1 && typeof original[0] !== "function" ? original[0] : original[1]
10
19
  };
11
20
  }
21
+ function findAbsoluteValueOrDefault(array, parameters, defaultValue, useDefaultValue, reversed) {
22
+ if (parameters.length === 0) {
23
+ if (Array.isArray(array) && array.length > 0) return reversed ? array.at(-1) : array[0];
24
+ return useDefaultValue ? defaultValue : void 0;
25
+ }
26
+ const index = findValue(ARRAY_FIND_VALUE_INDEX, array, parameters, reversed);
27
+ if (index === -1) return useDefaultValue ? defaultValue : void 0;
28
+ return array[index];
29
+ }
30
+ function findComplexValues(parameters) {
31
+ const { array, arrayLength, callback, limit, result, type, value } = parameters;
32
+ const keys = /* @__PURE__ */ new Set();
33
+ for (let arrayIndex = 0; arrayIndex < arrayLength; arrayIndex += 1) {
34
+ const arrayItem = array[arrayIndex];
35
+ const keyed = callback == null ? arrayItem : callback(arrayItem, arrayIndex, array);
36
+ const mapped = arrayItem;
37
+ if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
38
+ keys.add(keyed);
39
+ result.matched.push(mapped);
40
+ if (result.matched.length >= limit) return result;
41
+ } else result.notMatched.push(arrayItem);
42
+ }
43
+ return result;
44
+ }
45
+ function findSimpleValue(array, value, findIndex, reversed) {
46
+ if (findIndex) return reversed ? array.lastIndexOf(value) : array.indexOf(value);
47
+ return reversed ? array.findLast((item) => Object.is(item, value)) : array.find((item) => Object.is(item, value));
48
+ }
49
+ function findSimpleValues(parameters) {
50
+ const { array, arrayLength, callback, flat, limit, map, result, value } = parameters;
51
+ const { after, before } = map ?? {};
52
+ const handler = callback ?? ((item) => Object.is(item, value));
53
+ for (let arrayIndex = 0; arrayIndex < arrayLength; arrayIndex += 1) {
54
+ const arrayItem = array[arrayIndex];
55
+ let mapped = before == null ? arrayItem : before(arrayItem, arrayIndex, array);
56
+ if (!flat || after != null) {
57
+ if (handler(mapped, arrayIndex, array)) {
58
+ mapped = after == null ? mapped : after(arrayItem, arrayIndex, array);
59
+ addResult(result, mapped, flat);
60
+ if (result.matched.length >= limit) return result;
61
+ } else result.notMatched.push(arrayItem);
62
+ continue;
63
+ }
64
+ const flatItems = Array.isArray(mapped) ? mapped : [mapped];
65
+ const flatLength = flatItems.length;
66
+ for (let flatIndex = 0; flatIndex < flatLength; flatIndex += 1) {
67
+ const flatItem = flatItems[flatIndex];
68
+ if (handler(flatItem, arrayIndex, array)) result.matched.push(flatItem);
69
+ else result.notMatched.push(flatItem);
70
+ }
71
+ }
72
+ return result;
73
+ }
12
74
  function findValue(type, array, parameters, reversed) {
13
75
  const findIndex = type === ARRAY_FIND_VALUE_INDEX;
14
76
  if (!Array.isArray(array) || array.length === 0) return findIndex ? -1 : void 0;
15
77
  const { bool, key, value } = createFindParameters(parameters);
16
78
  const callbacks = getArrayCallbacks(bool, key);
17
- if (callbacks?.bool == null && callbacks?.keyed == null) {
18
- if (findIndex) return reversed ? array.lastIndexOf(value) : array.indexOf(value);
19
- return reversed ? array.findLast((item) => Object.is(item, value)) : array.find((item) => Object.is(item, value));
20
- }
79
+ if (callbacks?.bool == null && callbacks?.keyed == null) return findSimpleValue(array, value, findIndex, reversed);
21
80
  if (callbacks.bool != null) {
22
81
  const index = reversed ? array.findLastIndex(callbacks.bool) : array.findIndex(callbacks.bool);
23
82
  return findIndex ? index : array[index];
@@ -32,59 +91,42 @@ function findValueInArray(array, callback, value, findIndex, reversed) {
32
91
  }
33
92
  return findIndex ? -1 : void 0;
34
93
  }
35
- function findAbsoluteValueOrDefault(array, parameters, defaultValue, useDefaultValue, reversed) {
36
- if (parameters.length === 0) {
37
- if (Array.isArray(array) && array.length > 0) return reversed ? array.at(-1) : array[0];
38
- return useDefaultValue ? defaultValue : void 0;
39
- }
40
- const index = findValue(ARRAY_FIND_VALUE_INDEX, array, parameters, reversed);
41
- return index > -1 ? array[index] : useDefaultValue ? defaultValue : void 0;
42
- }
43
- function findValues(type, array, parameters, mapper) {
94
+ function findValues(type, array, parameters, options) {
44
95
  const result = {
45
96
  matched: [],
46
97
  notMatched: []
47
98
  };
48
- if (!Array.isArray(array) || array.length === 0) return result;
49
- const { length } = array;
99
+ if (!Array.isArray(array)) return result;
100
+ const arrayLength = array.length;
101
+ if (arrayLength === 0) return result;
50
102
  const { bool, key, value } = createFindParameters(parameters);
51
103
  const callbacks = getArrayCallbacks(bool, key);
52
- if (type === "unique" && callbacks?.keyed == null && length >= 100) {
104
+ if (type === "unique" && callbacks?.keyed == null && arrayLength >= 100) {
53
105
  result.matched = [...new Set(array)];
54
106
  return result;
55
107
  }
56
- const mapCallback = getArrayCallback(mapper?.callback);
57
- const mapReverse = mapper?.reverse ?? false;
58
- const mapAfter = mapCallback == null ? void 0 : mapReverse ? void 0 : mapCallback;
59
- const mapBefore = mapCallback == null ? void 0 : mapReverse ? mapCallback : void 0;
60
- if (callbacks?.bool != null || type === "all" && key == null) {
61
- const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
62
- for (let index = 0; index < length; index += 1) {
63
- const item = array[index];
64
- const transformed = mapBefore?.(item, index, array) ?? item;
65
- if (callback(transformed, index, array)) result.matched.push(mapAfter?.(item, index, array) ?? transformed);
66
- else result.notMatched.push(item);
67
- }
68
- return result;
69
- }
70
- const keys = /* @__PURE__ */ new Set();
71
- for (let index = 0; index < length; index += 1) {
72
- const item = array[index];
73
- let keyed;
74
- let transformed;
75
- if (mapBefore == null) {
76
- keyed = callbacks?.keyed?.(item, index, array) ?? item;
77
- transformed = item;
78
- } else {
79
- transformed = mapBefore(item, index, array);
80
- keyed = callbacks?.keyed?.(transformed, index, array) ?? transformed;
81
- }
82
- if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
83
- keys.add(keyed);
84
- result.matched.push(mapAfter?.(item, index, array) ?? transformed);
85
- } else result.notMatched.push(item);
86
- }
87
- return result;
108
+ const limit = options?.limit ?? Number.POSITIVE_INFINITY;
109
+ const flat = options?.map?.flat ?? false;
110
+ const mapCallback = getArrayCallback(options?.map?.callback);
111
+ const mapReverse = options?.map?.reverse ?? false;
112
+ let map;
113
+ if (mapCallback != null) map = {
114
+ before: mapReverse ? mapCallback : void 0,
115
+ after: mapReverse ? void 0 : mapCallback
116
+ };
117
+ const simple = callbacks?.bool != null || type === "all" && key == null;
118
+ const findParameters = {
119
+ array,
120
+ arrayLength,
121
+ flat,
122
+ limit,
123
+ map,
124
+ result,
125
+ type,
126
+ value,
127
+ callback: simple ? callbacks?.bool : callbacks?.keyed
128
+ };
129
+ return simple ? findSimpleValues(findParameters) : findComplexValues(findParameters);
88
130
  }
89
131
  //#endregion
90
132
  export { createFindParameters, findAbsoluteValueOrDefault, findValue, findValues };
@@ -3,7 +3,7 @@ import { chunk } from "./chunk.mjs";
3
3
  import { max, min } from "../math/aggregate.mjs";
4
4
  //#region src/internal/array/insert.ts
5
5
  function insertChunkedValues(type, array, items, start, deleteCount) {
6
- const actualDeleteCount = deleteCount < 0 ? 0 : deleteCount;
6
+ const actualDeleteCount = max([deleteCount, 0]);
7
7
  const actualStart = min([max([0, start]), array.length]);
8
8
  const chunked = chunk(items);
9
9
  const lastIndex = chunked.length - 1;
@@ -4,6 +4,21 @@ import { ARRAY_MATCH_COMPARISON_INSIDE, ARRAY_MATCH_COMPARISON_SAME, ARRAY_MATCH
4
4
  function endsWithArray(haystack, needle, key) {
5
5
  return arrayMatchEndings.has(getPosition(haystack, needle, key)[1]);
6
6
  }
7
+ function evaluatePosition(haystack, haystackLength, needle, needleLength, key) {
8
+ const callback = getArrayCallback(key);
9
+ const limit = haystackLength - needleLength + 1;
10
+ let needleValues = callback == null ? needle : needle.slice();
11
+ if (callback != null) for (let needleIndex = 0; needleIndex < needleLength; needleIndex += 1) needleValues[needleIndex] = callback(needle[needleIndex], needleIndex, needle);
12
+ for (let haystackIndex = 0; haystackIndex < limit; haystackIndex += 1) {
13
+ if (!inHaystack(haystack, haystackIndex, needleValues, 0, callback)) continue;
14
+ if (needleLength === 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
15
+ for (let needleIndex = 1; needleIndex < needleLength; needleIndex += 1) {
16
+ if (!inHaystack(haystack, haystackIndex, needleValues, needleIndex, callback)) break;
17
+ if (needleIndex === needleLength - 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
18
+ }
19
+ }
20
+ return arrayMatchOutside;
21
+ }
7
22
  function getArrayComparison(haystack, needle, key) {
8
23
  return getPosition(haystack, needle, key)[1];
9
24
  }
@@ -17,23 +32,12 @@ function getPosition(haystack, needle, key) {
17
32
  const needleLength = needle.length;
18
33
  if (haystackLength === 0 || needleLength === 0) return arrayMatchOutside;
19
34
  if (needleLength > haystackLength) return arrayMatchOutside;
20
- const callback = getArrayCallback(key);
21
- const limit = haystackLength - needleLength + 1;
22
- let needleValues = callback == null ? needle : needle.slice();
23
- if (callback != null) for (let needleIndex = 0; needleIndex < needleLength; needleIndex += 1) needleValues[needleIndex] = callback(needle[needleIndex], needleIndex, needle);
24
- for (let haystackIndex = 0; haystackIndex < limit; haystackIndex += 1) {
25
- let haystackItem = haystack[haystackIndex];
26
- let haystackValue = callback?.(haystackItem, haystackIndex, haystack) ?? haystackItem;
27
- if (!Object.is(haystackValue, needleValues[0])) continue;
28
- if (needleLength === 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
29
- for (let needleIndex = 1; needleIndex < needleLength; needleIndex += 1) {
30
- haystackItem = haystack[haystackIndex + needleIndex];
31
- haystackValue = callback?.(haystackItem, haystackIndex + needleIndex, haystack) ?? haystackItem;
32
- if (!Object.is(haystackValue, needleValues[needleIndex])) break;
33
- if (needleIndex === needleLength - 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
34
- }
35
- }
36
- return arrayMatchOutside;
35
+ return evaluatePosition(haystack, haystackLength, needle, needleLength, key);
36
+ }
37
+ function inHaystack(haystack, haystackIndex, needleValues, needleIndex, callback) {
38
+ const haystackItem = haystack[haystackIndex + needleIndex];
39
+ const haystackValue = callback?.(haystackItem, haystackIndex + needleIndex, haystack) ?? haystackItem;
40
+ return Object.is(haystackValue, needleValues[needleIndex]);
37
41
  }
38
42
  function includesArray(haystack, needle, key) {
39
43
  return !arrayMatchOutsides.has(getPosition(haystack, needle, key)[1]);
@@ -6,8 +6,14 @@ function compareSets(type, first, second, key) {
6
6
  const isDifference = type === ARRAY_SETS_COMPARE_DIFFERENCE;
7
7
  const isIntersection = type === ARRAY_SETS_COMPARE_INTERSECTION;
8
8
  const isUnion = type === ARRAY_SETS_COMPARE_UNION;
9
- if (first.length === 0) return isDifference ? first.slice() : isIntersection ? [] : second.slice();
9
+ if (first.length === 0) {
10
+ if (isDifference) return first.slice();
11
+ return isIntersection ? [] : second.slice();
12
+ }
10
13
  if (!Array.isArray(second) || second.length === 0) return isIntersection ? [] : first.slice();
14
+ return getComparison(first, second, isIntersection, isUnion, key);
15
+ }
16
+ function getComparison(first, second, isIntersection, isUnion, key) {
11
17
  const callback = getArrayCallback(key);
12
18
  const values = isUnion ? first : second;
13
19
  let { length } = values;
@@ -4,6 +4,11 @@
4
4
  *
5
5
  * @param array Original array
6
6
  * @returns Shuffled array
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * shuffle([1, 2, 3]); // => [3, 1, 2] (maybe? it's random, you know)
11
+ * ```
7
12
  */
8
13
  export declare function shuffle<Item>(array: Item[]): Item[];
9
14
  //#endregion
@@ -5,6 +5,11 @@ import { getRandomInteger } from "../random.mjs";
5
5
  *
6
6
  * @param array Original array
7
7
  * @returns Shuffled array
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * shuffle([1, 2, 3]); // => [3, 1, 2] (maybe? it's random, you know)
12
+ * ```
8
13
  */
9
14
  function shuffle(array) {
10
15
  if (!Array.isArray(array)) return [];
@@ -7,6 +7,11 @@ export declare function getAggregated(type: NonAverageAggregationType, array: un
7
7
  /**
8
8
  * Get the maximum value from a list of items
9
9
  *
10
+ * @param items List of items
11
+ * @param callback Callback to get an item's value
12
+ * @param first Return only the first item with the maximum value
13
+ * @returns Item with the maximum value, or `undefined` if no maximum can be found
14
+ *
10
15
  * @example
11
16
  * ```typescript
12
17
  * max(
@@ -16,16 +21,16 @@ export declare function getAggregated(type: NonAverageAggregationType, array: un
16
21
  *
17
22
  * max([], item => item.value); // => []
18
23
  * ```
19
- *
20
- * @param items List of items
21
- * @param callback Callback to get an item's value
22
- * @param first Return only the first item with the maximum value
23
- * @returns Item with the maximum value, or `undefined` if no maximum can be found
24
24
  */
25
25
  export declare function max<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
26
26
  /**
27
27
  * Get the maximum value from a list of items
28
28
  *
29
+ * @param items List of items
30
+ * @param key Key to use for value
31
+ * @param first Return only the first item with the maximum value
32
+ * @returns Item with the maximum value, or `undefined` if no maximum can be found
33
+ *
29
34
  * @example
30
35
  * ```typescript
31
36
  * max(
@@ -35,16 +40,15 @@ export declare function max<Item>(items: Item[], callback: (item: Item, index: n
35
40
  *
36
41
  * max([], 'value'); // => []
37
42
  * ```
38
- *
39
- * @param items List of items
40
- * @param key Key to use for value
41
- * @param first Return only the first item with the maximum value
42
- * @returns Item with the maximum value, or `undefined` if no maximum can be found
43
43
  */
44
44
  export declare function max<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
45
45
  /**
46
46
  * Get the maximum value from a list of items
47
47
  *
48
+ * @param items List of items
49
+ * @param callback Callback to get an item's value
50
+ * @returns Items with the maximum value
51
+ *
48
52
  * @example
49
53
  * ```typescript
50
54
  * max(
@@ -54,15 +58,15 @@ export declare function max<Item extends PlainObject>(items: Item[], key: keyof
54
58
  *
55
59
  * max([], item => item.value); // => []
56
60
  * ```
57
- *
58
- * @param items List of items
59
- * @param callback Callback to get an item's value
60
- * @returns Items with the maximum value
61
61
  */
62
62
  export declare function max<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): Item[];
63
63
  /**
64
64
  * Get the maximum value from a list of items
65
65
  *
66
+ * @param items List of items
67
+ * @param key Key to use for value
68
+ * @returns Items with the maximum value
69
+ *
66
70
  * @example
67
71
  * ```typescript
68
72
  * max(
@@ -72,23 +76,19 @@ export declare function max<Item>(items: Item[], callback: (item: Item, index: n
72
76
  *
73
77
  * max([], 'value'); // => []
74
78
  * ```
75
- *
76
- * @param items List of items
77
- * @param key Key to use for value
78
- * @returns Items with the maximum value
79
79
  */
80
80
  export declare function max<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
81
81
  /**
82
82
  * Get the maximum value from a list of numbers
83
83
  *
84
+ * @param values List of numbers
85
+ * @returns Maximum value, or `Number.NaN` if no maximum can be found
86
+ *
84
87
  * @example
85
88
  * ```typescript
86
89
  * max([10, 20]); // => 20
87
90
  * max([]); // => Number.NaN
88
91
  * ```
89
- *
90
- * @param values List of numbers
91
- * @returns Maximum value, or `Number.NaN` if no maximum can be found
92
92
  */
93
93
  export declare function max(values: number[]): number;
94
94
  /**
@@ -98,6 +98,17 @@ export declare function max(values: number[]): number;
98
98
  * @param callback Callback to get an item's value
99
99
  * @param first Return only the first item with the minimum value
100
100
  * @returns First item with the minimum value, or `undefined` if no minimum can be found
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * min(
105
+ * [{id: 1, value: 10}, {id: 2, value: 5}],
106
+ * item => item.value,
107
+ * true,
108
+ * ); // => {id: 2, value: 5}
109
+ *
110
+ * min([], item => item.value, true); // => undefined
111
+ * ```
101
112
  */
102
113
  export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
103
114
  /**
@@ -107,6 +118,17 @@ export declare function min<Item>(items: Item[], callback: (item: Item, index: n
107
118
  * @param key Key to use for value
108
119
  * @param first Return only the first item with the minimum value
109
120
  * @returns First item with the minimum value, or `undefined` if no minimum can be found
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * min(
125
+ * [{id: 1, value: 10}, {id: 2, value: 5}],
126
+ * 'value',
127
+ * true,
128
+ * ); // => {id: 2, value: 5}
129
+ *
130
+ * min([], 'value', true); // => undefined
131
+ * ```
110
132
  */
111
133
  export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
112
134
  /**
@@ -115,6 +137,16 @@ export declare function min<Item extends PlainObject>(items: Item[], key: keyof
115
137
  * @param items List of items
116
138
  * @param callback Callback to get an item's value
117
139
  * @returns Items with the minimum value
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * min(
144
+ * [{id: 1, value: 10}, {id: 2, value: 5}],
145
+ * item => item.value,
146
+ * ); // => [{id: 2, value: 5}]
147
+ *
148
+ * min([], item => item.value); // => []
149
+ * ```
118
150
  */
119
151
  export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): Item[];
120
152
  /**
@@ -123,6 +155,16 @@ export declare function min<Item>(items: Item[], callback: (item: Item, index: n
123
155
  * @param items List of items
124
156
  * @param key Key to use for value
125
157
  * @returns Items with the minimum value
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * min(
162
+ * [{id: 1, value: 10}, {id: 2, value: 5}],
163
+ * 'value',
164
+ * ); // => [{id: 2, value: 5}]
165
+ *
166
+ * min([], 'value'); // => []
167
+ * ```
126
168
  */
127
169
  export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
128
170
  /**
@@ -130,6 +172,12 @@ export declare function min<Item extends PlainObject>(items: Item[], key: keyof
130
172
  *
131
173
  * @param values List of numbers
132
174
  * @returns Minimum value, or `Number.NaN` if no minimum can be found
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * min([10, 20]); // => 10
179
+ * min([]); // => Number.NaN
180
+ * ```
133
181
  */
134
182
  export declare function min(values: number[]): number;
135
183
  //#endregion
@@ -44,7 +44,10 @@ function getAggregateCallback(key) {
44
44
  }
45
45
  function getAggregated(type, array, key, first) {
46
46
  const aggregation = aggregate(type, array, key, first);
47
- if (aggregation.count === 0) return aggregation.array ? aggregation.first ? void 0 : [] : NaN;
47
+ if (aggregation.count === 0) {
48
+ if (aggregation.array) return aggregation.first ? void 0 : [];
49
+ return NaN;
50
+ }
48
51
  if (aggregation.array) {
49
52
  const array = aggregation.items[aggregation.value];
50
53
  return aggregation.first ? array[0] : array;
@@ -5,6 +5,13 @@
5
5
  * @param value Number to round down
6
6
  * @param decimals Number of decimal places to round to _(defaults to `0`)_
7
7
  * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * floor(10.567, 2); // => 10.56
12
+ * floor(10.567); // => 10
13
+ * floor('abc'); // => Number.NaN
14
+ * ```
8
15
  */
9
16
  export declare function floor(value: number, decimals?: number): number;
10
17
  /**
@@ -13,6 +20,13 @@ export declare function floor(value: number, decimals?: number): number;
13
20
  * @param value Number to round
14
21
  * @param decimals Number of decimal places to round to _(defaults to `0`)_
15
22
  * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * round(10.567, 2); // => 10.57
27
+ * round(10.567); // => 11
28
+ * round('abc'); // => Number.NaN
29
+ * ```
16
30
  */
17
31
  export declare function round(value: number, decimals?: number): number;
18
32
  export declare function roundNumber(callback: (value: number) => number, value: number, decimals?: number): number;
@@ -5,6 +5,13 @@
5
5
  * @param value Number to round down
6
6
  * @param decimals Number of decimal places to round to _(defaults to `0`)_
7
7
  * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * floor(10.567, 2); // => 10.56
12
+ * floor(10.567); // => 10
13
+ * floor('abc'); // => Number.NaN
14
+ * ```
8
15
  */
9
16
  function floor(value, decimals) {
10
17
  return roundNumber(Math.floor, value, decimals);
@@ -15,6 +22,13 @@ function floor(value, decimals) {
15
22
  * @param value Number to round
16
23
  * @param decimals Number of decimal places to round to _(defaults to `0`)_
17
24
  * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * round(10.567, 2); // => 10.57
29
+ * round(10.567); // => 11
30
+ * round('abc'); // => Number.NaN
31
+ * ```
18
32
  */
19
33
  function round(value, decimals) {
20
34
  return roundNumber(Math.round, value, decimals);
@@ -4,8 +4,15 @@
4
4
  *
5
5
  * @param value Value to check
6
6
  * @param minimum Minimum value
7
- * @param maximum Maximum value
7
+ * @param maximum Maximum value _(inclusive)_
8
8
  * @returns `true` if the value is between the minimum and maximum, otherwise `false`
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * between(5, 1, 10); // => true
13
+ * between(0, 1, 10); // => false
14
+ * between(10, 10, 10); // => true
15
+ * ```
9
16
  */
10
17
  export declare function between(value: number, minimum: number, maximum: number): boolean;
11
18
  /**
@@ -31,6 +38,17 @@ export declare function clamp(value: number, minimum: number, maximum: number, l
31
38
  *
32
39
  * @param value Original value
33
40
  * @returns Original value as a number, or `Number.NaN` if the value is unable to be parsed
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * getNumber(null) // => Number.NaN
45
+ * getNumber('foo') // => Number.NaN
46
+ * getNumber('123') // => 123
47
+ * getNumber(true) // => 1
48
+ * getNumber([1, 2, 3]) // => Number.NaN
49
+ * getNumber({a: 1}) // => Number.NaN
50
+ * getNumber(() => 123) // => 123
51
+ * ```
34
52
  */
35
53
  export declare function getNumber(value: unknown): number;
36
54
  //#endregion
@@ -1,4 +1,5 @@
1
1
  import { isNumber } from "./is.mjs";
2
+ import { max, min } from "./math/aggregate.mjs";
2
3
  import { EXPRESSION_NUMBER_BINARY, EXPRESSION_NUMBER_OCTAL, EXPRESSION_NUMBER_ZEROISH } from "../models/misc.model.mjs";
3
4
  //#region src/internal/number.ts
4
5
  /**
@@ -6,8 +7,15 @@ import { EXPRESSION_NUMBER_BINARY, EXPRESSION_NUMBER_OCTAL, EXPRESSION_NUMBER_ZE
6
7
  *
7
8
  * @param value Value to check
8
9
  * @param minimum Minimum value
9
- * @param maximum Maximum value
10
+ * @param maximum Maximum value _(inclusive)_
10
11
  * @returns `true` if the value is between the minimum and maximum, otherwise `false`
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * between(5, 1, 10); // => true
16
+ * between(0, 1, 10); // => false
17
+ * between(10, 10, 10); // => true
18
+ * ```
11
19
  */
12
20
  function between(value, minimum, maximum) {
13
21
  if (![
@@ -16,7 +24,8 @@ function between(value, minimum, maximum) {
16
24
  maximum
17
25
  ].every(isNumber)) return false;
18
26
  if (Object.is(minimum, maximum)) return Object.is(value, minimum);
19
- return value >= (maximum > minimum ? minimum : maximum) && value <= (maximum > minimum ? maximum : minimum);
27
+ const maximumValue = max([maximum, minimum]);
28
+ return value >= min([maximum, minimum]) && value <= maximumValue;
20
29
  }
21
30
  /**
22
31
  * Clamp a number between a minimum and maximum value
@@ -49,6 +58,17 @@ function clamp(value, minimum, maximum, loop) {
49
58
  *
50
59
  * @param value Original value
51
60
  * @returns Original value as a number, or `Number.NaN` if the value is unable to be parsed
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * getNumber(null) // => Number.NaN
65
+ * getNumber('foo') // => Number.NaN
66
+ * getNumber('123') // => 123
67
+ * getNumber(true) // => 1
68
+ * getNumber([1, 2, 3]) // => Number.NaN
69
+ * getNumber({a: 1}) // => Number.NaN
70
+ * getNumber(() => 123) // => 123
71
+ * ```
52
72
  */
53
73
  function getNumber(value) {
54
74
  if (typeof value === "number") return value;
@@ -4,7 +4,7 @@ import { isError, isOk } from "../result/misc.mjs";
4
4
  //#region src/internal/plan/plan.generate.ts
5
5
  async function asyncGenerate(input, unwrap, first, second) {
6
6
  const aborter = createAborter(second ?? first, noop);
7
- if (aborter != null && aborter.signal.aborted) throw aborter.signal.reason;
7
+ if (aborter?.signal.aborted) throw aborter.signal.reason;
8
8
  const parameters = Array.isArray(first) ? first : [];
9
9
  let generator;
10
10
  if (typeof input === "function") generator = input(...parameters);
@@ -13,7 +13,7 @@ async function asyncGenerate(input, unwrap, first, second) {
13
13
  let lastValue;
14
14
  try {
15
15
  while (true) {
16
- if (aborter != null && aborter.signal.aborted) await generator.throw(aborter.signal.reason);
16
+ if (aborter?.signal.aborted) await generator.throw(aborter.signal.reason);
17
17
  const next = await generator.next(lastValue);
18
18
  const done = next.done === true;
19
19
  const { value } = next;
@@ -29,7 +29,7 @@ async function asyncGenerate(input, unwrap, first, second) {
29
29
  aborter?.cancel();
30
30
  await generator.return(lastValue);
31
31
  }
32
- if (aborter != null && aborter.signal.aborted) throw aborter.signal.reason;
32
+ if (aborter?.signal.aborted) throw aborter.signal.reason;
33
33
  if (success) return unwrap && isOk(lastValue) ? lastValue.value : lastValue;
34
34
  throw lastValue;
35
35
  }