@oscarpalmer/atoms 0.205.0 → 0.206.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 (40) hide show
  1. package/dist/array/reverse.mjs +2 -1
  2. package/dist/array/single.d.mts +3 -3
  3. package/dist/array/sort.mjs +3 -2
  4. package/dist/color/misc/alpha.mjs +1 -1
  5. package/dist/color/misc/get.mjs +1 -1
  6. package/dist/color/space/hsl.mjs +7 -2
  7. package/dist/color/space/hwb.mjs +2 -1
  8. package/dist/color/space/rgb.mjs +22 -13
  9. package/dist/function/retry.mjs +12 -14
  10. package/dist/index.d.mts +53 -53
  11. package/dist/index.mjs +258 -258
  12. package/dist/internal/array/insert.mjs +2 -1
  13. package/dist/internal/function/limit.mjs +9 -9
  14. package/dist/internal/math/aggregate.d.mts +41 -0
  15. package/dist/internal/math/aggregate.mjs +4 -1
  16. package/dist/internal/math/misc.d.mts +8 -0
  17. package/dist/internal/math/misc.mjs +11 -1
  18. package/dist/internal/random.mjs +2 -1
  19. package/dist/internal/result/attempt.d.mts +59 -0
  20. package/dist/internal/result/attempt.mjs +24 -0
  21. package/dist/internal/string/misc.mjs +4 -5
  22. package/dist/internal/value/equal.mjs +2 -1
  23. package/dist/internal/value/misc.mjs +1 -1
  24. package/dist/math.d.mts +3 -52
  25. package/dist/math.mjs +3 -16
  26. package/dist/promise/constants.d.mts +0 -1
  27. package/dist/promise/constants.mjs +1 -2
  28. package/dist/promise/helpers.mjs +1 -1
  29. package/dist/promise/index.mjs +9 -9
  30. package/dist/promise/misc.mjs +1 -1
  31. package/dist/queue.mjs +11 -6
  32. package/dist/result/index.d.mts +2 -59
  33. package/dist/result/index.mjs +1 -23
  34. package/dist/result/work/flow.mjs +2 -1
  35. package/dist/result/work/pipe.mjs +2 -1
  36. package/dist/string/fuzzy.mjs +7 -5
  37. package/dist/string/index.mjs +2 -1
  38. package/dist/value/clone.mjs +5 -8
  39. package/dist/value/diff.mjs +3 -2
  40. package/package.json +1 -1
@@ -1,3 +1,4 @@
1
+ import { floor } from "../internal/math/misc.mjs";
1
2
  //#region src/array/reverse.ts
