@valkyriestudios/utils 12.36.0 → 12.38.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 (196) hide show
  1. package/array/groupBy.d.ts +3 -3
  2. package/array/join.d.ts +2 -2
  3. package/array/split.d.ts +3 -3
  4. package/caching/LRU.d.ts +16 -10
  5. package/{array → cjs/array}/groupBy.js +16 -4
  6. package/{array → cjs/array}/join.js +15 -3
  7. package/{array → cjs/array}/mapFnAsMap.js +1 -1
  8. package/{array → cjs/array}/split.js +16 -7
  9. package/cjs/caching/LRU.js +123 -0
  10. package/{caching → cjs/caching}/memoize.js +11 -5
  11. package/{date → cjs/date}/format.js +7 -7
  12. package/{date → cjs/date}/isFormat.js +5 -2
  13. package/cjs/formdata/toObject.js +92 -0
  14. package/{object → cjs/object}/pick.js +2 -2
  15. package/{string → cjs/string}/humanizeNumber.js +2 -2
  16. package/cjs/string/isNotEmpty.js +23 -0
  17. package/{string → cjs/string}/shorten.js +1 -1
  18. package/esm/array/dedupe.js +52 -0
  19. package/esm/array/groupBy.js +43 -0
  20. package/esm/array/index.js +14 -0
  21. package/esm/array/is.js +4 -0
  22. package/esm/array/isNotEmpty.js +4 -0
  23. package/esm/array/join.js +58 -0
  24. package/esm/array/mapFn.js +22 -0
  25. package/esm/array/mapFnAsMap.js +21 -0
  26. package/esm/array/mapKey.js +22 -0
  27. package/esm/array/mapKeyAsMap.js +22 -0
  28. package/esm/array/mapPrimitive.js +33 -0
  29. package/esm/array/shuffle.js +10 -0
  30. package/esm/array/sort.js +137 -0
  31. package/esm/array/split.js +43 -0
  32. package/esm/boolean/index.js +1 -0
  33. package/esm/boolean/is.js +4 -0
  34. package/esm/caching/LRU.js +116 -0
  35. package/esm/caching/index.js +3 -0
  36. package/esm/caching/memoize.js +37 -0
  37. package/esm/date/addUTC.js +41 -0
  38. package/esm/date/convertToDate.js +12 -0
  39. package/esm/date/diff.js +34 -0
  40. package/esm/date/endOfUTC.js +50 -0
  41. package/esm/date/format.js +241 -0
  42. package/esm/date/index.js +15 -0
  43. package/esm/date/is.js +4 -0
  44. package/esm/date/isFormat.js +118 -0
  45. package/esm/date/isLeap.js +9 -0
  46. package/esm/date/nowUnix.js +4 -0
  47. package/esm/date/nowUnixMs.js +4 -0
  48. package/esm/date/setTimeUTC.js +10 -0
  49. package/esm/date/startOfUTC.js +50 -0
  50. package/esm/date/toUTC.js +7 -0
  51. package/esm/date/toUnix.js +7 -0
  52. package/esm/deep/freeze.js +21 -0
  53. package/esm/deep/get.js +84 -0
  54. package/esm/deep/index.js +8 -0
  55. package/esm/deep/seal.js +21 -0
  56. package/esm/deep/set.js +50 -0
  57. package/esm/equal.js +67 -0
  58. package/esm/formdata/index.js +2 -0
  59. package/esm/formdata/is.js +4 -0
  60. package/esm/formdata/toObject.js +89 -0
  61. package/esm/function/debounce.js +39 -0
  62. package/esm/function/index.js +9 -0
  63. package/esm/function/is.js +4 -0
  64. package/esm/function/isAsync.js +4 -0
  65. package/esm/function/noop.js +2 -0
  66. package/esm/function/noopresolve.js +4 -0
  67. package/esm/function/noopreturn.js +4 -0
  68. package/esm/function/sleep.js +6 -0
  69. package/esm/hash/fnv1A.js +51 -0
  70. package/esm/hash/guid.js +31 -0
  71. package/esm/hash/index.js +2 -0
  72. package/esm/is.js +63 -0
  73. package/esm/modules/PubSub.js +187 -0
  74. package/esm/modules/Scheduler.js +343 -0
  75. package/esm/modules/index.js +2 -0
  76. package/esm/number/index.js +37 -0
  77. package/esm/number/is.js +4 -0
  78. package/esm/number/isAbove.js +4 -0
  79. package/esm/number/isAboveOrEqual.js +4 -0
  80. package/esm/number/isBelow.js +4 -0
  81. package/esm/number/isBelowOrEqual.js +4 -0
  82. package/esm/number/isBetween.js +7 -0
  83. package/esm/number/isInteger.js +4 -0
  84. package/esm/number/isIntegerAbove.js +4 -0
  85. package/esm/number/isIntegerAboveOrEqual.js +4 -0
  86. package/esm/number/isIntegerBelow.js +4 -0
  87. package/esm/number/isIntegerBelowOrEqual.js +4 -0
  88. package/esm/number/isIntegerBetween.js +7 -0
  89. package/esm/number/isNumericalNaN.js +4 -0
  90. package/esm/number/randomBetween.js +7 -0
  91. package/esm/number/randomIntBetween.js +7 -0
  92. package/esm/number/round.js +10 -0
  93. package/esm/number/toPercentage.js +9 -0
  94. package/esm/object/define.js +7 -0
  95. package/esm/object/index.js +7 -0
  96. package/esm/object/is.js +4 -0
  97. package/esm/object/isNotEmpty.js +9 -0
  98. package/esm/object/merge.js +31 -0
  99. package/esm/object/omit.js +30 -0
  100. package/esm/object/pick.js +33 -0
  101. package/esm/regexp/index.js +3 -0
  102. package/esm/regexp/is.js +4 -0
  103. package/esm/regexp/sanitize.js +7 -0
  104. package/esm/string/humanizeBytes.js +15 -0
  105. package/esm/string/humanizeNumber.js +40 -0
  106. package/esm/string/index.js +7 -0
  107. package/esm/string/is.js +4 -0
  108. package/esm/string/isBetween.js +12 -0
  109. package/esm/string/isNotEmpty.js +20 -0
  110. package/esm/string/shorten.js +18 -0
  111. package/formdata/toObject.d.ts +4 -0
  112. package/index.d.ts +20 -15
  113. package/package.json +193 -851
  114. package/caching/LRU.js +0 -72
  115. package/formdata/toObject.js +0 -70
  116. package/string/isNotEmpty.js +0 -9
  117. /package/{array → cjs/array}/dedupe.js +0 -0
  118. /package/{array → cjs/array}/index.js +0 -0
  119. /package/{array → cjs/array}/is.js +0 -0
  120. /package/{array → cjs/array}/isNotEmpty.js +0 -0
  121. /package/{array → cjs/array}/mapFn.js +0 -0
  122. /package/{array → cjs/array}/mapKey.js +0 -0
  123. /package/{array → cjs/array}/mapKeyAsMap.js +0 -0
  124. /package/{array → cjs/array}/mapPrimitive.js +0 -0
  125. /package/{array → cjs/array}/shuffle.js +0 -0
  126. /package/{array → cjs/array}/sort.js +0 -0
  127. /package/{boolean → cjs/boolean}/index.js +0 -0
  128. /package/{boolean → cjs/boolean}/is.js +0 -0
  129. /package/{caching → cjs/caching}/index.js +0 -0
  130. /package/{date → cjs/date}/addUTC.js +0 -0
  131. /package/{date → cjs/date}/convertToDate.js +0 -0
  132. /package/{date → cjs/date}/diff.js +0 -0
  133. /package/{date → cjs/date}/endOfUTC.js +0 -0
  134. /package/{date → cjs/date}/index.js +0 -0
  135. /package/{date → cjs/date}/is.js +0 -0
  136. /package/{date → cjs/date}/isLeap.js +0 -0
  137. /package/{date → cjs/date}/nowUnix.js +0 -0
  138. /package/{date → cjs/date}/nowUnixMs.js +0 -0
  139. /package/{date → cjs/date}/setTimeUTC.js +0 -0
  140. /package/{date → cjs/date}/startOfUTC.js +0 -0
  141. /package/{date → cjs/date}/toUTC.js +0 -0
  142. /package/{date → cjs/date}/toUnix.js +0 -0
  143. /package/{deep → cjs/deep}/freeze.js +0 -0
  144. /package/{deep → cjs/deep}/get.js +0 -0
  145. /package/{deep → cjs/deep}/index.js +0 -0
  146. /package/{deep → cjs/deep}/seal.js +0 -0
  147. /package/{deep → cjs/deep}/set.js +0 -0
  148. /package/{equal.js → cjs/equal.js} +0 -0
  149. /package/{formdata → cjs/formdata}/index.js +0 -0
  150. /package/{formdata → cjs/formdata}/is.js +0 -0
  151. /package/{function → cjs/function}/debounce.js +0 -0
  152. /package/{function → cjs/function}/index.js +0 -0
  153. /package/{function → cjs/function}/is.js +0 -0
  154. /package/{function → cjs/function}/isAsync.js +0 -0
  155. /package/{function → cjs/function}/noop.js +0 -0
  156. /package/{function → cjs/function}/noopresolve.js +0 -0
  157. /package/{function → cjs/function}/noopreturn.js +0 -0
  158. /package/{function → cjs/function}/sleep.js +0 -0
  159. /package/{hash → cjs/hash}/fnv1A.js +0 -0
  160. /package/{hash → cjs/hash}/guid.js +0 -0
  161. /package/{hash → cjs/hash}/index.js +0 -0
  162. /package/{is.js → cjs/is.js} +0 -0
  163. /package/{modules → cjs/modules}/PubSub.js +0 -0
  164. /package/{modules → cjs/modules}/Scheduler.js +0 -0
  165. /package/{modules → cjs/modules}/index.js +0 -0
  166. /package/{number → cjs/number}/index.js +0 -0
  167. /package/{number → cjs/number}/is.js +0 -0
  168. /package/{number → cjs/number}/isAbove.js +0 -0
  169. /package/{number → cjs/number}/isAboveOrEqual.js +0 -0
  170. /package/{number → cjs/number}/isBelow.js +0 -0
  171. /package/{number → cjs/number}/isBelowOrEqual.js +0 -0
  172. /package/{number → cjs/number}/isBetween.js +0 -0
  173. /package/{number → cjs/number}/isInteger.js +0 -0
  174. /package/{number → cjs/number}/isIntegerAbove.js +0 -0
  175. /package/{number → cjs/number}/isIntegerAboveOrEqual.js +0 -0
  176. /package/{number → cjs/number}/isIntegerBelow.js +0 -0
  177. /package/{number → cjs/number}/isIntegerBelowOrEqual.js +0 -0
  178. /package/{number → cjs/number}/isIntegerBetween.js +0 -0
  179. /package/{number → cjs/number}/isNumericalNaN.js +0 -0
  180. /package/{number → cjs/number}/randomBetween.js +0 -0
  181. /package/{number → cjs/number}/randomIntBetween.js +0 -0
  182. /package/{number → cjs/number}/round.js +0 -0
  183. /package/{number → cjs/number}/toPercentage.js +0 -0
  184. /package/{object → cjs/object}/define.js +0 -0
  185. /package/{object → cjs/object}/index.js +0 -0
  186. /package/{object → cjs/object}/is.js +0 -0
  187. /package/{object → cjs/object}/isNotEmpty.js +0 -0
  188. /package/{object → cjs/object}/merge.js +0 -0
  189. /package/{object → cjs/object}/omit.js +0 -0
  190. /package/{regexp → cjs/regexp}/index.js +0 -0
  191. /package/{regexp → cjs/regexp}/is.js +0 -0
  192. /package/{regexp → cjs/regexp}/sanitize.js +0 -0
  193. /package/{string → cjs/string}/humanizeBytes.js +0 -0
  194. /package/{string → cjs/string}/index.js +0 -0
  195. /package/{string → cjs/string}/is.js +0 -0
  196. /package/{string → cjs/string}/isBetween.js +0 -0
