@oscarpalmer/atoms 0.142.0 → 0.143.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 (59) hide show
  1. package/dist/atoms.full.js +108 -40
  2. package/dist/color/misc/is.js +6 -6
  3. package/dist/function/memoize.js +1 -1
  4. package/dist/index.js +7 -6
  5. package/dist/internal/math/aggregate.js +12 -8
  6. package/dist/internal/number.js +1 -1
  7. package/dist/internal/value/partial.js +14 -0
  8. package/dist/is.js +11 -3
  9. package/dist/math.js +21 -5
  10. package/dist/promise.js +30 -4
  11. package/dist/result.js +7 -5
  12. package/dist/sized/map.js +1 -1
  13. package/dist/sized/set.js +2 -2
  14. package/dist/value/misc.js +3 -2
  15. package/dist/value/omit.js +11 -0
  16. package/dist/value/pick.js +11 -0
  17. package/package.json +1 -1
  18. package/src/color/misc/is.ts +6 -6
  19. package/src/function/debounce.ts +2 -2
  20. package/src/function/memoize.ts +1 -1
  21. package/src/function/throttle.ts +2 -2
  22. package/src/index.ts +2 -1
  23. package/src/internal/function/limiter.ts +3 -3
  24. package/src/internal/math/aggregate.ts +19 -10
  25. package/src/internal/number.ts +1 -1
  26. package/src/internal/value/partial.ts +46 -0
  27. package/src/is.ts +11 -2
  28. package/src/math.ts +67 -9
  29. package/src/models.ts +2 -2
  30. package/src/promise.ts +52 -15
  31. package/src/random.ts +2 -2
  32. package/src/result.ts +31 -11
  33. package/src/sized/map.ts +1 -1
  34. package/src/sized/set.ts +2 -2
  35. package/src/value/misc.ts +2 -1
  36. package/src/value/omit.ts +19 -0
  37. package/src/value/pick.ts +19 -0
  38. package/types/color/misc/is.d.ts +6 -6
  39. package/types/function/debounce.d.ts +2 -2
  40. package/types/function/memoize.d.ts +1 -1
  41. package/types/function/throttle.d.ts +2 -2
  42. package/types/index.d.ts +2 -1
  43. package/types/internal/function/limiter.d.ts +2 -2
  44. package/types/internal/math/aggregate.d.ts +1 -0
  45. package/types/internal/number.d.ts +1 -1
  46. package/types/internal/value/partial.d.ts +2 -0
  47. package/types/is.d.ts +8 -2
  48. package/types/math.d.ts +20 -0
  49. package/types/models.d.ts +2 -2
  50. package/types/promise.d.ts +26 -11
  51. package/types/random.d.ts +2 -2
  52. package/types/result.d.ts +21 -6
  53. package/types/sized/map.d.ts +1 -1
  54. package/types/sized/set.d.ts +2 -2
  55. package/types/value/misc.d.ts +2 -1
  56. package/types/value/omit.d.ts +8 -0
  57. package/types/value/{partial.d.ts → pick.d.ts} +1 -1
  58. package/dist/value/partial.js +0 -17
  59. package/src/value/partial.ts +0 -39
@@ -273,24 +273,27 @@ function aggregate(type, array, key) {
273
273
  value: NaN
274
274
  };
275
275
  const aggregator = aggregators[type];
276
- const isCallback = typeof key === "function";
276
+ const callback = getAggregateCallback(key);
277
277
  let counted = 0;
278
278
  let aggregated = NaN;
279
279
  let notNumber = true;
280
280
  for (let index = 0; index < length; index += 1) {
281
281
  const item = array[index];
282
- const value = isCallback ? key(item, index, array) : item[key] ?? item;
283
- if (typeof value === "number" && !Number.isNaN(value)) {
284
- aggregated = aggregator(aggregated, value, notNumber);
285
- counted += 1;
286
- notNumber = false;
287
- }
282
+ const value = callback == null ? item : callback(item, index, array);
283
+ if (!isNumber(value)) continue;
284
+ aggregated = aggregator(aggregated, value, notNumber);
285
+ counted += 1;
286
+ notNumber = false;
288
287
  }
289
288
  return {
290
289
  count: counted,
291
290
  value: aggregated
292
291
  };
