@valkyriestudios/utils 12.37.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 (193) 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}/split.js +16 -7
  8. package/cjs/caching/LRU.js +123 -0
  9. package/{caching → cjs/caching}/memoize.js +11 -5
  10. package/{date → cjs/date}/format.js +4 -1
  11. package/{date → cjs/date}/isFormat.js +4 -1
  12. package/esm/array/dedupe.js +52 -0
  13. package/esm/array/groupBy.js +43 -0
  14. package/esm/array/index.js +14 -0
  15. package/esm/array/is.js +4 -0
  16. package/esm/array/isNotEmpty.js +4 -0
  17. package/esm/array/join.js +58 -0
  18. package/esm/array/mapFn.js +22 -0
  19. package/esm/array/mapFnAsMap.js +21 -0
  20. package/esm/array/mapKey.js +22 -0
  21. package/esm/array/mapKeyAsMap.js +22 -0
  22. package/esm/array/mapPrimitive.js +33 -0
  23. package/esm/array/shuffle.js +10 -0
  24. package/esm/array/sort.js +137 -0
  25. package/esm/array/split.js +43 -0
  26. package/esm/boolean/index.js +1 -0
  27. package/esm/boolean/is.js +4 -0
  28. package/esm/caching/LRU.js +116 -0
  29. package/esm/caching/index.js +3 -0
  30. package/esm/caching/memoize.js +37 -0
  31. package/esm/date/addUTC.js +41 -0
  32. package/esm/date/convertToDate.js +12 -0
  33. package/esm/date/diff.js +34 -0
  34. package/esm/date/endOfUTC.js +50 -0
  35. package/esm/date/format.js +241 -0
  36. package/esm/date/index.js +15 -0
  37. package/esm/date/is.js +4 -0
  38. package/esm/date/isFormat.js +118 -0
  39. package/esm/date/isLeap.js +9 -0
  40. package/esm/date/nowUnix.js +4 -0
  41. package/esm/date/nowUnixMs.js +4 -0
  42. package/esm/date/setTimeUTC.js +10 -0
  43. package/esm/date/startOfUTC.js +50 -0
  44. package/esm/date/toUTC.js +7 -0
  45. package/esm/date/toUnix.js +7 -0
  46. package/esm/deep/freeze.js +21 -0
  47. package/esm/deep/get.js +84 -0
  48. package/esm/deep/index.js +8 -0
  49. package/esm/deep/seal.js +21 -0
  50. package/esm/deep/set.js +50 -0
  51. package/esm/equal.js +67 -0
  52. package/esm/formdata/index.js +2 -0
  53. package/esm/formdata/is.js +4 -0
  54. package/esm/formdata/toObject.js +89 -0
  55. package/esm/function/debounce.js +39 -0
  56. package/esm/function/index.js +9 -0
  57. package/esm/function/is.js +4 -0
  58. package/esm/function/isAsync.js +4 -0
  59. package/esm/function/noop.js +2 -0
  60. package/esm/function/noopresolve.js +4 -0
  61. package/esm/function/noopreturn.js +4 -0
  62. package/esm/function/sleep.js +6 -0
  63. package/esm/hash/fnv1A.js +51 -0
  64. package/esm/hash/guid.js +31 -0
  65. package/esm/hash/index.js +2 -0
  66. package/esm/is.js +63 -0
  67. package/esm/modules/PubSub.js +187 -0
  68. package/esm/modules/Scheduler.js +343 -0
  69. package/esm/modules/index.js +2 -0
  70. package/esm/number/index.js +37 -0
  71. package/esm/number/is.js +4 -0
  72. package/esm/number/isAbove.js +4 -0
  73. package/esm/number/isAboveOrEqual.js +4 -0
  74. package/esm/number/isBelow.js +4 -0
  75. package/esm/number/isBelowOrEqual.js +4 -0
  76. package/esm/number/isBetween.js +7 -0
  77. package/esm/number/isInteger.js +4 -0
  78. package/esm/number/isIntegerAbove.js +4 -0
  79. package/esm/number/isIntegerAboveOrEqual.js +4 -0
  80. package/esm/number/isIntegerBelow.js +4 -0
  81. package/esm/number/isIntegerBelowOrEqual.js +4 -0
  82. package/esm/number/isIntegerBetween.js +7 -0
  83. package/esm/number/isNumericalNaN.js +4 -0
  84. package/esm/number/randomBetween.js +7 -0
  85. package/esm/number/randomIntBetween.js +7 -0
  86. package/esm/number/round.js +10 -0
  87. package/esm/number/toPercentage.js +9 -0
  88. package/esm/object/define.js +7 -0
  89. package/esm/object/index.js +7 -0
  90. package/esm/object/is.js +4 -0
  91. package/esm/object/isNotEmpty.js +9 -0
  92. package/esm/object/merge.js +31 -0
  93. package/esm/object/omit.js +30 -0
  94. package/esm/object/pick.js +33 -0
  95. package/esm/regexp/index.js +3 -0
  96. package/esm/regexp/is.js +4 -0
  97. package/esm/regexp/sanitize.js +7 -0
  98. package/esm/string/humanizeBytes.js +15 -0
  99. package/esm/string/humanizeNumber.js +40 -0
  100. package/esm/string/index.js +7 -0
  101. package/esm/string/is.js +4 -0
  102. package/esm/string/isBetween.js +12 -0
  103. package/esm/string/isNotEmpty.js +20 -0
  104. package/esm/string/shorten.js +18 -0
  105. package/index.d.ts +19 -15
  106. package/package.json +192 -850
  107. package/caching/LRU.js +0 -72
  108. /package/{array → cjs/array}/dedupe.js +0 -0
  109. /package/{array → cjs/array}/index.js +0 -0
  110. /package/{array → cjs/array}/is.js +0 -0
  111. /package/{array → cjs/array}/isNotEmpty.js +0 -0
  112. /package/{array → cjs/array}/mapFn.js +0 -0
  113. /package/{array → cjs/array}/mapFnAsMap.js +0 -0
  114. /package/{array → cjs/array}/mapKey.js +0 -0
  115. /package/{array → cjs/array}/mapKeyAsMap.js +0 -0
  116. /package/{array → cjs/array}/mapPrimitive.js +0 -0
  117. /package/{array → cjs/array}/shuffle.js +0 -0
  118. /package/{array → cjs/array}/sort.js +0 -0
  119. /package/{boolean → cjs/boolean}/index.js +0 -0
  120. /package/{boolean → cjs/boolean}/is.js +0 -0
  121. /package/{caching → cjs/caching}/index.js +0 -0
  122. /package/{date → cjs/date}/addUTC.js +0 -0
  123. /package/{date → cjs/date}/convertToDate.js +0 -0
  124. /package/{date → cjs/date}/diff.js +0 -0
  125. /package/{date → cjs/date}/endOfUTC.js +0 -0
  126. /package/{date → cjs/date}/index.js +0 -0
  127. /package/{date → cjs/date}/is.js +0 -0
  128. /package/{date → cjs/date}/isLeap.js +0 -0
  129. /package/{date → cjs/date}/nowUnix.js +0 -0
  130. /package/{date → cjs/date}/nowUnixMs.js +0 -0
  131. /package/{date → cjs/date}/setTimeUTC.js +0 -0
  132. /package/{date → cjs/date}/startOfUTC.js +0 -0
  133. /package/{date → cjs/date}/toUTC.js +0 -0
  134. /package/{date → cjs/date}/toUnix.js +0 -0
  135. /package/{deep → cjs/deep}/freeze.js +0 -0
  136. /package/{deep → cjs/deep}/get.js +0 -0
  137. /package/{deep → cjs/deep}/index.js +0 -0
  138. /package/{deep → cjs/deep}/seal.js +0 -0
  139. /package/{deep → cjs/deep}/set.js +0 -0
  140. /package/{equal.js → cjs/equal.js} +0 -0
  141. /package/{formdata → cjs/formdata}/index.js +0 -0
  142. /package/{formdata → cjs/formdata}/is.js +0 -0
  143. /package/{formdata → cjs/formdata}/toObject.js +0 -0
  144. /package/{function → cjs/function}/debounce.js +0 -0
  145. /package/{function → cjs/function}/index.js +0 -0
  146. /package/{function → cjs/function}/is.js +0 -0
  147. /package/{function → cjs/function}/isAsync.js +0 -0
  148. /package/{function → cjs/function}/noop.js +0 -0
  149. /package/{function → cjs/function}/noopresolve.js +0 -0
  150. /package/{function → cjs/function}/noopreturn.js +0 -0
  151. /package/{function → cjs/function}/sleep.js +0 -0
  152. /package/{hash → cjs/hash}/fnv1A.js +0 -0
  153. /package/{hash → cjs/hash}/guid.js +0 -0
  154. /package/{hash → cjs/hash}/index.js +0 -0
  155. /package/{is.js → cjs/is.js} +0 -0
  156. /package/{modules → cjs/modules}/PubSub.js +0 -0
  157. /package/{modules → cjs/modules}/Scheduler.js +0 -0
  158. /package/{modules → cjs/modules}/index.js +0 -0
  159. /package/{number → cjs/number}/index.js +0 -0
  160. /package/{number → cjs/number}/is.js +0 -0
  161. /package/{number → cjs/number}/isAbove.js +0 -0
  162. /package/{number → cjs/number}/isAboveOrEqual.js +0 -0
  163. /package/{number → cjs/number}/isBelow.js +0 -0
  164. /package/{number → cjs/number}/isBelowOrEqual.js +0 -0
  165. /package/{number → cjs/number}/isBetween.js +0 -0
  166. /package/{number → cjs/number}/isInteger.js +0 -0
  167. /package/{number → cjs/number}/isIntegerAbove.js +0 -0
  168. /package/{number → cjs/number}/isIntegerAboveOrEqual.js +0 -0
  169. /package/{number → cjs/number}/isIntegerBelow.js +0 -0
  170. /package/{number → cjs/number}/isIntegerBelowOrEqual.js +0 -0
  171. /package/{number → cjs/number}/isIntegerBetween.js +0 -0
  172. /package/{number → cjs/number}/isNumericalNaN.js +0 -0
  173. /package/{number → cjs/number}/randomBetween.js +0 -0
  174. /package/{number → cjs/number}/randomIntBetween.js +0 -0
  175. /package/{number → cjs/number}/round.js +0 -0
  176. /package/{number → cjs/number}/toPercentage.js +0 -0
  177. /package/{object → cjs/object}/define.js +0 -0
  178. /package/{object → cjs/object}/index.js +0 -0
  179. /package/{object → cjs/object}/is.js +0 -0
  180. /package/{object → cjs/object}/isNotEmpty.js +0 -0
  181. /package/{object → cjs/object}/merge.js +0 -0
  182. /package/{object → cjs/object}/omit.js +0 -0
  183. /package/{object → cjs/object}/pick.js +0 -0
  184. /package/{regexp → cjs/regexp}/index.js +0 -0
  185. /package/{regexp → cjs/regexp}/is.js +0 -0
  186. /package/{regexp → cjs/regexp}/sanitize.js +0 -0
  187. /package/{string → cjs/string}/humanizeBytes.js +0 -0
  188. /package/{string → cjs/string}/humanizeNumber.js +0 -0
  189. /package/{string → cjs/string}/index.js +0 -0
  190. /package/{string → cjs/string}/is.js +0 -0
  191. /package/{string → cjs/string}/isBetween.js +0 -0
  192. /package/{string → cjs/string}/isNotEmpty.js +0 -0
  193. /package/{string → cjs/string}/shorten.js +0 -0