@@ -0,0 +1,43 @@
1
+ import { isNotEmptyObject } from '../object/isNotEmpty';
2
+ const FALLBACK = '_';
3
+ const defaultHandler = () => FALLBACK;
4
+ function groupBy(val, handler) {
5
+ let normalized;
6
+ if (Array.isArray(val)) {
7
+ if (!val.length)
8
+ return {};
9
+ normalized = val;
10
+ }
11
+ else if (val instanceof Set) {
12
+ if (!val.size)
13
+ return {};
14
+ normalized = [...val];
15
+ }
16
+ else {
17
+ return {};
18
+ }
19
+ const acc = {};
20
+ const n_handler = typeof handler === 'function'
21
+ ? handler
22
+ : typeof handler === 'string'
23
+ ? ((el) => el[handler])
24
+ : defaultHandler;
25
+ let key;
26
+ let el;
27
+ for (let i = 0; i < normalized.length; i++) {
28
+ el = normalized[i];
29
+ if (!isNotEmptyObject(el))
30
+ continue;
31
+ key = n_handler(el);
32
+ if (key === undefined || (typeof key === 'string' && !key.length))
33
+ key = FALLBACK;
34
+ if (!acc[key]) {
35
+ acc[key] = [el];
36
+ }
37
+ else {
38
+ acc[key].push(el);
39
+ }
40
+ }
41
+ return acc;
42
+ }
43
+ export { groupBy, groupBy as default };
@@ -0,0 +1,14 @@
1
+ export { dedupe } from './dedupe';
2
+ export { join } from './join';
3
+ export { mapFn } from './mapFn';
4
+ export { mapFnAsMap } from './mapFnAsMap';
5
+ export { mapKey } from './mapKey';
6
+ export { mapKeyAsMap } from './mapKeyAsMap';
7
+ export { mapPrimitive } from './mapPrimitive';
8
+ export { groupBy } from './groupBy';
9
+ export { shuffle } from './shuffle';
10
+ export { split } from './split';
11
+ export { sort } from './sort';
12
+ export { isArray } from './is';
13
+ export { isNotEmptyArray } from './isNotEmpty';
14
+ export { isNotEmptyArray as isNeArray } from './isNotEmpty';
@@ -0,0 +1,4 @@
1
+ function isArray(val) {
2
+ return Array.isArray(val);
3
+ }
4
+ export { isArray, isArray as default };
@@ -0,0 +1,4 @@
1
+ function isNotEmptyArray(val) {
2
+ return Array.isArray(val) && val.length !== 0;
3
+ }
4
+ export { isNotEmptyArray, isNotEmptyArray as default };
@@ -0,0 +1,58 @@
1
+ import { round } from '../number/round';
2
+ import { isIntegerAboveOrEqual } from '../number/isIntegerAboveOrEqual';
3
+ const SPACE_RGX = /(\s)+/g;
4
+ function join(val, opts) {
5
+ let normalized;
6
+ if (Array.isArray(val)) {
7
+ if (!val.length)
8
+ return '';
9
+ normalized = val;
10
+ }
11
+ else if (val instanceof Set) {
12
+ if (!val.size)
13
+ return '';
14
+ normalized = [...val];
15
+ }
16
+ else {
17
+ return '';
18
+ }
19
+ const DELIM = typeof opts?.delim === 'string' ? opts.delim : ' ';
20
+ const DEDUPE = opts?.dedupe === true ? new Set() : null;
21
+ const VALTRIM = opts?.valtrim ?? true;
22
+ const INNERTRIM = opts?.innertrim ?? false;
23
+ const VALROUND = isIntegerAboveOrEqual(opts?.valround, 0) ? opts.valround : false;
24
+ let result = '';
25
+ let has_val = false;
26
+ for (let i = 0; i < normalized.length; i++) {
27
+ const el = normalized[i];
28
+ let n_el;
29
+ if (typeof el === 'string') {
30
+ const trimmed = el.trim();
31
+ if (!trimmed)
32
+ continue;
33
+ n_el = VALTRIM ? trimmed : el;
34
+ if (INNERTRIM)
35
+ n_el = n_el.replace(SPACE_RGX, ' ');
36
+ }
37
+ else if (Number.isFinite(el)) {
38
+ n_el = '' + (VALROUND !== false ? round(el, VALROUND) : el);
39
+ }
40
+ else {
41
+ continue;
42
+ }
43
+ if (DEDUPE) {
44
+ if (DEDUPE.has(n_el))
45
+ continue;
46
+ DEDUPE.add(n_el);
47
+ }
48
+ if (has_val) {
49
+ result = result + DELIM + n_el;
50
+ }
51
+ else {
52
+ result = n_el;
53
+ has_val = true;
54
+ }
55
+ }
56
+ return opts?.trim ?? true ? result.trim() : result;
57
+ }
58
+ export { join, join as default };
@@ -0,0 +1,22 @@
1
+ import { merge } from '../object/merge';
2
+ function mapFn(arr, fn, opts) {
3
+ if ((!Array.isArray(arr) || !arr.length) ||
4
+ typeof fn !== 'function')
5
+ return {};
6
+ const MERGE = opts?.merge === true;
7
+ const TRANSFORM_FN = opts?.transform_fn;
8
+ const map = {};
9
+ for (let i = 0; i < arr.length; i++) {
10
+ const el = arr[i];
11
+ if (Object.prototype.toString.call(el) !== '[object Object]')
12
+ continue;
13
+ let hash = fn(el);
14
+ if (Number.isFinite(hash) || (typeof hash === 'string' && hash.length)) {
15
+ hash = hash + '';
16
+ const transformed = TRANSFORM_FN ? TRANSFORM_FN(el) : el;
17
+ map[hash] = MERGE && hash in map ? merge(map[hash], transformed, { union: true }) : transformed;
18
+ }
19
+ }
20
+ return map;
21
+ }
22
+ export { mapFn, mapFn as default };
@@ -0,0 +1,21 @@
1
+ import { merge } from '../object/merge';
2
+ function mapFnAsMap(arr, fn, opts) {
3
+ if ((!Array.isArray(arr) || !arr.length) ||
4
+ typeof fn !== 'function')
5
+ return new Map();
6
+ const MERGE = opts?.merge === true;
7
+ const TRANSFORM_FN = opts?.transform_fn;
8
+ const map = new Map();
9
+ for (let i = 0; i < arr.length; i++) {
10
+ const el = arr[i];
11
+ if (Object.prototype.toString.call(el) !== '[object Object]')
12
+ continue;
13
+ const hash = fn(el);
14
+ if (Number.isFinite(hash) || (typeof hash === 'string' && hash.length > 0)) {
15
+ const transformed = TRANSFORM_FN ? TRANSFORM_FN(el) : el;
16
+ map.set(hash, MERGE && map.has(hash) ? merge(map.get(hash), transformed, { union: true }) : transformed);
17
+ }
18
+ }
19
+ return map;
20
+ }
21
+ export { mapFnAsMap, mapFnAsMap as default };
@@ -0,0 +1,22 @@
1
+ import { merge } from '../object/merge';
2
+ function mapKey(arr, key, opts) {
3
+ if (!Array.isArray(arr) ||
4
+ !arr.length ||
5
+ typeof key !== 'string' ||
6
+ !key.length)
7
+ return {};
8
+ const FILTER_FN = opts?.filter_fn;
9
+ const MERGE = opts?.merge === true;
10
+ const TRANSFORMER = opts?.transform_fn;
11
+ const map = {};
12
+ for (let i = 0; i < arr.length; i++) {
13
+ const el = arr[i];
14
+ const el_key = el?.[key];
15
+ if (el_key !== undefined && (!FILTER_FN || FILTER_FN(el))) {
16
+ const transformed = TRANSFORMER ? TRANSFORMER(el) : el;
17
+ map[el_key] = MERGE && el_key in map ? merge(map[el_key], transformed, { union: true }) : transformed;
18
+ }
19
+ }
20
+ return map;
21
+ }
22
+ export { mapKey, mapKey as default };
@@ -0,0 +1,22 @@
1
+ import { merge } from '../object/merge';
2
+ function mapKeyAsMap(arr, key, opts) {
3
+ if (!Array.isArray(arr) ||
4
+ !arr.length ||
5
+ typeof key !== 'string' ||
6
+ !key.length)
7
+ return new Map();
8
+ const FILTER_FN = opts?.filter_fn;
9
+ const MERGE = opts?.merge === true;
10
+ const TRANSFORMER = opts?.transform_fn;
11
+ const map = new Map();
12
+ for (let i = 0; i < arr.length; i++) {
13
+ const el = arr[i];
14
+ const el_key = el?.[key];
15
+ if (el_key !== undefined && (!FILTER_FN || FILTER_FN(el))) {
16
+ const transformed = (TRANSFORMER ? TRANSFORMER(el) : el);
17
+ map.set(el_key, MERGE && map.has(el_key) ? merge(map.get(el_key), transformed, { union: true }) : transformed);
18
+ }
19
+ }
20
+ return map;
21
+ }
22
+ export { mapKeyAsMap, mapKeyAsMap as default };
@@ -0,0 +1,33 @@
1
+ import { round } from '../number/round';
2
+ import { isIntegerAboveOrEqual } from '../number/isIntegerAboveOrEqual';
3
+ function mapPrimitive(arr, opts = {}) {
4
+ if (!Array.isArray(arr) || !arr.length)
5
+ return {};
6
+ const VALTRIM = opts?.valtrim === true;
7
+ const VALROUND = isIntegerAboveOrEqual(opts?.valround, 0)
8
+ ? opts?.valround
9
+ : opts?.valround === true
10
+ ? 0
11
+ : null;
12
+ const KEYROUND = opts?.keyround === true;
13
+ const FILTER_FN = opts?.filter_fn;
14
+ const map = {};
15
+ for (let i = 0; i < arr.length; i++) {
16
+ const el = arr[i];
17
+ if (FILTER_FN && !FILTER_FN(el))
18
+ continue;
19
+ if (typeof el === 'string') {
20
+ const trimmed = el.trim();
21
+ if (!trimmed)
22
+ continue;
23
+ map[trimmed] = VALTRIM ? trimmed : el;
24
+ }
25
+ else if (Number.isFinite(el)) {
26
+ map[`${KEYROUND ? Math.round(el) : el}`] = VALROUND === null
27
+ ? el
28
+ : round(el, VALROUND);
29
+ }
30
+ }
31
+ return map;
32
+ }
33
+ export { mapPrimitive, mapPrimitive as default };
@@ -0,0 +1,10 @@
1
+ function shuffle(arr) {
2
+ if (!Array.isArray(arr))
3
+ return;
4
+ let j;
5
+ for (let i = arr.length - 1; i > 0; i--) {
6
+ j = (Math.random() * (i + 1)) | 0;
7
+ [arr[i], arr[j]] = [arr[j], arr[i]];
8
+ }
9
+ }
10
+ export { shuffle, shuffle as default };
@@ -0,0 +1,137 @@
1
+ import { isObject } from '../object/is';
2
+ const INSERTION_SORT_THRESHOLD = 10;
3
+ function partitionAsc(arr, low, high) {
4
+ const pivot = arr[(low + high) >> 1][0];
5
+ let i = low;
6
+ let j = high;
7
+ while (i <= j) {
8
+ while (arr[i][0] < pivot)
9
+ i++;
10
+ while (arr[j][0] > pivot)
11
+ j--;
12
+ if (i <= j) {
13
+ [arr[i], arr[j]] = [arr[j], arr[i]];
14
+ i++;
15
+ j--;
16
+ }
17
+ }
18
+ return i;
19
+ }
20
+ function quickSortAsc(arr) {
21
+ const stack = [[0, arr.length - 1]];
22
+ while (stack.length) {
23
+ const [low, high] = stack.pop();
24
+ if (high - low <= INSERTION_SORT_THRESHOLD) {
25
+ for (let i = low + 1; i <= high; i++) {
26
+ const key = arr[i];
27
+ let j = i - 1;
28
+ while (j >= low && arr[j][0] > key[0]) {
29
+ arr[j + 1] = arr[j];
30
+ j--;
31
+ }
32
+ arr[j + 1] = key;
33
+ }
34
+ }
35
+ else {
36
+ const p = partitionAsc(arr, low, high);
37
+ if (p - 1 > low)
38
+ stack.push([low, p - 1]);
39
+ if (p < high)
40
+ stack.push([p, high]);
41
+ }
42
+ }
43
+ }
44
+ function partitionDesc(arr, low, high) {
45
+ const pivot = arr[(low + high) >> 1][0];
46
+ let i = low;
47
+ let j = high;
48
+ while (i <= j) {
49
+ while (arr[i][0] > pivot)
50
+ i++;
51
+ while (arr[j][0] < pivot)
52
+ j--;
53
+ if (i <= j) {
54
+ [arr[i], arr[j]] = [arr[j], arr[i]];
55
+ i++;
56
+ j--;
57
+ }
58
+ }
59
+ return i;
60
+ }
61
+ function quickSortDesc(arr) {
62
+ const stack = [[0, arr.length - 1]];
63
+ while (stack.length) {
64
+ const [low, high] = stack.pop();
65
+ if (high - low <= INSERTION_SORT_THRESHOLD) {
66
+ for (let i = low + 1; i <= high; i++) {
67
+ const key = arr[i];
68
+ let j = i - 1;
69
+ while (j >= low && arr[j][0] < key[0]) {
70
+ arr[j + 1] = arr[j];
71
+ j--;
72
+ }
73
+ arr[j + 1] = key;
74
+ }
75
+ }
76
+ else {
77
+ const p = partitionDesc(arr, low, high);
78
+ if (p - 1 > low)
79
+ stack.push([low, p - 1]);
80
+ if (p < high)
81
+ stack.push([p, high]);
82
+ }
83
+ }
84
+ }
85
+ function sort(arr, by, dir = 'asc', opts) {
86
+ if (!Array.isArray(arr))
87
+ return [];
88
+ const len = arr.length;
89
+ if (!len)
90
+ return [];
91
+ const NOKEY_HIDE = opts?.nokey_hide === true;
92
+ const NOKEY_AT_END = opts?.nokey_atend !== false;
93
+ let FILTER_FN = isObject;
94
+ if (typeof opts?.filter_fn === 'function') {
95
+ const fn = opts.filter_fn;
96
+ FILTER_FN = (el => isObject(el) && fn(el));
97
+ }
98
+ const by_type = typeof by;
99
+ let BY_FN;
100
+ switch (by_type) {
101
+ case 'function':
102
+ BY_FN = by;
103
+ break;
104
+ case 'string':
105
+ if (!by.length)
106
+ throw new Error('Sort by as string should contain content');
107
+ BY_FN = ((val) => val?.[by]);
108
+ break;
109
+ default:
110
+ throw new Error('Sort by should either be a string with content or a function');
111
+ }
112
+ const tosort_arr = [];
113
+ const noval_arr = [];
114
+ for (let i = 0; i < len; i++) {
115
+ const el = arr[i];
116
+ if (FILTER_FN(el)) {
117
+ const val = BY_FN(el);
118
+ if (val === undefined)
119
+ noval_arr.push(el);
120
+ else
121
+ tosort_arr.push([val, el]);
122
+ }
123
+ }
124
+ if (dir === 'desc')
125
+ quickSortDesc(tosort_arr);
126
+ else
127
+ quickSortAsc(tosort_arr);
128
+ const rslt = [];
129
+ if (!NOKEY_HIDE && !NOKEY_AT_END)
130
+ rslt.push(...noval_arr);
131
+ for (let i = 0; i < tosort_arr.length; i++)
132
+ rslt.push(tosort_arr[i][1]);
133
+ if (!NOKEY_HIDE && NOKEY_AT_END)
134
+ rslt.push(...noval_arr);
135
+ return rslt;
136
+ }
137
+ export { sort, sort as default };
@@ -0,0 +1,43 @@
1
+ function split(arr, size, opts) {
2
+ if (!Number.isInteger(size) ||
3
+ size <= 0)
4
+ throw new Error('split requires a positive integer size');
5
+ let normalized;
6
+ if (Array.isArray(arr)) {
7
+ normalized = arr;
8
+ }
9
+ else if (arr instanceof Set) {
10
+ normalized = [...arr];
11
+ }
12
+ else {
13
+ throw new Error('split requires an array or set');
14
+ }
15
+ const FILTER_FN = typeof opts?.filter_fn === 'function' ? opts?.filter_fn : false;
16
+ const result = [];
17
+ let cursor = [];
18
+ let ticker = 0;
19
+ const len = normalized.length;
20
+ if (FILTER_FN) {
21
+ for (let i = 0; i < len; i++) {
22
+ const el = normalized[i];
23
+ if (FILTER_FN && !FILTER_FN(el))
24
+ continue;
25
+ ticker++;
26
+ cursor.push(el);
27
+ if (ticker === size) {
28
+ result.push(cursor);
29
+ cursor = [];
30
+ ticker = 0;
31
+ }
32
+ }
33
+ if (ticker)
34
+ result.push(cursor);
35
+ }
36
+ else {
37
+ for (let i = 0; i < len; i += size) {
38
+ result.push(normalized.slice(i, i + size));
39
+ }
40
+ }
41
+ return result;
42
+ }
43
+ export { split, split as default };
@@ -0,0 +1 @@
1
+ export { isBoolean } from './is';
@@ -0,0 +1,4 @@
1
+ function isBoolean(val) {
2
+ return val === true || val === false;
3
+ }
4
+ export { isBoolean, isBoolean as default };
@@ -0,0 +1,116 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _LRUCache_map, _LRUCache_head, _LRUCache_tail, _LRUCache_max_size, _LRUCache_size;
13
+ import isObject from '../object/is';
14
+ import isIntegerGt from '../number/isIntegerAbove';
15
+ class LRUCache {
16
+ constructor(opts = {}) {
17
+ _LRUCache_map.set(this, void 0);
18
+ _LRUCache_head.set(this, null);
19
+ _LRUCache_tail.set(this, null);
20
+ _LRUCache_max_size.set(this, void 0);
21
+ _LRUCache_size.set(this, 0);
22
+ const { max_size = 100 } = isObject(opts) ? opts : {};
23
+ __classPrivateFieldSet(this, _LRUCache_map, Object.create(null), "f");
24
+ __classPrivateFieldSet(this, _LRUCache_max_size, isIntegerGt(max_size, 0) ? max_size : 100, "f");
25
+ }
26
+ get max_size() {
27
+ return __classPrivateFieldGet(this, _LRUCache_max_size, "f");
28
+ }
29
+ set max_size(max_size) {
30
+ if (!isIntegerGt(max_size, 0))
31
+ throw new Error('max_size must be a positive integer');
32
+ __classPrivateFieldSet(this, _LRUCache_max_size, max_size, "f");
33
+ while (__classPrivateFieldGet(this, _LRUCache_size, "f") > max_size)
34
+ this.evictTail();
35
+ }
36
+ has(key) {
37
+ return key in __classPrivateFieldGet(this, _LRUCache_map, "f");
38
+ }
39
+ get(key) {
40
+ const node = __classPrivateFieldGet(this, _LRUCache_map, "f")[key];
41
+ if (!node)
42
+ return undefined;
43
+ this.moveToFront(node);
44
+ return node.value;
45
+ }
46
+ set(key, value) {
47
+ var _a;
48
+ let node = __classPrivateFieldGet(this, _LRUCache_map, "f")[key];
49
+ if (node !== undefined) {
50
+ node.value = value;
51
+ this.moveToFront(node);
52
+ }
53
+ else {
54
+ node = { key, value, prev: null, next: null };
55
+ __classPrivateFieldGet(this, _LRUCache_map, "f")[key] = node;
56
+ this.addToFront(node);
57
+ __classPrivateFieldSet(this, _LRUCache_size, (_a = __classPrivateFieldGet(this, _LRUCache_size, "f"), _a++, _a), "f");
58
+ if (__classPrivateFieldGet(this, _LRUCache_tail, "f") && __classPrivateFieldGet(this, _LRUCache_size, "f") > __classPrivateFieldGet(this, _LRUCache_max_size, "f")) {
59
+ this.evictTail();
60
+ }
61
+ }
62
+ }
63
+ del(key) {
64
+ var _a;
65
+ const node = __classPrivateFieldGet(this, _LRUCache_map, "f")[key];
66
+ if (!node)
67
+ return;
68
+ this.removeNode(node);
69
+ delete __classPrivateFieldGet(this, _LRUCache_map, "f")[key];
70
+ __classPrivateFieldSet(this, _LRUCache_size, (_a = __classPrivateFieldGet(this, _LRUCache_size, "f"), _a--, _a), "f");
71
+ }
72
+ clear() {
73
+ __classPrivateFieldSet(this, _LRUCache_map, Object.create(null), "f");
74
+ __classPrivateFieldSet(this, _LRUCache_head, null, "f");
75
+ __classPrivateFieldSet(this, _LRUCache_tail, null, "f");
76
+ __classPrivateFieldSet(this, _LRUCache_size, 0, "f");
77
+ }
78
+ addToFront(node) {
79
+ node.next = __classPrivateFieldGet(this, _LRUCache_head, "f");
80
+ node.prev = null;
81
+ if (__classPrivateFieldGet(this, _LRUCache_head, "f")) {
82
+ __classPrivateFieldGet(this, _LRUCache_head, "f").prev = node;
83
+ }
84
+ __classPrivateFieldSet(this, _LRUCache_head, node, "f");
85
+ if (!__classPrivateFieldGet(this, _LRUCache_tail, "f")) {
86
+ __classPrivateFieldSet(this, _LRUCache_tail, node, "f");
87
+ }
88
+ }
89
+ removeNode(node) {
90
+ if (node.prev)
91
+ node.prev.next = node.next;
92
+ else
93
+ __classPrivateFieldSet(this, _LRUCache_head, node.next, "f");
94
+ if (node.next)
95
+ node.next.prev = node.prev;
96
+ else
97
+ __classPrivateFieldSet(this, _LRUCache_tail, node.prev, "f");
98
+ node.prev = null;
99
+ node.next = null;
100
+ }
101
+ moveToFront(node) {
102
+ if (__classPrivateFieldGet(this, _LRUCache_head, "f") === node)
103
+ return;
104
+ this.removeNode(node);
105
+ this.addToFront(node);
106
+ }
107
+ evictTail() {
108
+ var _a;
109
+ const old_tail = __classPrivateFieldGet(this, _LRUCache_tail, "f");
110
+ this.removeNode(old_tail);
111
+ delete __classPrivateFieldGet(this, _LRUCache_map, "f")[old_tail.key];
112
+ __classPrivateFieldSet(this, _LRUCache_size, (_a = __classPrivateFieldGet(this, _LRUCache_size, "f"), _a--, _a), "f");
113
+ }
114
+ }
115
+ _LRUCache_map = new WeakMap(), _LRUCache_head = new WeakMap(), _LRUCache_tail = new WeakMap(), _LRUCache_max_size = new WeakMap(), _LRUCache_size = new WeakMap();
116
+ export { LRUCache, LRUCache as default };
@@ -0,0 +1,3 @@
1
+ export { memoize } from './memoize';
2
+ export { LRUCache } from './LRU';
3
+ export { LRUCache as LRU } from './LRU';
@@ -0,0 +1,37 @@
1
+ import isAsyncFunction from '../function/isAsync';
2
+ import fnv1A from '../hash/fnv1A';
3
+ import isIntegerGt from '../number/isIntegerAbove';
4
+ import LRU from './LRU';
5
+ function memoize(fn, resolver, cache_duration_ms = false, cache_max_size = 100) {
6
+ const cache_duration = isIntegerGt(cache_duration_ms, 0) ? cache_duration_ms : false;
7
+ const cache = new LRU({ max_size: cache_max_size });
8
+ const isResolverFn = typeof resolver === 'function';
9
+ const memoized = isAsyncFunction(fn)
10
+ ? async function (...args) {
11
+ let key = isResolverFn ? resolver(...args) : args[0];
12
+ key = typeof key === 'string' ? key : Number.isFinite(key) ? String(key) : String(fnv1A(key));
13
+ const cached_val = cache.get(key);
14
+ const now = Date.now();
15
+ if (cached_val !== undefined && (cache_duration === false || (now - cached_val.ts) < cache_duration)) {
16
+ return cached_val.r;
17
+ }
18
+ const result = await fn(...args);
19
+ cache.set(key, { r: result, ts: now });
20
+ return result;
21
+ }
22
+ : function (...args) {
23
+ let key = isResolverFn ? resolver(...args) : args[0];
24
+ key = typeof key === 'string' ? key : Number.isFinite(key) ? String(key) : String(fnv1A(key));
25
+ const cached_val = cache.get(key);
26
+ const now = Date.now();
27
+ if (cached_val !== undefined && (cache_duration === false || (now - cached_val.ts) < cache_duration)) {
28
+ return cached_val.r;
29
+ }
30
+ const result = fn(...args);
31
+ cache.set(key, { r: result, ts: now });
32
+ return result;
33
+ };
34
+ memoized.cache = cache;
35
+ return memoized;
36
+ }
37
+ export { memoize, memoize as default };
@@ -0,0 +1,41 @@
1
+ import { convertToDate } from './convertToDate';
2
+ function addUTC(val, amt = 0, key = 'millisecond') {
3
+ if (!Number.isInteger(amt))
4
+ throw new TypeError('Amount needs to be an integer');
5
+ const n_val = convertToDate(val);
6
+ if (n_val === null)
7
+ throw new TypeError('addUTC requires a date object');
8
+ const year = n_val.getUTCFullYear();
9
+ const month = n_val.getUTCMonth();
10
+ const date = n_val.getUTCDate();
11
+ const hour = n_val.getUTCHours();
12
+ const min = n_val.getUTCMinutes();
13
+ const sec = n_val.getUTCSeconds();
14
+ const ms = n_val.getUTCMilliseconds();
15
+ switch (key) {
16
+ case 'years':
17
+ case 'year':
18
+ return new Date(Date.UTC(year + amt, month, date, hour, min, sec, ms));
19
+ case 'months':
20
+ case 'month':
21
+ return new Date(Date.UTC(year, month + amt, date, hour, min, sec, ms));
22
+ case 'days':
23
+ case 'day':
24
+ return new Date(Date.UTC(year, month, date + amt, hour, min, sec, ms));
25
+ case 'hours':
26
+ case 'hour':
27
+ return new Date(Date.UTC(year, month, date, hour + amt, min, sec, ms));
28
+ case 'minutes':
29
+ case 'minute':
30
+ return new Date(Date.UTC(year, month, date, hour, min + amt, sec, ms));
31
+ case 'seconds':
32
+ case 'second':
33
+ return new Date(Date.UTC(year, month, date, hour, min, sec + amt, ms));
34
+ case 'milliseconds':
35
+ case 'millisecond':
36
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms + amt));
37
+ default:
38
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms));
39
+ }
40
+ }
41
+ export { addUTC, addUTC as default };
@@ -0,0 +1,12 @@
1
+ import { isDate } from './is';
2
+ function convertToDate(val) {
3
+ if (isDate(val)) {
4
+ return val;
5
+ }
6
+ else if (typeof val === 'string') {
7
+ const date = new Date(val);
8
+ return Number.isNaN(date.getTime()) ? null : date;
9
+ }
10
+ return null;
11
+ }
12
+ export { convertToDate, convertToDate as default };