@oscarpalmer/atoms 0.98.0 → 0.100.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 (81) hide show
  1. package/dist/array/flatten.cjs +1 -4
  2. package/dist/array/flatten.js +1 -4
  3. package/dist/array/sort.cjs +37 -37
  4. package/dist/array/sort.js +37 -37
  5. package/dist/array/unique.cjs +1 -4
  6. package/dist/array/unique.js +1 -4
  7. package/dist/color/is.cjs +6 -6
  8. package/dist/color/is.js +6 -6
  9. package/dist/function.cjs +47 -35
  10. package/dist/function.js +48 -35
  11. package/dist/index.cjs +1 -1
  12. package/dist/index.js +2 -2
  13. package/dist/internal/array/shuffle.cjs +1 -1
  14. package/dist/internal/array/shuffle.js +1 -1
  15. package/dist/internal/function.cjs +21 -0
  16. package/dist/internal/function.js +22 -1
  17. package/dist/internal/is.cjs +4 -0
  18. package/dist/internal/is.js +4 -1
  19. package/dist/internal/number.cjs +14 -1
  20. package/dist/internal/number.js +14 -1
  21. package/dist/internal/random.cjs +12 -10
  22. package/dist/internal/random.js +12 -10
  23. package/dist/internal/value/get.cjs +3 -0
  24. package/dist/internal/value/get.js +3 -0
  25. package/dist/internal/value/misc.cjs +8 -6
  26. package/dist/internal/value/misc.js +8 -6
  27. package/dist/internal/value/set.cjs +4 -0
  28. package/dist/internal/value/set.js +4 -0
  29. package/dist/is.cjs +3 -6
  30. package/dist/is.js +2 -4
  31. package/dist/sized.cjs +22 -28
  32. package/dist/sized.js +22 -28
  33. package/package.json +3 -3
  34. package/src/array/flatten.ts +3 -5
  35. package/src/array/index.ts +0 -1
  36. package/src/array/models.ts +32 -9
  37. package/src/array/sort.ts +124 -76
  38. package/src/array/unique.ts +3 -5
  39. package/src/color/index.ts +0 -1
  40. package/src/color/is.ts +7 -7
  41. package/src/emitter.ts +0 -1
  42. package/src/function.ts +69 -45
  43. package/src/internal/array/shuffle.ts +1 -1
  44. package/src/internal/function.ts +36 -0
  45. package/src/internal/is.ts +7 -0
  46. package/src/internal/number.ts +23 -1
  47. package/src/internal/random.ts +16 -14
  48. package/src/internal/value/get.ts +5 -0
  49. package/src/internal/value/misc.ts +11 -12
  50. package/src/internal/value/set.ts +7 -0
  51. package/src/is.ts +1 -7
  52. package/src/sized.ts +24 -32
  53. package/types/array/index.d.cts +31 -17
  54. package/types/array/models.d.cts +22 -12
  55. package/types/array/models.d.ts +22 -9
  56. package/types/array/sort.d.cts +31 -21
  57. package/types/array/sort.d.ts +11 -11
  58. package/types/function.d.cts +10 -13
  59. package/types/function.d.ts +6 -8
  60. package/types/index.d.cts +59 -45
  61. package/types/internal/function.d.cts +4 -0
  62. package/types/internal/function.d.ts +4 -0
  63. package/types/internal/is.d.cts +4 -0
  64. package/types/internal/is.d.ts +4 -0
  65. package/types/internal/number.d.cts +2 -1
  66. package/types/internal/number.d.ts +2 -1
  67. package/types/internal/random.d.cts +2 -2
  68. package/types/internal/random.d.ts +2 -2
  69. package/types/internal/value/get.d.cts +6 -3
  70. package/types/internal/value/misc.d.cts +1 -1
  71. package/types/internal/value/misc.d.ts +1 -1
  72. package/types/internal/value/set.d.cts +6 -3
  73. package/types/is.d.cts +4 -4
  74. package/types/is.d.ts +0 -4
  75. package/types/models.d.cts +6 -3
  76. package/types/number.d.cts +2 -1
  77. package/types/random.d.cts +2 -2
  78. package/types/sized.d.cts +8 -10
  79. package/types/sized.d.ts +8 -10
  80. package/types/value/index.d.cts +6 -3
  81. package/types/value/smush.d.cts +6 -3