2
3
  /**
3
4
  * Reverse the order of items in an array
@@ -9,7 +10,7 @@ function reverse(array) {
9
10
  if (!Array.isArray(array)) return [];
10
11
  const { length } = array;
11
12
  if (length < 2) return array;
12
- const half = Math.floor(length / 2);
13
+ const half = floor(length / 2);
13
14
  for (let firstIndex = 0; firstIndex < half; firstIndex += 1) {
14
15
  const temporaryItem = array[firstIndex];
15
16
  const secondIndex = length - 1 - firstIndex;
@@ -3,7 +3,7 @@ import { PlainObject } from "../models.mjs";
3
3
  /**
4
4
  * Get the _only_ item matching the given value
5
5
  *
6
- * Throws an error if multiple items match the value
6
+ * _Throws an error if multiple items match the value_
7
7
  *
8
8
  * @param array Array to search in
9
9
  * @param callback Callback to get an item's value for matching
@@ -23,7 +23,7 @@ export declare function single<Item, Callback extends (item: Item, index: number
23
23
  /**
24
24
  * Get the _only_ item matching the given value by key
25
25
  *
26
- * Throws an error if multiple items match the value
26
+ * _Throws an error if multiple items match the value_
27
27
  *
28
28
  * @param array Array to search in
29
29
  * @param key Key to get an item's value for matching
@@ -43,7 +43,7 @@ export declare function single<Item extends PlainObject, ItemKey extends keyof I
43
43
  /**
44
44
  * Get the _only_ item matching the filter
45
45
  *
46
- * Throws an error if multiple items match the filter
46
+ * _Throws an error if multiple items match the filter_
47
47
  *
48
48
  * @param array Array to search in
49
49
  * @param filter Filter callback to match items
@@ -1,4 +1,5 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
+ import { floor, round } from "../internal/math/misc.mjs";
2
3
  import { compare } from "../internal/value/compare.mjs";
3
4
  //#region src/array/sort.ts
4
5
  function Sorter(sorters) {
@@ -48,7 +49,7 @@ function getSortedArrayIndex(array, item) {
48
49
  let low = 0;
49
50
  let high = length - 1;
50
51
  while (low <= high) {
51
- const mid = Math.floor((low + high) / 2);
52
+ const mid = floor((low + high) / 2);
52
53
  if (getComparisonValue(item, array[mid], sorters, sortersLength) < 0) high = mid - 1;
53
54
  else low = mid + 1;
54
55
  }
@@ -103,7 +104,7 @@ function isSortedArray(array) {
103
104
  const sortersLength = sorters.length;
104
105
  let offset = 0;
105
106
  if (length >= SORT_THRESHOLD) {
106
- offset = Math.round(length / SORT_PEEK_PERCENTAGE);
107
+ offset = round(length / SORT_PEEK_PERCENTAGE);
107
108
  offset = offset > SORT_THRESHOLD ? SORT_THRESHOLD : offset;
108
109
  for (let index = 0; index < offset; index += 1) {
109
110
  const [firstItem, firstOffset] = [array[index], array[index + 1]];
@@ -1,6 +1,6 @@
1
+ import { round } from "../../internal/math/misc.mjs";
1
2
  import { clamp } from "../../internal/number.mjs";
2
3
  import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX, COLOR_SYMBOL } from "../constants.mjs";
3
- import { round } from "../../internal/math/misc.mjs";
4
4
  //#region src/color/misc/alpha.ts
5
5
  function getAlpha(value, hex) {
6
6
  if (typeof value === "number") return getAlphaFromValue(value);
@@ -1,6 +1,6 @@
1
+ import { round } from "../../internal/math/misc.mjs";
1
2
  import { clamp } from "../../internal/number.mjs";
2
3
  import { COLOR_DEFAULTS, COLOR_MAX, COLOR_SRGB, COLOR_TYPE } from "../constants.mjs";
3
- import { round } from "../../internal/math/misc.mjs";
4
4
  import { getColorFromState, getColorState } from "./state.mjs";
5
5
  import { color } from "../instance.mjs";
6
6
  //#region src/color/misc/get.ts
@@ -1,3 +1,4 @@
1
+ import { max, min } from "../../internal/math/aggregate.mjs";
1
2
  import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
2
3
  import { getAlphaValue } from "../misc/alpha.mjs";
3
4
  import { getDegrees, getHexValue, getPercentage } from "../misc/get.mjs";
@@ -9,7 +10,7 @@ function convertHslToHwba(input) {
9
10
  let { hue, lightness, saturation } = isHslLike(input) ? getHslValues(input) : { ...COLOR_DEFAULTS.hsl };
10
11
  lightness /= COLOR_MAX.percent;
11
12
  saturation /= COLOR_MAX.percent;
12
- const value = lightness + saturation * Math.min(lightness, 1 - lightness);
13
+ const value = lightness + saturation * min([lightness, 1 - lightness]);
13
14
  if (value === 0) saturation = 0;
14
15
  else saturation = 2 * (1 - lightness / value);
15
16
  const blackness = 1 - value;
@@ -46,7 +47,11 @@ function getColorFromHsl(state, type) {
46
47
  }
47
48
  function getHexyValue(hue, lightness, saturation, value) {
48
49
  const part = (value + hue / 30) % 12;
49
- return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * COLOR_MAX.hex;
50
+ return (lightness - saturation * min([lightness, 1 - lightness]) * max([-1, min([
51
+ part - 3,
52
+ 9 - part,
53
+ 1
54
+ ])])) * COLOR_MAX.hex;
50
55
  }
51
56
  function getHslValue() {
52
57
  return getStateValue(this, COLOR_TYPE.hsl, false);
@@ -1,3 +1,4 @@
1
+ import { min } from "../../internal/math/aggregate.mjs";
1
2
  import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
2
3
  import { getAlphaValue } from "../misc/alpha.mjs";
3
4
  import { getDegrees, getPercentage } from "../misc/get.mjs";
@@ -38,7 +39,7 @@ function getColorFromHwb(state, type) {
38
39
  function getHwbSaturation(blackness, whiteness, lightness) {
39
40
  const hue = 1 - blackness - whiteness;
40
41
  if (hue <= 0) return 0;
41
- return hue / Math.min(2 * lightness, 2 - 2 * lightness);
42
+ return hue / min([2 * lightness, 2 - 2 * lightness]);
42
43
  }
43
44
  function getHwbValue() {
44
45
  return getStateValue(this, COLOR_TYPE.hwb, false);
@@ -1,3 +1,4 @@
1
+ import { max, min } from "../../internal/math/aggregate.mjs";
1
2
  import { join } from "../../internal/string/misc.mjs";
2
3
  import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
3
4
  import { getAlpha, getAlphaValue } from "../misc/alpha.mjs";
@@ -20,12 +21,12 @@ function convertRgbToHex(rgb, alpha) {
20
21
  }
21
22
  function convertRgbToHsla(value) {
22
23
  const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
23
- const { delta, max, min } = values;
24
- const lightness = (min + max) / 2;
24
+ const { delta, maximum, minimum } = values;
25
+ const lightness = (minimum + maximum) / 2;
25
26
  let hue = 0;
26
27
  let saturation = 0;
27
28
  if (delta !== 0) {
28
- saturation = (max - lightness) / Math.min(lightness, 1 - lightness);
29
+ saturation = (maximum - lightness) / min([lightness, 1 - lightness]);
29
30
  hue = getRgbHue(values);
30
31
  hue *= 60;
31
32
  }
@@ -38,7 +39,7 @@ function convertRgbToHsla(value) {
38
39
  }
39
40
  function convertRgbToHwba(value) {
40
41
  const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
41
- const { delta, max, min } = values;
42
+ const { delta, maximum, minimum } = values;
42
43
  let hue = 0;
43
44
  if (delta !== 0) {
44
45
  hue = getRgbHue(values);
@@ -46,8 +47,8 @@ function convertRgbToHwba(value) {
46
47
  }
47
48
  return {
48
49
  hue,
49
- whiteness: min * COLOR_MAX.percent,
50
- blackness: (1 - max) * COLOR_MAX.percent,
50
+ whiteness: minimum * COLOR_MAX.percent,
51
+ blackness: (1 - maximum) * COLOR_MAX.percent,
51
52
  alpha: getAlphaValue(value?.alpha ?? COLOR_MAX.percent)
52
53
  };
53
54
  }
@@ -67,14 +68,22 @@ function getRgbCalculationValues(rgb) {
67
68
  const blue = rgb.blue / COLOR_MAX.hex;
68
69
  const green = rgb.green / COLOR_MAX.hex;
69
70
  const red = rgb.red / COLOR_MAX.hex;
70
- const max = Math.max(blue, green, red);
71
- const min = Math.min(blue, green, red);
71
+ const maximum = max([
72
+ blue,
73
+ green,
74
+ red
75
+ ]);
76
+ const minimum = min([
77
+ blue,
78
+ green,
79
+ red
80
+ ]);
72
81
  return {
73
82
  blue,
74
- delta: max - min,
83
+ delta: maximum - minimum,
75
84
  green,
76
- max,
77
- min,
85
+ maximum,
86
+ minimum,
78
87
  red
79
88
  };
80
89
  }
@@ -85,8 +94,8 @@ function getRgbaColor() {
85
94
  return getStateValue(this, COLOR_TYPE.rgb, true);
86
95
  }
87
96
  function getRgbHue(values) {
88
- const { blue, delta, green, max, red } = values;
89
- switch (max) {
97
+ const { blue, delta, green, maximum, red } = values;
98
+ switch (maximum) {
90
99
  case blue: return (red - green) / delta + 4;
91
100
  case green: return (blue - red) / delta + 2;
92
101
  default: return (green - blue) / delta + (green < blue ? 6 : 0);
@@ -1,4 +1,5 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
+ import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
3
  import { getNumberOrDefault } from "../internal/defaults.mjs";
3
4
  import { LIMITER_WAIT, getLimiter } from "../internal/function/limit.mjs";
4
5
  //#region src/function/retry.ts
@@ -25,15 +26,12 @@ var RetryError = class extends Error {
25
26
  async function asyncRetry(callback, options) {
26
27
  if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
27
28
  async function handle() {
28
- try {
29
- const result = await callback();
30
- resolver(result);
31
- } catch (error) {
32
- if (attempts >= times || !when(error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, error));
33
- else {
34
- attempts += 1;
35
- limiter.run();
36
- }
29
+ const result = await asyncAttempt(async () => callback());
30
+ if (result.ok) resolver(result.value);
31
+ else if (attempts >= times || !when(result.error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, result.error));
32
+ else {
33
+ attempts += 1;
34
+ limiter.run();
37
35
  }
38
36
  }
39
37
  const { delay, times, when } = createRetryOptions(options);
@@ -66,11 +64,11 @@ function retry(callback, options) {
66
64
  if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
67
65
  const { times, when } = createRetryOptions(options);
68
66
  let last;
69
- for (let index = 0; index <= times; index += 1) try {
70
- return callback();
71
- } catch (error) {
72
- if (index >= times || !when(error)) {
73
- last = error;
67
+ for (let index = 0; index <= times; index += 1) {
68
+ const result = attempt(callback);
69
+ if (result.ok) return result.value;
70
+ if (index >= times || !when(result.error)) {
71
+ last = result.error;
74
72
  break;
75
73
  }
76
74
  }
package/dist/index.d.mts CHANGED
@@ -1482,7 +1482,7 @@ export declare function reverse<Item>(array: Item[]): Item[];
1482
1482
  /**
1483
1483
  * Get the _only_ item matching the given value
1484
1484
  *
1485
- * Throws an error if multiple items match the value
1485
+ * _Throws an error if multiple items match the value_
1486
1486
  *
1487
1487
  * @param array Array to search in
1488
1488
  * @param callback Callback to get an item's value for matching
@@ -1502,7 +1502,7 @@ export declare function single<Item, Callback extends (item: Item, index: number
1502
1502
  /**
1503
1503
  * Get the _only_ item matching the given value by key
1504
1504
  *
1505
- * Throws an error if multiple items match the value
1505
+ * _Throws an error if multiple items match the value_
1506
1506
  *
1507
1507
  * @param array Array to search in
1508
1508
  * @param key Key to get an item's value for matching
@@ -1522,7 +1522,7 @@ export declare function single<Item extends PlainObject, ItemKey extends keyof I
1522
1522
  /**
1523
1523
  * Get the _only_ item matching the filter
1524
1524
  *
1525
- * Throws an error if multiple items match the filter
1525
+ * _Throws an error if multiple items match the filter_
1526
1526
  *
1527
1527
  * @param array Array to search in
1528
1528
  * @param filter Filter callback to match items
@@ -5001,7 +5001,7 @@ export declare function timed<Value>(promise: Promise<Value>, options: RequiredK
5001
5001
  */