@@ -0,0 +1,15 @@
1
+ import { humanizeNumber } from './humanizeNumber';
2
+ const DEFAULT_UNITS = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
3
+ function humanizeBytes(val, options = {}) {
4
+ return humanizeNumber(val, {
5
+ delim: 'delim' in options ? options.delim : ',',
6
+ separator: 'separator' in options ? options.separator : '.',
7
+ precision: 'precision' in options ? options.precision : 2,
8
+ units: Array.isArray(options?.units) && options.units.length
9
+ ? options.units
10
+ : DEFAULT_UNITS,
11
+ divider: 1024,
12
+ real: true,
13
+ });
14
+ }
15
+ export { humanizeBytes, humanizeBytes as default };
@@ -0,0 +1,40 @@
1
+ import { round } from '../number/round';
2
+ const DEFAULT_UNITS = ['', 'k', 'm', 'b', 't', 'q'];
3
+ function humanizeNumber(val, options = {}) {
4
+ const DELIM = typeof options?.delim === 'string' ? options.delim : ',';
5
+ const SEPARATOR = typeof options?.separator === 'string' && options.separator.length ? options.separator : '.';
6
+ const PRECISION = Number.isInteger(options?.precision) && options.precision >= 0 ? options.precision : 2;
7
+ const DIVIDER = Number.isInteger(options?.divider) && options.divider >= 2 ? options.divider : 1000;
8
+ const REAL = options?.real === true;
9
+ const UNITS = Array.isArray(options?.units) && options.units.length
10
+ ? options.units
11
+ : options?.units === false
12
+ ? false
13
+ : DEFAULT_UNITS;
14
+ let normalized = typeof val === 'string' ? parseFloat(val) : val;
15
+ if (!Number.isFinite(normalized) || normalized === 0)
16
+ return UNITS ? '0' + UNITS[0] : '0';
17
+ if (REAL)
18
+ normalized = Math.round(normalized);
19
+ const sign = normalized < 0 ? '-' : '';
20
+ normalized = Math.abs(normalized);
21
+ let unit_ix = 0;
22
+ if (UNITS) {
23
+ while (normalized >= DIVIDER && unit_ix < UNITS.length - 1) {
24
+ normalized /= DIVIDER;
25
+ unit_ix++;
26
+ }
27
+ }
28
+ const humanized = ('' + round(normalized, PRECISION)).split('.', 2);
29
+ const integer_part = humanized[0];
30
+ const integer_part_len = integer_part.length;
31
+ let formattedIntegerPart = '';
32
+ for (let i = 0; i < integer_part_len; i++) {
33
+ if (i > 0 && (integer_part_len - i) % 3 === 0) {
34
+ formattedIntegerPart += DELIM;
35
+ }
36
+ formattedIntegerPart += integer_part[i];
37
+ }
38
+ return sign + formattedIntegerPart + (humanized[1] ? SEPARATOR + humanized[1] : '') + (UNITS ? UNITS[unit_ix] : '');
39
+ }
40
+ export { humanizeNumber, humanizeNumber as default };
@@ -0,0 +1,7 @@
1
+ export { humanizeBytes } from './humanizeBytes';
2
+ export { humanizeNumber } from './humanizeNumber';
3
+ export { shorten } from './shorten';
4
+ export { isString } from './is';
5
+ export { isNotEmptyString } from './isNotEmpty';
6
+ export { isNotEmptyString as isNeString } from './isNotEmpty';
7
+ export { isStringBetween } from './isBetween';
@@ -0,0 +1,4 @@
1
+ function isString(val) {
2
+ return typeof val === 'string';
3
+ }
4
+ export { isString, isString as default };
@@ -0,0 +1,12 @@
1
+ function isStringBetween(val, min, max, trimmed = true) {
2
+ if (typeof val !== 'string' ||
3
+ !Number.isFinite(min) ||
4
+ min < 0 ||
5
+ !Number.isFinite(max) ||
6
+ max < 0 ||
7
+ min >= max)
8
+ return false;
9
+ const length = (trimmed === true ? val.trim() : val).length;
10
+ return length >= min && length <= max;
11
+ }
12
+ export { isStringBetween, isStringBetween as default };
@@ -0,0 +1,20 @@
1
+ function isNotEmptyString(val, trimmed = true) {
2
+ if (typeof val !== 'string')
3
+ return false;
4
+ if (trimmed) {
5
+ for (let i = 0; i < val.length; i++) {
6
+ switch (val[i]) {
7
+ case ' ':
8
+ case '\t':
9
+ case '\n':
10
+ case '\r':
11
+ break;
12
+ default:
13
+ return true;
14
+ }
15
+ }
16
+ return false;
17
+ }
18
+ return val.length > 0;
19
+ }
20
+ export { isNotEmptyString, isNotEmptyString as default };
@@ -0,0 +1,18 @@
1
+ function shorten(val, length, postfix = '...', truncate_words = true) {
2
+ if (typeof val !== 'string')
3
+ return '';
4
+ if (typeof postfix !== 'string' || !Number.isInteger(length) || length <= 0)
5
+ return val;
6
+ const sanitized = val.trim();
7
+ if (sanitized.length <= length)
8
+ return sanitized;
9
+ const truncated = sanitized.substring(0, length);
10
+ if (truncate_words)
11
+ return truncated + postfix;
12
+ let end = length;
13
+ while (end && sanitized[end] !== ' ' && sanitized[end - 1] !== ' ') {
14
+ end--;
15
+ }
16
+ return sanitized.substring(0, end).trimEnd() + postfix;
17
+ }
18
+ export { shorten, shorten as default };
package/index.d.ts CHANGED
@@ -26,7 +26,7 @@ declare module "array/join" {
26
26
  valtrim?: boolean;
27
27
  valround?: number;
28
28
  }