@@ -3,10 +3,7 @@
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  function flatten(array) {
6
- if (!Array.isArray(array)) {
7
- return [];
8
- }
9
- return array.flat(Number.POSITIVE_INFINITY);
6
+ return Array.isArray(array) ? array.flat(Number.POSITIVE_INFINITY) : [];
10
7
  }
11
8
 
12
9
  exports.flatten = flatten;
@@ -1,8 +1,5 @@
1
1
  function flatten(array) {
2
- if (!Array.isArray(array)) {
3
- return [];
4
- }
5
- return array.flat(Number.POSITIVE_INFINITY);
2
+ return Array.isArray(array) ? array.flat(Number.POSITIVE_INFINITY) : [];
6
3
  }
7
4
 
8
5
  export { flatten };
@@ -2,9 +2,26 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const internal_is = require('../internal/is.cjs');
6
5
  const internal_value_compare = require('../internal/value/compare.cjs');
6
+ const internal_is = require('../internal/is.cjs');
7
7
 
8
+ function getSorter(value, modifier) {
9
+ const isObject = internal_is.isPlainObject(value);
10
+ const sorter = {
11
+ identifier: "",
12
+ modifier
13
+ };
14
+ sorter.compare = isObject && typeof value.compare === "function" ? value.compare : void 0;
15
+ sorter.key = isObject && typeof value.key === "string" ? value.key : typeof value === "string" ? value : void 0;
16
+ sorter.callback = sorter.key == null ? isObject && typeof value.value === "function" ? value.value : typeof value === "function" ? value : void 0 : void 0;
17
+ if (isObject && typeof value.direction === "string") {
18
+ sorter.modifier = value.direction === "ascending" ? 1 : value.direction === "descending" ? -1 : modifier;
19
+ }
20
+ if (sorter.key != null || sorter.callback != null) {
21
+ sorter.identifier = `${sorter.key ?? sorter.callback}`;
22
+ return sorter;
23
+ }
24
+ }
8
25
  function sort(array, first, second) {
9
26
  if (!Array.isArray(array)) {
10
27
  return [];
@@ -13,55 +30,38 @@ function sort(array, first, second) {
13
30
  return array;
14
31
  }
15
32
  const direction = first === true || second === true ? "desc" : "asc";
16
- const unknownSorters = Array.isArray(first) ? first : [first];
17
- let { length } = unknownSorters;
18
- const sorters = [];
19
- for (let index = 0; index < length; index += 1) {
20
- const unknownSorter = unknownSorters[index];
21
- const sorter = {
22
- direction,
23
- callback: void 0
24
- };
25
- if (internal_is.isKey(unknownSorter)) {
26
- sorter.callback = (value) => value[unknownSorter];
27
- } else if (typeof unknownSorter === "function") {
28
- sorter.callback = unknownSorter;
29
- } else if (typeof unknownSorter?.value === "function" || internal_is.isKey(unknownSorter?.value)) {
30
- sorter.direction = unknownSorter?.direction ?? direction;
31
- sorter.callback = typeof unknownSorter.value === "function" ? unknownSorter.value : (value) => value[unknownSorter.value];
32
- }
33
- if (typeof sorter.callback === "function") {
34
- const existing = sorters.findIndex(
35
- (existing2) => existing2.callback.toString() === sorter.callback.toString()
36
- );
37
- if (existing > -1) {
38
- sorters.splice(existing, 1);
39
- }
40
- sorters.push(sorter);
41
- }
42
- }
43
- length = sorters.length;
33
+ const modifier = direction === "asc" ? 1 : -1;
34
+ const sorters = (Array.isArray(first) ? first : [first]).map((item) => getSorter(item, modifier)).filter((sorter) => sorter != null).filter(
35
+ (current, index, sorters2) => sorters2.findIndex((next) => next.identifier === current.identifier) === index
36
+ );
37
+ const { length } = sorters;
44
38
  if (length === 0) {
45
39
  return array.sort(
46
- (first2, second2) => internal_value_compare.compare(first2, second2) * (direction === "asc" ? 1 : -1)
40
+ (first2, second2) => internal_value_compare.compare(first2, second2) * modifier
47
41
  );
48
42
  }
49
43
  if (length === 1) {
50
- return array.sort(
51
- (first2, second2) => internal_value_compare.compare(sorters[0].callback(first2), sorters[0].callback(second2)) * (sorters[0].direction === "asc" ? 1 : -1)
52
- );
44
+ const sorter = sorters[0];
45
+ const { callback, key, modifier: modifier2 } = sorter;
46
+ return array.sort((first2, second2) => {
47
+ const firstValue = key == null ? callback?.(first2) : first2[key];
48
+ const secondValue = key == null ? callback?.(second2) : second2[key];
49
+ return (sorter.compare?.(first2, firstValue, second2, secondValue) ?? internal_value_compare.compare(firstValue, secondValue)) * modifier2;
50
+ });
53
51
  }
54
- const sorted = array.sort((first2, second2) => {
52
+ return array.sort((first2, second2) => {
55
53
  for (let index = 0; index < length; index += 1) {
56
- const { callback, direction: direction2 } = sorters[index];
57
- const compared = internal_value_compare.compare(callback(first2), callback(second2)) * (direction2 === "asc" ? 1 : -1);
54
+ const sorter = sorters[index];
55
+ const { callback, key, modifier: modifier2 } = sorter;
56
+ const firstValue = key == null ? callback?.(first2) : first2[key];
57
+ const secondValue = key == null ? callback?.(second2) : second2[key];
58
+ const compared = (sorter.compare?.(first2, firstValue, second2, secondValue) ?? internal_value_compare.compare(firstValue, secondValue)) * modifier2;
58
59
  if (compared !== 0) {
59
60
  return compared;
60
61
  }
61
62
  }
62
63
  return 0;
63
64
  });
64
- return sorted;
65
65
  }
66
66
 
67
67
  exports.sort = sort;
@@ -1,6 +1,23 @@
1
- import { isKey } from '../internal/is.js';
2
1
  import { compare } from '../internal/value/compare.js';
2
+ import { isPlainObject } from '../internal/is.js';
3
3
 
4
+ function getSorter(value, modifier) {
5
+ const isObject = isPlainObject(value);
6
+ const sorter = {
7
+ identifier: "",
8
+ modifier
9
+ };
10
+ sorter.compare = isObject && typeof value.compare === "function" ? value.compare : void 0;
11
+ sorter.key = isObject && typeof value.key === "string" ? value.key : typeof value === "string" ? value : void 0;
12
+ sorter.callback = sorter.key == null ? isObject && typeof value.value === "function" ? value.value : typeof value === "function" ? value : void 0 : void 0;
13
+ if (isObject && typeof value.direction === "string") {
14
+ sorter.modifier = value.direction === "ascending" ? 1 : value.direction === "descending" ? -1 : modifier;
15
+ }
16
+ if (sorter.key != null || sorter.callback != null) {
17
+ sorter.identifier = `${sorter.key ?? sorter.callback}`;
18
+ return sorter;
19
+ }
20
+ }
4
21
  function sort(array, first, second) {
5
22
  if (!Array.isArray(array)) {
6
23
  return [];
@@ -9,55 +26,38 @@ function sort(array, first, second) {
9
26
  return array;
10
27
  }
11
28
  const direction = first === true || second === true ? "desc" : "asc";
12
- const unknownSorters = Array.isArray(first) ? first : [first];
13
- let { length } = unknownSorters;
14
- const sorters = [];
15
- for (let index = 0; index < length; index += 1) {
16
- const unknownSorter = unknownSorters[index];
17
- const sorter = {
18
- direction,
19
- callback: void 0
20
- };
21
- if (isKey(unknownSorter)) {
22
- sorter.callback = (value) => value[unknownSorter];
23
- } else if (typeof unknownSorter === "function") {
24
- sorter.callback = unknownSorter;
25
- } else if (typeof unknownSorter?.value === "function" || isKey(unknownSorter?.value)) {
26
- sorter.direction = unknownSorter?.direction ?? direction;
27
- sorter.callback = typeof unknownSorter.value === "function" ? unknownSorter.value : (value) => value[unknownSorter.value];
28
- }
29
- if (typeof sorter.callback === "function") {
30
- const existing = sorters.findIndex(
31
- (existing2) => existing2.callback.toString() === sorter.callback.toString()
32
- );
33
- if (existing > -1) {
34
- sorters.splice(existing, 1);
35
- }
36
- sorters.push(sorter);
37
- }
38
- }
39
- length = sorters.length;
29
+ const modifier = direction === "asc" ? 1 : -1;
30
+ const sorters = (Array.isArray(first) ? first : [first]).map((item) => getSorter(item, modifier)).filter((sorter) => sorter != null).filter(
31
+ (current, index, sorters2) => sorters2.findIndex((next) => next.identifier === current.identifier) === index
32
+ );
33
+ const { length } = sorters;
40
34
  if (length === 0) {
41
35
  return array.sort(
42
- (first2, second2) => compare(first2, second2) * (direction === "asc" ? 1 : -1)
36
+ (first2, second2) => compare(first2, second2) * modifier
43
37
  );
44
38
  }
45
39
  if (length === 1) {
46
- return array.sort(
47
- (first2, second2) => compare(sorters[0].callback(first2), sorters[0].callback(second2)) * (sorters[0].direction === "asc" ? 1 : -1)
48
- );
40
+ const sorter = sorters[0];
41
+ const { callback, key, modifier: modifier2 } = sorter;
42
+ return array.sort((first2, second2) => {
43
+ const firstValue = key == null ? callback?.(first2) : first2[key];
44
+ const secondValue = key == null ? callback?.(second2) : second2[key];
45
+ return (sorter.compare?.(first2, firstValue, second2, secondValue) ?? compare(firstValue, secondValue)) * modifier2;
46
+ });
49
47
  }
50
- const sorted = array.sort((first2, second2) => {
48
+ return array.sort((first2, second2) => {
51
49
  for (let index = 0; index < length; index += 1) {
52
- const { callback, direction: direction2 } = sorters[index];
53
- const compared = compare(callback(first2), callback(second2)) * (direction2 === "asc" ? 1 : -1);
50
+ const sorter = sorters[index];
51
+ const { callback, key, modifier: modifier2 } = sorter;
52
+ const firstValue = key == null ? callback?.(first2) : first2[key];
53
+ const secondValue = key == null ? callback?.(second2) : second2[key];
54
+ const compared = (sorter.compare?.(first2, firstValue, second2, secondValue) ?? compare(firstValue, secondValue)) * modifier2;
54
55
  if (compared !== 0) {
55
56
  return compared;
56
57
  }
57
58
  }
58
59
  return 0;
59
60
  });
60
- return sorted;
61
61
  }
62
62
 
63
63
  export { sort };
@@ -8,10 +8,7 @@ function unique(array, key) {
8
8
  if (!Array.isArray(array)) {
9
9
  return [];
10
10
  }
11
- if (array.length < 2) {
12
- return array;
13
- }
14
- return internal_array_find.findValues("unique", array, [key, void 0]);
11
+ return array.length > 1 ? internal_array_find.findValues("unique", array, [key, void 0]) : array;
15
12
  }
16
13
 
17
14
  exports.unique = unique;
@@ -4,10 +4,7 @@ function unique(array, key) {
4
4
  if (!Array.isArray(array)) {
5
5
  return [];
6
6
  }
7
- if (array.length < 2) {
8
- return array;
9
- }
10
- return findValues("unique", array, [key, void 0]);
7
+ return array.length > 1 ? findValues("unique", array, [key, void 0]) : array;
11
8
  }
12
9
 
13
10
  export { unique };
package/dist/color/is.cjs CHANGED
@@ -5,6 +5,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const internal_number = require('../internal/number.cjs');
6
6
  const color_constants = require('./constants.cjs');
7
7
 
8
+ function isBytey(value) {
9
+ return typeof value === "number" && internal_number.between(value, 0, 255);
10
+ }
8
11
  function isColor(value) {
9
12
  return typeof value === "object" && value !== null && "$color" in value && value.$color === true;
10
13
  }
@@ -14,9 +17,6 @@ function isDegree(value) {
14
17
  function isHexColor(value) {
15
18
  return typeof value === "string" && color_constants.anyPattern.test(value);
16
19
  }
17
- function isHexy(value) {
18
- return typeof value === "number" && internal_number.between(value, 0, 255);
19
- }
20
20
  function isHSLColor(value) {
21
21
  return isObject(value, color_constants.hslKeys);
22
22
  }
@@ -44,12 +44,12 @@ function isPercentage(value) {
44
44
  return typeof value === "number" && internal_number.between(value, 0, 100);
45
45
  }
46
46
  const validators = {
47
- blue: isHexy,
48
- green: isHexy,
47
+ blue: isBytey,
48
+ green: isBytey,
49
49
  hue: isDegree,
50
50
  lightness: isPercentage,
51
51
  saturation: isPercentage,
52
- red: isHexy
52
+ red: isBytey
53
53
  };
54
54
 
55
55
  exports.isColor = isColor;
package/dist/color/is.js CHANGED
@@ -1,6 +1,9 @@
1
1
  import { between } from '../internal/number.js';
2
2
  import { anyPattern, hslKeys, rgbKeys } from './constants.js';
3
3
 
4
+ function isBytey(value) {
5
+ return typeof value === "number" && between(value, 0, 255);
6
+ }
4
7
  function isColor(value) {
5
8
  return typeof value === "object" && value !== null && "$color" in value && value.$color === true;
6
9
  }
@@ -10,9 +13,6 @@ function isDegree(value) {
10
13
  function isHexColor(value) {
11
14
  return typeof value === "string" && anyPattern.test(value);
12
15
  }
13
- function isHexy(value) {
14
- return typeof value === "number" && between(value, 0, 255);
15
- }
16
16
  function isHSLColor(value) {
17
17
  return isObject(value, hslKeys);
18
18
  }
@@ -40,12 +40,12 @@ function isPercentage(value) {
40
40
  return typeof value === "number" && between(value, 0, 100);
41
41
  }
42
42
  const validators = {
43
- blue: isHexy,
44
- green: isHexy,
43
+ blue: isBytey,
44
+ green: isBytey,
45
45
  hue: isDegree,
46
46
  lightness: isPercentage,
47
47
  saturation: isPercentage,
48
- red: isHexy
48
+ red: isBytey
49
49
  };
50
50
 
51
51
  export { isColor, isHSLColor, isHexColor, isRGBColor };
package/dist/function.cjs CHANGED
@@ -2,16 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const internal_number = require('./internal/number.cjs');
5
+ const internal_function = require('./internal/function.cjs');
6
6
  const internal_string = require('./internal/string.cjs');
7
7
  const sized = require('./sized.cjs');
8
- const internal_function = require('./internal/function.cjs');
9
8
 
10
- class Memoised {
11
- constructor(callback, cacheSize) {
12
- const cache = new sized.SizedMap(
13
- cacheSize ?? 2 ** 16
14
- );
9
+ class Memoized {
10
+ constructor(callback, size) {
11
+ const cache = new sized.SizedMap(size ?? 2 ** 16);
15
12
  const getter = (...parameters) => {
16
13
  const key = parameters.length === 1 ? parameters[0] : internal_string.join(parameters.map(internal_string.getString));
17
14
  if (cache?.has(key) ?? false) {
@@ -66,49 +63,64 @@ class Memoised {
66
63
  }
67
64
  }
68
65
  function debounce(callback, time) {
69
- const interval = internal_number.clamp(time ?? 0, 0, 1e3);
70
- let timer;
71
- const debounced = (...parameters) => {
72
- if (timer != null) {
73
- clearTimeout(timer);
74
- }
75
- timer = +setTimeout(() => {
66
+ const interval = typeof time === "number" && time >= internal_function.milliseconds ? time : internal_function.milliseconds;
67
+ function step(now, parameters) {
68
+ if (interval === internal_function.milliseconds || now - start >= interval) {
76
69
  callback(...parameters);
77
- }, interval);
70
+ } else {
71
+ frame = requestAnimationFrame((next) => {
72
+ step(next, parameters);
73
+ });
74
+ }
75
+ }
76
+ let frame;
77
+ let start;
78
+ const debounced = (...parameters) => {
79
+ debounced.cancel();
80
+ frame = requestAnimationFrame((now) => {
81
+ start = now - internal_function.milliseconds;
82
+ step(now, parameters);
83
+ });
78
84
  };
79
85
  debounced.cancel = () => {
80
- if (timer != null) {
81
- clearTimeout(timer);
86
+ if (frame != null) {
87
+ cancelAnimationFrame(frame);
88
+ frame = void 0;
82
89
  }
83
90
  };
84
91
  return debounced;
85
92
  }
86
93
  function memoize(callback, cacheSize) {
87
- return new Memoised(callback, cacheSize);
94
+ return new Memoized(
95
+ callback,
96
+ typeof cacheSize === "number" ? cacheSize : void 0
97
+ );
88
98
  }
89
99
  function throttle(callback, time) {
90
- const interval = internal_number.clamp(time ?? 0, 0, 1e3);
91
- let timestamp = performance.now();
92
- let timer;
93
- const throttler = (...parameters) => {
94
- if (timer != null) {
95
- clearTimeout(timer);
96
- }
97
- const now = performance.now();
98
- const difference = now - timestamp;
99
- if (difference >= interval) {
100
- timestamp = now;
100
+ const interval = typeof time === "number" && time >= internal_function.milliseconds ? time : internal_function.milliseconds;
101
+ function step(now, parameters) {
102
+ if (interval === internal_function.milliseconds || now - last >= interval) {
103
+ last = now;
101
104
  callback(...parameters);
102
105
  } else {
103
- timer = +setTimeout(() => {
104
- timestamp = performance.now();
105
- callback(...parameters);
106
- }, difference + interval);
106
+ frame = requestAnimationFrame((next) => {
107
+ step(next, parameters);
108
+ });
107
109
  }
110
+ }
111
+ let last;
112
+ let frame;
113
+ const throttler = (...parameters) => {
114
+ throttler.cancel();
115
+ frame = requestAnimationFrame((now) => {
116
+ last ??= now - internal_function.milliseconds;
117
+ step(now, parameters);
118
+ });
108
119
  };
109
120
  throttler.cancel = () => {
110
- if (timer != null) {
111
- clearTimeout(timer);
121
+ if (frame != null) {
122
+ cancelAnimationFrame(frame);
123
+ frame = void 0;
112
124
  }
113
125
  };
114
126
  return throttler;
package/dist/function.js CHANGED
@@ -1,13 +1,11 @@
1
- import { clamp } from './internal/number.js';
1
+ import { milliseconds } from './internal/function.js';
2
+ export { noop } from './internal/function.js';
2
3
  import { join, getString } from './internal/string.js';
3
4
  import { SizedMap } from './sized.js';
4
- export { noop } from './internal/function.js';
5
5
 
6
- class Memoised {
7
- constructor(callback, cacheSize) {
8
- const cache = new SizedMap(
9
- cacheSize ?? 2 ** 16
10
- );
6
+ class Memoized {
7
+ constructor(callback, size) {
8
+ const cache = new SizedMap(size ?? 2 ** 16);
11
9
  const getter = (...parameters) => {
12
10
  const key = parameters.length === 1 ? parameters[0] : join(parameters.map(getString));
13
11
  if (cache?.has(key) ?? false) {
@@ -62,49 +60,64 @@ class Memoised {
62
60
  }
63
61
  }
64
62
  function debounce(callback, time) {
65
- const interval = clamp(time ?? 0, 0, 1e3);
66
- let timer;
67
- const debounced = (...parameters) => {
68
- if (timer != null) {
69
- clearTimeout(timer);
70
- }
71
- timer = +setTimeout(() => {
63
+ const interval = typeof time === "number" && time >= milliseconds ? time : milliseconds;
64
+ function step(now, parameters) {
65
+ if (interval === milliseconds || now - start >= interval) {
72
66
  callback(...parameters);
73
- }, interval);
67
+ } else {
68
+ frame = requestAnimationFrame((next) => {
69
+ step(next, parameters);
70
+ });
71
+ }
72
+ }
73
+ let frame;
74
+ let start;
75
+ const debounced = (...parameters) => {
76
+ debounced.cancel();
77
+ frame = requestAnimationFrame((now) => {
78
+ start = now - milliseconds;
79
+ step(now, parameters);
80
+ });
74
81
  };
75
82
  debounced.cancel = () => {
76
- if (timer != null) {
77
- clearTimeout(timer);
83
+ if (frame != null) {
84
+ cancelAnimationFrame(frame);
85
+ frame = void 0;
78
86
  }
79
87
  };
80
88
  return debounced;
81
89
  }
82
90
  function memoize(callback, cacheSize) {
83
- return new Memoised(callback, cacheSize);
91
+ return new Memoized(
92
+ callback,
93
+ typeof cacheSize === "number" ? cacheSize : void 0
94
+ );
84
95
  }
85
96
  function throttle(callback, time) {
86
- const interval = clamp(time ?? 0, 0, 1e3);
87
- let timestamp = performance.now();
88
- let timer;
89
- const throttler = (...parameters) => {
90
- if (timer != null) {
91
- clearTimeout(timer);
92
- }
93
- const now = performance.now();
94
- const difference = now - timestamp;
95
- if (difference >= interval) {
96
- timestamp = now;
97
+ const interval = typeof time === "number" && time >= milliseconds ? time : milliseconds;
98
+ function step(now, parameters) {
99
+ if (interval === milliseconds || now - last >= interval) {
100
+ last = now;
97
101
  callback(...parameters);
98
102
  } else {
99
- timer = +setTimeout(() => {
100
- timestamp = performance.now();
101
- callback(...parameters);
102
- }, difference + interval);
103
+ frame = requestAnimationFrame((next) => {
104
+ step(next, parameters);
105
+ });
103
106
  }
107
+ }
108
+ let last;
109
+ let frame;
110
+ const throttler = (...parameters) => {
111
+ throttler.cancel();
112
+ frame = requestAnimationFrame((now) => {
113
+ last ??= now - milliseconds;
114
+ step(now, parameters);
115
+ });
104
116
  };
105
117
  throttler.cancel = () => {
106
- if (timer != null) {
107
- clearTimeout(timer);
118
+ if (frame != null) {
119
+ cancelAnimationFrame(frame);
120
+ frame = void 0;
108
121
  }
109
122
  };
110
123
  return throttler;
package/dist/index.cjs CHANGED
@@ -85,7 +85,6 @@ exports.isEmpty = is.isEmpty;
85
85
  exports.isNullable = is.isNullable;
86
86
  exports.isNullableOrEmpty = is.isNullableOrEmpty;
87
87
  exports.isNullableOrWhitespace = is.isNullableOrWhitespace;
88
- exports.isNumber = is.isNumber;
89
88
  exports.isNumerical = is.isNumerical;
90
89
  exports.isObject = is.isObject;
91
90
  exports.isPrimitive = is.isPrimitive;
@@ -137,6 +136,7 @@ exports.isRGBColor = color_is.isRGBColor;
137
136
  exports.noop = internal_function.noop;
138
137
  exports.isArrayOrPlainObject = internal_is.isArrayOrPlainObject;
139
138
  exports.isKey = internal_is.isKey;
139
+ exports.isNumber = internal_is.isNumber;
140
140
  exports.isPlainObject = internal_is.isPlainObject;
141
141
  exports.isTypedArray = internal_is.isTypedArray;
142
142
  exports.max = internal_math_aggregate.max;
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ export { Color, getColor, getForegroundColor, getHSLColor, getHexColor, getRGBCo
19
19
  export { emitter } from './emitter.js';
20
20
  export { debounce, memoize, throttle } from './function.js';
21
21
  export { translate } from './i18n.js';
22
- export { isEmpty, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPrimitive } from './is.js';
22
+ export { isEmpty, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from './is.js';
23
23
  export { logger } from './logger.js';
24
24
  export { average, min, round, sum } from './math.js';
25
25
  export { between, clamp, getNumber } from './internal/number.js';
@@ -43,6 +43,6 @@ export { smush } from './value/smush.js';
43
43
  export { unsmush } from './value/unsmush.js';
44
44
  export { isColor, isHSLColor, isHexColor, isRGBColor } from './color/is.js';
45
45
  export { noop } from './internal/function.js';
46
- export { isArrayOrPlainObject, isKey, isPlainObject, isTypedArray } from './internal/is.js';
46
+ export { isArrayOrPlainObject, isKey, isNumber, isPlainObject, isTypedArray } from './internal/is.js';
47
47
  export { max } from './internal/math/aggregate.js';
48
48
  export { getRandomFloat, getRandomInteger } from './internal/random.js';
@@ -11,7 +11,7 @@ function shuffle(array) {
11
11
  if (array.length < 2) {
12
12
  return array;
13
13
  }
14
- const shuffled = array.slice();
14
+ const shuffled = [...array];
15
15
  let index = Number(shuffled.length);
16
16
  while (--index >= 0) {
17
17
  const random = internal_random.getRandomInteger(0, index);
@@ -7,7 +7,7 @@ function shuffle(array) {
7
7
  if (array.length < 2) {
8
8
  return array;
9
9
  }
10
- const shuffled = array.slice();
10
+ const shuffled = [...array];
11
11
  let index = Number(shuffled.length);
12
12
  while (--index >= 0) {
13
13
  const random = getRandomInteger(0, index);
@@ -4,5 +4,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  function noop() {
6
6
  }
7
+ function step(now) {
8
+ if (values.length === 7) {
9
+ exports.milliseconds = Math.floor(
10
+ values.slice(2).reduce((first, second) => first + second) / 5 * 2
11
+ ) / 2;
12
+ last = void 0;
13
+ values.length = 0;
14
+ } else {
15
+ last ??= now;
16
+ const difference = now - last;
17
+ if (difference > 0) {
18
+ values.push(difference);
19
+ }
20
+ last = now;
21
+ requestAnimationFrame(step);
22
+ }
23
+ }
24
+ const values = [];
25
+ let last;
26
+ exports.milliseconds = Math.floor(1e3 / 60 * 2) / 2;
27
+ requestAnimationFrame(step);
7
28
 
8
29
  exports.noop = noop;
@@ -1,4 +1,25 @@
1
1
  function noop() {
2
2
  }
3
+ function step(now) {
4
+ if (values.length === 7) {
5
+ milliseconds = Math.floor(
6
+ values.slice(2).reduce((first, second) => first + second) / 5 * 2
7
+ ) / 2;
8
+ last = void 0;
9
+ values.length = 0;
10
+ } else {
11
+ last ??= now;
12
+ const difference = now - last;
13
+ if (difference > 0) {
14
+ values.push(difference);
15
+ }
16
+ last = now;
17
+ requestAnimationFrame(step);
18
+ }
19
+ }
20
+ const values = [];
21
+ let last;
22
+ let milliseconds = Math.floor(1e3 / 60 * 2) / 2;
23
+ requestAnimationFrame(step);
3
24
 
4
- export { noop };
25
+ export { milliseconds, noop };