5002
5002
  export declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
5003
5003
  //#endregion
5004
- //#region src/result/index.d.ts
5004
+ //#region src/internal/result/attempt.d.ts
5005
5005
  /**
5006
5006
  * Executes a _Promise_, catching any errors, and returns a result
5007
5007
  *
@@ -7731,8 +7731,57 @@ export declare function max<Item extends PlainObject>(items: Item[], key: keyof
7731
7731
  * @returns Maximum value, or `Number.NaN` if no maximum can be found
7732
7732
  */
7733
7733
  export declare function max(values: number[]): number;
7734
+ /**
7735
+ * Get the minimum value from a list of items
7736
+ *
7737
+ * @param items List of items
7738
+ * @param callback Callback to get an item's value
7739
+ * @param first Return only the first item with the minimum value
7740
+ * @returns First item with the minimum value, or `undefined` if no minimum can be found
7741
+ */
7742
+ export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
7743
+ /**
7744
+ * Get the minimum value from a list of items
7745
+ *
7746
+ * @param items List of items
7747
+ * @param key Key to use for value
7748
+ * @param first Return only the first item with the minimum value
7749
+ * @returns First item with the minimum value, or `undefined` if no minimum can be found
7750
+ */
7751
+ export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
7752
+ /**
7753
+ * Get the minimum value from a list of items
7754
+ *
7755
+ * @param items List of items
7756
+ * @param callback Callback to get an item's value
7757
+ * @returns Items with the minimum value
7758
+ */
7759
+ export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): Item[];
7760
+ /**
7761
+ * Get the minimum value from a list of items
7762
+ *
7763
+ * @param items List of items
7764
+ * @param key Key to use for value
7765
+ * @returns Items with the minimum value
7766
+ */
7767
+ export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
7768
+ /**
7769
+ * Get the minimum value from a list of numbers
7770
+ *
7771
+ * @param values List of numbers
7772
+ * @returns Minimum value, or `Number.NaN` if no minimum can be found
7773
+ */
7774
+ export declare function min(values: number[]): number;
7734
7775
  //#endregion