29
- function join(val: unknown[], opts?: joinOptions): string;
29
+ function join<T>(val: T[] | Set<T>, opts?: joinOptions): string;
30
30
  export { join, join as default };
31
31
  }
32
32
  declare module "object/merge" {
@@ -95,8 +95,8 @@ declare module "object/isNotEmpty" {
95
95
  export { isNotEmptyObject, isNotEmptyObject as default };
96
96
  }
97
97
  declare module "array/groupBy" {
98
- type Handler<T> = (val: T) => string | number | boolean;
99
- function groupBy<T extends Record<string, any>>(arr: T[], handler: Handler<T> | keyof T): Record<string, T[]>;
98
+ type Handler<T> = (val: T) => string | number | boolean | undefined;
99
+ function groupBy<T extends Record<string, any>>(val: T[] | Set<T>, handler: Handler<T> | keyof T): Record<string, T[]>;
100
100
  export { groupBy, groupBy as default };
101
101
  }
102
102
  declare module "array/shuffle" {
@@ -107,7 +107,7 @@ declare module "array/split" {
107
107
  type SplitOptions<T> = {
108
108
  filter_fn?: (el: T) => boolean;
109
109
  };
110
- function split<T>(arr: T[], size: number, opts?: SplitOptions<T>): T[][];
110
+ function split<T>(arr: T[] | Set<T>, size: number, opts?: SplitOptions<T>): T[][];
111
111
  export { split, split as default };
112
112
  }
113
113
  declare module "object/is" {
@@ -188,16 +188,20 @@ declare module "caching/LRU" {
188
188
  export type LRUCacheOptions = {
189
189
  max_size?: number;
190
190
  };
191
- class LRUCache<K, V> {
191
+ class LRUCache<V> {
192
192
  #private;
193
193
  constructor(opts?: LRUCacheOptions);
194
194
  get max_size(): number;
195
195
  set max_size(max_size: number);
196
- has(key: K): boolean;
197
- get(key: K): V | undefined;
198
- set(key: K, value: V): void;
199
- del(key: K): void;
196
+ has(key: string): boolean;
197
+ get(key: string): V | undefined;
198
+ set(key: string, value: V): void;
199
+ del(key: string): void;
200
200
  clear(): void;
201
+ private addToFront;
202
+ private removeNode;
203
+ private moveToFront;
204
+ private evictTail;
201
205
  }
202
206
  export { LRUCache, LRUCache as default };
203
207
  }
@@ -614,6 +618,12 @@ declare module "is" {
614
618
  }>;
615
619
  export { Is, Is as default };
616
620
  }
621
+ declare module "hash/fnv1A" {
622
+ export const FNV_32 = 2166136261;
623
+ export const FNV_64 = 1099511628211;
624
+ function fnv1A(data: unknown, offset?: number): number;
625
+ export { fnv1A, fnv1A as default };
626
+ }
617
627
  declare module "caching/memoize" {
618
628
  function memoize<T extends (...args: any[]) => unknown>(fn: T, resolver?: (...args: Parameters<T>) => any, cache_duration_ms?: number | false, cache_max_size?: number): T;
619
629
  export { memoize, memoize as default };
@@ -665,12 +675,6 @@ declare module "deep/index" {
665
675
  export { deepSet as set } from "deep/set";
666
676
  export { deepSet } from "deep/set";
667
677
  }
668
- declare module "hash/fnv1A" {
669
- export const FNV_32 = 2166136261;
670
- export const FNV_64 = 1099511628211;
671
- function fnv1A(data: unknown, offset?: number): number;
672
- export { fnv1A, fnv1A as default };
673
- }
674
678
  declare module "hash/guid" {
675
679
  function guid(): string;
676
680
  export { guid, guid as default };