293
292
  }
293
+ function getAggregateCallback(key) {
294
+ if (key == null) return;
295
+ return typeof key === "function" ? key : (item) => item[key];
296
+ }
294
297
  function max(array, key) {
295
298
  return getAggregated("max", array, key);
296
299
  }
@@ -665,7 +668,7 @@ function clamp(value, minimum, maximum, loop) {
665
668
  /**
666
669
  * Get the number value from an unknown value _(based on Lodash)_
667
670
  * @param value Original value
668
- * @returns The value as a number, or `NaN` if the value is unable to be parsed
671
+ * @returns Original value as a number, or `NaN` if the value is unable to be parsed
669
672
  */
670
673
  function getNumber(value) {
671
674
  if (typeof value === "number") return value;
@@ -699,7 +702,7 @@ const MAXIMUM_DEFAULT = 1048576;
699
702
  /**
700
703
  * A Map with a maximum size
701
704
  *
702
- * Behaviour is similar to a _LRU_-cache, where the least recently used entries are removed
705
+ * Behavior is similar to a _LRU_-cache, where the least recently used entries are removed
703
706
  */
704
707
  var SizedMap = class extends Map {
705
708
  /**
@@ -795,7 +798,7 @@ var Memoized = class {
795
798
  /**
796
799
  * Get a result from the cache
797
800
  * @param key Key to get
798
- * @returns The cached result or `undefined` if it does not exist
801
+ * @returns Cached result or `undefined` if it does not exist
799
802
  */
800
803
  get(key) {
801
804
  return this.#state.cache?.get(key);
@@ -1637,22 +1640,37 @@ function mergeValues(values, options, validate, prefix) {
1637
1640
  const actual = validate ? values.filter(isArrayOrPlainObject) : values;
1638
1641
  return actual.length > 1 ? mergeObjects(actual, options, prefix) : actual[0] ?? {};
1639
1642
  }
1640
- /**
1641
- * Create a new object with only the specified keys
1642
- * @param value Original object
1643
- * @param keys Keys to use
1644
- * @returns Partial object with only the specified keys
1645
- */
1646
- function partial(value, keys) {
1647
- if (typeof value !== "object" || value === null || Object.keys(value).length === 0 || !Array.isArray(keys) || keys.length === 0) return {};
1643
+ function partial(value, providedKeys, omit) {
1644
+ if (typeof value !== "object" || value === null) return {};
1645
+ const keys = omit ? Object.keys(value) : Array.isArray(providedKeys) ? providedKeys : [];
1648
1646
  const { length } = keys;
1647
+ if (length === 0) return omit ? value : {};
1649
1648
  const partials = {};
1650
1649
  for (let index = 0; index < length; index += 1) {
1651
1650
  const key = keys[index];
1652
- if (key in value) partials[key] = value[key];
1651
+ if (!(key in value)) continue;
1652
+ if (omit ? !providedKeys.includes(key) : true) partials[key] = value[key];
1653
1653
  }
1654
1654
  return partials;
1655
1655
  }
1656
+ /**
1657
+ * Create a new object without the specified keys
1658
+ * @param value Original object
1659
+ * @param keys Keys to omit
1660
+ * @returns Partial object without the specified keys
1661
+ */
1662
+ function omit(value, keys) {
1663
+ return partial(value, keys, true);
1664
+ }
1665
+ /**
1666
+ * Create a new object with only the specified keys
1667
+ * @param value Original object
1668
+ * @param keys Keys to use
1669
+ * @returns Partial object with only the specified keys
1670
+ */
1671
+ function pick(value, keys) {
1672
+ return partial(value, keys, false);
1673
+ }
1656
1674
  function flattenObject(value, depth, smushed, prefix) {
1657
1675
  if (depth >= MAX_DEPTH) return {};
1658
1676
  if (smushed.has(value)) return smushed.get(value);
@@ -1983,7 +2001,7 @@ function isBytey(value) {
1983
2001
  }
1984
2002
  /**
1985
2003
  * Is the value a Color?
1986
- * @param value The value to check
2004
+ * @param value Value to check
1987
2005
  * @returns `true` if the value is a Color, otherwise `false`
1988
2006
  */
1989
2007
  function isColor(value) {
@@ -2005,7 +2023,7 @@ function isDegree(value) {
2005
2023
  }
2006
2024
  /**
2007
2025
  * Is the value a hex color?
2008
- * @param value The value to check
2026
+ * @param value Value to check
2009
2027
  * @param alpha Allow alpha channel? _(defaults to `true`)_
2010
2028
  * @returns `true` if the value is a hex color, otherwise `false`
2011
2029
  */
@@ -2017,7 +2035,7 @@ function isHexColor(value, alpha) {
2017
2035
  }
2018
2036
  /**
2019
2037
  * Is the value an HSLA color?
2020
- * @param value The value to check
2038
+ * @param value Value to check
2021
2039
  * @returns `true` if the value is an HSLA color, otherwise `false`
2022
2040
  */
2023
2041
  function isHslaColor(value) {
@@ -2025,7 +2043,7 @@ function isHslaColor(value) {
2025
2043
  }
2026
2044
  /**
2027
2045
  * Is the value an HSL color?
2028
- * @param value The value to check
2046
+ * @param value Value to check
2029
2047
  * @returns `true` if the value is an HSL color, otherwise `false`
2030
2048
  */
2031
2049
  function isHslColor(value) {
@@ -2036,7 +2054,7 @@ function isHslLike(value) {
2036
2054
  }
2037
2055
  /**
2038
2056
  * Is the value an RGBA color?
2039
- * @param value The value to check
2057
+ * @param value Value to check
2040
2058
  * @returns `true` if the value is an RGBA color, otherwise `false`
2041
2059
  */
2042
2060
  function isRgbaColor(value) {
@@ -2044,7 +2062,7 @@ function isRgbaColor(value) {
2044
2062
  }
2045
2063
  /**
2046
2064
  * Is the value an RGB color?
2047
- * @param value The value to check
2065
+ * @param value Value to check
2048
2066
  * @returns `true` if the value is an RGB color, otherwise `false`
2049
2067
  */
2050
2068
  function isRgbColor(value) {
@@ -2544,8 +2562,8 @@ function getColor(value) {
2544
2562
  }
2545
2563
  /**
2546
2564
  * Is the value empty, or only containing `null` or `undefined` values?
2547
- * @param value Object to check
2548
- * @returns `true` if the object is considered empty, otherwise `false`
2565
+ * @param value Value to check
2566
+ * @returns `true` if the value is considered empty, otherwise `false`
2549
2567
  */
2550
2568
  function isEmpty(value) {
2551
2569
  if (value == null) return true;
@@ -2556,6 +2574,14 @@ function isEmpty(value) {
2556
2574
  return true;
2557
2575
  }
2558
2576
  /**
2577
+ * Is the value not `undefined` or `null`?
2578
+ * @param value Value to check
2579
+ * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
2580
+ */
2581
+ function isNonNullable(value) {
2582
+ return value != null;
2583
+ }
2584
+ /**
2559
2585
  * Is the value `undefined` or `null`?
2560
2586
  * @param value Value to check
2561
2587
  * @returns `true` if the value is `undefined` or `null`, otherwise `false`
@@ -2723,9 +2749,8 @@ function average(array, key) {
2723
2749
  function count(array, key, value) {
2724
2750
  if (!Array.isArray(array)) return NaN;
2725
2751
  const { length } = array;
2726
- if (key == null) return length;
2727
- if (typeof key !== "string" && typeof key !== "function") return NaN;
2728
- const callback = typeof key === "function" ? key : (item) => item[key];
2752
+ const callback = getAggregateCallback(key);
2753
+ if (callback == null) return length;
2729
2754
  let counted = 0;
2730
2755
  for (let index = 0; index < length; index += 1) {
2731
2756
  const item = array[index];
@@ -2733,6 +2758,22 @@ function count(array, key, value) {
2733
2758
  }
2734
2759
  return counted;
2735
2760
  }
2761
+ function median(array, key) {
2762
+ let length = Array.isArray(array) ? array.length : 0;
2763
+ if (!Array.isArray(array) || length === 0) return NaN;
2764
+ if (length === 1) return isNumber(array[0]) ? array[0] : NaN;
2765
+ let values = array;
2766
+ const callback = getAggregateCallback(key);
2767
+ if (callback != null) values = array.map((item, index) => callback(item, index, array));
2768
+ const numbers = values.filter(isNumber).sort((first, second) => first - second);
2769
+ length = numbers.length;
2770
+ if (length % 2 === 0) {
2771
+ const first = length / 2 - 1;
2772
+ const second = length / 2;
2773
+ return (numbers[first] + numbers[second]) / 2;
2774
+ }
2775
+ return numbers[Math.floor(length / 2)];
2776
+ }
2736
2777
  function min(array, key) {
2737
2778
  return getAggregated("min", array, key);
2738
2779
  }
@@ -2751,12 +2792,38 @@ function round(value, decimals) {
2751
2792
  function sum(array, key) {
2752
2793
  return getAggregated("sum", array, key);
2753
2794
  }
2795
+ var CancelablePromise = class extends Promise {
2796
+ #rejector;
2797
+ constructor(executor) {
2798
+ let rejector;
2799
+ super((resolve, reject) => {
2800
+ rejector = reject;
2801
+ executor(resolve, reject);
2802
+ });
2803
+ this.#rejector = rejector;
2804
+ }
2805
+ /**
2806
+ * Cancel the promise, rejecting it with an optional reason
2807
+ * @param reason Optional reason for canceling the promise
2808
+ */
2809
+ cancel(reason) {
2810
+ this.#rejector(reason);
2811
+ }
2812
+ };
2754
2813
  var PromiseTimeoutError = class extends Error {
2755
2814
  constructor() {
2756
2815
  super(MESSAGE_TIMEOUT);
2757
2816
  this.name = ERROR_NAME$1;
2758
2817
  }
2759
2818
  };
2819
+ /**
2820
+ * Create a cancelable promise
2821
+ * @param executor Executor function for the promise
2822
+ * @returns Cancelable promise
2823
+ */
2824
+ function cancelable(executor) {
2825
+ return new CancelablePromise(executor);
2826
+ }
2760
2827
  function delay(options) {
2761
2828
  const { signal, time } = getPromiseOptions(options);
2762
2829
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
@@ -2909,9 +2976,9 @@ async function timed(promise, options) {
2909
2976
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
2910
2977
  return time > 0 ? getTimed(promise, time, signal) : promise;
2911
2978
  }
2912
- async function tryPromise(value, options) {
2979
+ async function attemptPromise(value, options) {
2913
2980
  const isFunction = typeof value === "function";
2914
- if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_TRY));
2981
+ if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
2915
2982
  const { signal, time } = getPromiseOptions(options);
2916
2983
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
2917
2984
  function abort() {
@@ -2938,9 +3005,9 @@ const ABORT_OPTIONS = { once: true };
2938
3005
  const DEFAULT_STRATEGY = "complete";
2939
3006
  const ERROR_NAME$1 = "PromiseTimeoutError";
2940
3007
  const EVENT_NAME$1 = "abort";
3008
+ const MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
2941
3009
  const MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
2942
3010
  const MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
2943
- const MESSAGE_EXPECTATION_TRY = "TryPromise expected a function or a promise";
2944
3011
  const MESSAGE_TIMEOUT = "Promise timed out";
2945
3012
  const strategies = new Set(["complete", "first"]);
2946
3013
  const TYPE_FULFILLED = "fulfilled";
@@ -3317,17 +3384,18 @@ function ok(value) {
3317
3384
  value
3318
3385
  };
3319
3386
  }
3320
- function result(callback, err) {
3387
+ function attempt(callback, err) {
3321
3388
  try {
3322
3389
  return ok(callback());
3323
3390
  } catch (thrown) {
3324
3391
  return getError(err ?? thrown, err == null ? void 0 : thrown);
3325
3392
  }
3326
3393
  }
3327
- result.async = asyncResult;
3328
- async function asyncResult(callback, err) {
3394
+ attempt.async = asyncAttempt;
3395
+ attempt.promise = attemptPromise;
3396
+ async function asyncAttempt(value, err) {
3329
3397
  try {
3330
- return ok(await callback());
3398
+ return ok(await (typeof value === "function" ? value() : value));
3331
3399
  } catch (thrown) {
3332
3400
  return getError(err ?? thrown, err == null ? void 0 : thrown);
3333
3401
  }
@@ -3337,7 +3405,7 @@ function unwrap(value, defaultValue) {
3337
3405
  }
3338
3406
  /**
3339
3407
  * - A Set with a maximum size
3340
- * - Behaviour is similar to a _LRU_-cache, where the oldest values are removed
3408
+ * - Behavior is similar to a _LRU_-cache, where the oldest values are removed
3341
3409
  */
3342
3410
  var SizedSet = class extends Set {
3343
3411
  /**
@@ -3375,7 +3443,7 @@ var SizedSet = class extends Set {
3375
3443
  * Get a value from the SizedSet, if it exists _(and move it to the end)_
3376
3444
  * @param value Value to get from the SizedSet
3377
3445
  * @param update Update the value's position in the SizedSet? _(defaults to `false`)_
3378
- * @returns The value if it exists, otherwise `undefined`
3446
+ * @returns Found value if it exists, otherwise `undefined`
3379
3447
  */
3380
3448
  get(value, update) {
3381
3449
  if (this.has(value)) {
@@ -3387,4 +3455,4 @@ var SizedSet = class extends Set {
3387
3455
  }
3388
3456
  }
3389
3457
  };
3390
- export { frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, average, beacon, between, camelCase, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, memoize, merge, min, noop, ok, parse, partial, pascalCase, promises, push, queue, result, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, tryPromise, unique, unsmush, unwrap, upperCase, words };
3458
+ export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, pascalCase, pick, promises, push, queue, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, upperCase, words };
@@ -11,7 +11,7 @@ function isBytey(value) {
11
11
  }
12
12
  /**
13
13
  * Is the value a Color?
14
- * @param value The value to check
14
+ * @param value Value to check
15
15
  * @returns `true` if the value is a Color, otherwise `false`
16
16
  */
17
17
  function isColor(value) {
@@ -33,7 +33,7 @@ function isDegree(value) {
33
33
  }
34
34
  /**
35
35
  * Is the value a hex color?
36
- * @param value The value to check
36
+ * @param value Value to check
37
37
  * @param alpha Allow alpha channel? _(defaults to `true`)_
38
38
  * @returns `true` if the value is a hex color, otherwise `false`
39
39
  */
@@ -45,7 +45,7 @@ function isHexColor(value, alpha) {
45
45
  }
46
46
  /**
47
47
  * Is the value an HSLA color?
48
- * @param value The value to check
48
+ * @param value Value to check
49
49
  * @returns `true` if the value is an HSLA color, otherwise `false`
50
50
  */
51
51
  function isHslaColor(value) {
@@ -53,7 +53,7 @@ function isHslaColor(value) {
53
53
  }
54
54
  /**
55
55
  * Is the value an HSL color?
56
- * @param value The value to check
56
+ * @param value Value to check
57
57
  * @returns `true` if the value is an HSL color, otherwise `false`
58
58
  */
59
59
  function isHslColor(value) {
@@ -64,7 +64,7 @@ function isHslLike(value) {
64
64
  }
65
65
  /**
66
66
  * Is the value an RGBA color?
67
- * @param value The value to check
67
+ * @param value Value to check
68
68
  * @returns `true` if the value is an RGBA color, otherwise `false`
69
69
  */
70
70
  function isRgbaColor(value) {
@@ -72,7 +72,7 @@ function isRgbaColor(value) {
72
72
  }
73
73
  /**
74
74
  * Is the value an RGB color?
75
- * @param value The value to check
75
+ * @param value Value to check
76
76
  * @returns `true` if the value is an RGB color, otherwise `false`
77
77
  */
78
78
  function isRgbColor(value) {
@@ -54,7 +54,7 @@ var Memoized = class {
54
54
  /**
55
55
  * Get a result from the cache
56
56
  * @param key Key to get
57
- * @returns The cached result or `undefined` if it does not exist
57
+ * @returns Cached result or `undefined` if it does not exist
58
58
  */
59
59
  get(key) {
60
60
  return this.#state.cache?.get(key);
package/dist/index.js CHANGED
@@ -45,16 +45,17 @@ import { template } from "./string/template.js";
45
45
  import { clone } from "./value/clone.js";
46
46
  import { diff } from "./value/diff.js";
47
47
  import { merge } from "./value/merge.js";
48
- import { partial } from "./value/partial.js";
48
+ import { omit } from "./value/omit.js";
49
+ import { pick } from "./value/pick.js";
49
50
  import { smush } from "./value/smush.js";
50
51
  import { unsmush } from "./value/unsmush.js";
51
- import { isEmpty, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
52
+ import { isEmpty, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
52
53
  import { logger } from "./logger.js";
53
- import { average, count, min, round, sum } from "./math.js";
54
- import { PromiseTimeoutError, delay, isFulfilled, isRejected, promises, timed, tryPromise } from "./promise.js";
54
+ import { average, count, median, min, round, sum } from "./math.js";
55
+ import { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, isFulfilled, isRejected, promises, timed } from "./promise.js";
55
56
  import { fromQuery, toQuery } from "./query.js";
56
57
  import { QueueError, queue } from "./queue.js";
57
58
  import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
58
- import { error, isError, isOk, isResult, ok, result, unwrap } from "./result.js";
59
+ import { attempt, error, isError, isOk, isResult, ok, unwrap } from "./result.js";
59
60
  import { SizedSet } from "./sized/set.js";
60
- export { frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, average, beacon, between, camelCase, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, memoize, merge, min, noop, ok, parse, partial, pascalCase, promises, push, queue, result, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, tryPromise, unique, unsmush, unwrap, upperCase, words };
61
+ export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, pascalCase, pick, promises, push, queue, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, upperCase, words };
@@ -1,3 +1,4 @@
1
+ import { isNumber } from "../is.js";
1
2
  function aggregate(type, array, key) {
2
3
  const length = Array.isArray(array) ? array.length : 0;
3
4
  if (length === 0) return {
@@ -5,24 +6,27 @@ function aggregate(type, array, key) {
5
6
  value: NaN
6
7
  };
7
8
  const aggregator = aggregators[type];
8
- const isCallback = typeof key === "function";
9
+ const callback = getAggregateCallback(key);
9
10
  let counted = 0;
10
11
  let aggregated = NaN;
11
12
  let notNumber = true;
12
13
  for (let index = 0; index < length; index += 1) {
13
14
  const item = array[index];
14
- const value = isCallback ? key(item, index, array) : item[key] ?? item;
15
- if (typeof value === "number" && !Number.isNaN(value)) {
16
- aggregated = aggregator(aggregated, value, notNumber);
17
- counted += 1;
18
- notNumber = false;
19
- }
15
+ const value = callback == null ? item : callback(item, index, array);
16
+ if (!isNumber(value)) continue;
17
+ aggregated = aggregator(aggregated, value, notNumber);
18
+ counted += 1;
19
+ notNumber = false;
20
20
  }
21
21
  return {
22
22
  count: counted,
23
23
  value: aggregated
24
24
  };
25
25
  }
26
+ function getAggregateCallback(key) {
27
+ if (key == null) return;
28
+ return typeof key === "function" ? key : (item) => item[key];
29
+ }
26
30
  function max(array, key) {
27
31
  return getAggregated("max", array, key);
28
32
  }
@@ -39,4 +43,4 @@ var aggregators = {
39
43
  min: (current, value, notNumber) => notNumber || value < current ? value : current,
40
44
  sum: calculateSum
41
45
  };
42
- export { aggregate, getAggregated, max };
46
+ export { aggregate, getAggregateCallback, getAggregated, max };
@@ -35,7 +35,7 @@ function clamp(value, minimum, maximum, loop) {
35
35
  /**
36
36
  * Get the number value from an unknown value _(based on Lodash)_
37
37
  * @param value Original value
38
- * @returns The value as a number, or `NaN` if the value is unable to be parsed
38
+ * @returns Original value as a number, or `NaN` if the value is unable to be parsed
39
39
  */
40
40
  function getNumber(value) {
41
41
  if (typeof value === "number") return value;
@@ -0,0 +1,14 @@
1
+ function partial(value, providedKeys, omit) {
2
+ if (typeof value !== "object" || value === null) return {};
3
+ const keys = omit ? Object.keys(value) : Array.isArray(providedKeys) ? providedKeys : [];
4
+ const { length } = keys;
5
+ if (length === 0) return omit ? value : {};
6
+ const partials = {};
7
+ for (let index = 0; index < length; index += 1) {
8
+ const key = keys[index];
9
+ if (!(key in value)) continue;
10
+ if (omit ? !providedKeys.includes(key) : true) partials[key] = value[key];
11
+ }
12
+ return partials;
13
+ }
14
+ export { partial };
package/dist/is.js CHANGED
@@ -3,8 +3,8 @@ import { getArray } from "./array/get.js";
3
3
  import { getString } from "./internal/string.js";
4
4
  /**
5
5
  * Is the value empty, or only containing `null` or `undefined` values?
6
- * @param value Object to check
7
- * @returns `true` if the object is considered empty, otherwise `false`
6
+ * @param value Value to check
7
+ * @returns `true` if the value is considered empty, otherwise `false`
8
8
  */
9
9
  function isEmpty(value) {
10
10
  if (value == null) return true;
@@ -15,6 +15,14 @@ function isEmpty(value) {
15
15
  return true;
16
16
  }
17
17
  /**
18
+ * Is the value not `undefined` or `null`?
19
+ * @param value Value to check
20
+ * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
21
+ */
22
+ function isNonNullable(value) {
23
+ return value != null;
24
+ }
25
+ /**
18
26
  * Is the value `undefined` or `null`?
19
27
  * @param value Value to check
20
28
  * @returns `true` if the value is `undefined` or `null`, otherwise `false`
@@ -64,4 +72,4 @@ function isPrimitive(value) {
64
72
  }
65
73
  var EXPRESSION_PRIMITIVE = /^(bigint|boolean|number|string|symbol)$/;
66
74
  var EXPRESSION_WHITESPACE = /^\s*$/;
67
- export { isArrayOrPlainObject, isConstructor, isEmpty, isKey, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
75
+ export { isArrayOrPlainObject, isConstructor, isEmpty, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
package/dist/math.js CHANGED
@@ -1,4 +1,5 @@
1
- import { aggregate, getAggregated, max } from "./internal/math/aggregate.js";
1
+ import { isNumber } from "./internal/is.js";
2
+ import { aggregate, getAggregateCallback, getAggregated, max } from "./internal/math/aggregate.js";
2
3
  function average(array, key) {
3
4
  const aggregated = aggregate("average", array, key);
4
5
  return aggregated.count > 0 ? aggregated.value / aggregated.count : NaN;
@@ -6,9 +7,8 @@ function average(array, key) {
6
7
  function count(array, key, value) {
7
8
  if (!Array.isArray(array)) return NaN;
8
9
  const { length } = array;
9
- if (key == null) return length;
10
- if (typeof key !== "string" && typeof key !== "function") return NaN;
11
- const callback = typeof key === "function" ? key : (item) => item[key];
10
+ const callback = getAggregateCallback(key);
11
+ if (callback == null) return length;
12
12
  let counted = 0;
13
13
  for (let index = 0; index < length; index += 1) {
14
14
  const item = array[index];
@@ -16,6 +16,22 @@ function count(array, key, value) {
16
16
  }
17
17
  return counted;
18
18
  }
19
+ function median(array, key) {
20
+ let length = Array.isArray(array) ? array.length : 0;
21
+ if (!Array.isArray(array) || length === 0) return NaN;
22
+ if (length === 1) return isNumber(array[0]) ? array[0] : NaN;
23
+ let values = array;
24
+ const callback = getAggregateCallback(key);
25
+ if (callback != null) values = array.map((item, index) => callback(item, index, array));
26
+ const numbers = values.filter(isNumber).sort((first, second) => first - second);
27
+ length = numbers.length;
28
+ if (length % 2 === 0) {
29
+ const first = length / 2 - 1;
30
+ const second = length / 2;
31
+ return (numbers[first] + numbers[second]) / 2;
32
+ }
33
+ return numbers[Math.floor(length / 2)];
34
+ }
19
35
  function min(array, key) {
20
36
  return getAggregated("min", array, key);
21
37
  }
@@ -34,4 +50,4 @@ function round(value, decimals) {
34
50
  function sum(array, key) {
35
51
  return getAggregated("sum", array, key);
36
52
  }
37
- export { average, count, max, min, round, sum };
53
+ export { average, count, max, median, min, round, sum };
package/dist/promise.js CHANGED
@@ -1,9 +1,35 @@
1
+ var CancelablePromise = class extends Promise {
2
+ #rejector;
3
+ constructor(executor) {
4
+ let rejector;
5
+ super((resolve, reject) => {
6
+ rejector = reject;
7
+ executor(resolve, reject);
8
+ });
9
+ this.#rejector = rejector;
10
+ }
11
+ /**
12
+ * Cancel the promise, rejecting it with an optional reason
13
+ * @param reason Optional reason for canceling the promise
14
+ */
15
+ cancel(reason) {
16
+ this.#rejector(reason);
17
+ }
18
+ };
1
19
  var PromiseTimeoutError = class extends Error {
2
20
  constructor() {
3
21
  super(MESSAGE_TIMEOUT);
4
22
  this.name = ERROR_NAME;
5
23
  }
6
24
  };
25
+ /**
26
+ * Create a cancelable promise
27
+ * @param executor Executor function for the promise
28
+ * @returns Cancelable promise
29
+ */
30
+ function cancelable(executor) {
31
+ return new CancelablePromise(executor);
32
+ }
7
33
  function delay(options) {
8
34
  const { signal, time } = getPromiseOptions(options);
9
35
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
@@ -156,9 +182,9 @@ async function timed(promise, options) {
156
182
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
157
183
  return time > 0 ? getTimed(promise, time, signal) : promise;
158
184
  }
159
- async function tryPromise(value, options) {
185
+ async function attemptPromise(value, options) {
160
186
  const isFunction = typeof value === "function";
161
- if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_TRY));
187
+ if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
162
188
  const { signal, time } = getPromiseOptions(options);
163
189
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
164
190
  function abort() {
@@ -185,11 +211,11 @@ var ABORT_OPTIONS = { once: true };
185
211
  var DEFAULT_STRATEGY = "complete";
186
212
  var ERROR_NAME = "PromiseTimeoutError";
187
213
  var EVENT_NAME = "abort";
214
+ var MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
188
215
  var MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
189
216
  var MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
190
- var MESSAGE_EXPECTATION_TRY = "TryPromise expected a function or a promise";
191
217
  var MESSAGE_TIMEOUT = "Promise timed out";
192
218
  var strategies = new Set(["complete", "first"]);
193
219
  var TYPE_FULFILLED = "fulfilled";
194
220
  var TYPE_REJECTED = "rejected";
195
- export { PromiseTimeoutError, delay, isFulfilled, isRejected, promises, timed, tryPromise };
221
+ export { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, isFulfilled, isRejected, promises, timed };
package/dist/result.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { isPlainObject } from "./internal/is.js";
2
+ import { attemptPromise } from "./promise.js";
2
3
  function _isResult(value, okValue) {
3
4
  if (!isPlainObject(value)) return false;
4
5
  return value.ok === okValue && (okValue ? "value" : "error") in value;
@@ -44,17 +45,18 @@ function ok(value) {
44
45
  value
45
46
  };
46
47
  }
47
- function result(callback, err) {
48
+ function attempt(callback, err) {
48
49
  try {
49
50
  return ok(callback());
50
51
  } catch (thrown) {
51
52
  return getError(err ?? thrown, err == null ? void 0 : thrown);
52
53
  }
53
54
  }
54
- result.async = asyncResult;
55
- async function asyncResult(callback, err) {
55
+ attempt.async = asyncAttempt;
56
+ attempt.promise = attemptPromise;
57
+ async function asyncAttempt(value, err) {
56
58
  try {
57
- return ok(await callback());
59
+ return ok(await (typeof value === "function" ? value() : value));
58
60
  } catch (thrown) {
59
61
  return getError(err ?? thrown, err == null ? void 0 : thrown);
60
62
  }
@@ -62,4 +64,4 @@ async function asyncResult(callback, err) {
62
64
  function unwrap(value, defaultValue) {
63
65
  return isOk(value) ? value.value : defaultValue;
64
66
  }
65
- export { error, isError, isOk, isResult, ok, result, unwrap };
67
+ export { attempt, error, isError, isOk, isResult, ok, unwrap };