7735
7776
  //#region src/internal/math/misc.d.ts
7777
+ /**
7778
+ * Round a number down
7779
+ *
7780
+ * @param value Number to round down
7781
+ * @param decimals Number of decimal places to round to _(defaults to `0`)_
7782
+ * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
7783
+ */
7784
+ export declare function floor(value: number, decimals?: number): number;
7736
7785
  /**
7737
7786
  * Round a number
7738
7787
  *
@@ -7799,14 +7848,6 @@ export declare function count<Item extends PlainObject, ItemKey extends keyof It
7799
7848
  * @returns Number of items, or `Number.NaN` if no count can be calculated
7800
7849
  */
7801
7850
  export declare function count(values: unknown[]): number;
7802
- /**
7803
- * Round a number down
7804
- *
7805
- * @param value Number to round down
7806
- * @param decimals Number of decimal places to round to _(defaults to `0`)_
7807
- * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
7808
- */
7809
- export declare function floor(value: number, decimals?: number): number;
7810
7851
  /**
7811
7852
  * Get the median value from a list of items
7812
7853
  *
@@ -7830,47 +7871,6 @@ export declare function median<Item extends PlainObject>(array: Item[], key: key
7830
7871
  * @returns Median value, or `Number.NaN` if no median can be calculated
7831
7872
  */
7832
7873
  export declare function median(array: number[]): number;
7833
- /**
7834
- * Get the minimum value from a list of items
7835
- *
7836
- * @param items List of items
7837
- * @param callback Callback to get an item's value
7838
- * @param first Return only the first item with the minimum value
7839
- * @returns First item with the minimum value, or `undefined` if no minimum can be found
7840
- */
7841
- export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
7842
- /**
7843
- * Get the minimum value from a list of items
7844
- *
7845
- * @param items List of items
7846
- * @param key Key to use for value
7847
- * @param first Return only the first item with the minimum value
7848
- * @returns First item with the minimum value, or `undefined` if no minimum can be found
7849
- */
7850
- export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
7851
- /**
7852
- * Get the minimum value from a list of items
7853
- *
7854
- * @param items List of items
7855
- * @param callback Callback to get an item's value
7856
- * @returns Items with the minimum value
7857
- */
7858
- export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): Item[];
7859
- /**
7860
- * Get the minimum value from a list of items
7861
- *
7862
- * @param items List of items
7863
- * @param key Key to use for value
7864
- * @returns Items with the minimum value
7865
- */
7866
- export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
7867
- /**
7868
- * Get the minimum value from a list of numbers
7869
- *
7870
- * @param values List of numbers
7871
- * @returns Minimum value, or `Number.NaN` if no minimum can be found
7872
- */
7873
- export declare function min(values: number[]): number;
7874
7874
  /**
7875
7875
  * Get the sum of a list of items
7876
7876
  *