danholibraryjs 1.10.0 → 2.0.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 (158) hide show
  1. package/dist/Classes/DanhoLogger.d.ts +23 -0
  2. package/dist/Classes/DanhoLogger.js +65 -0
  3. package/dist/Classes/Events/EventEmitter.d.ts +1 -1
  4. package/dist/Classes/Events/EventEmitter.js +1 -1
  5. package/dist/Classes/Time/Date.d.ts +1 -0
  6. package/dist/Classes/Time/Date.js +4 -1
  7. package/dist/Classes/Time/Time.d.ts +5 -4
  8. package/dist/Classes/Time/Time.js +7 -4
  9. package/dist/Classes/index.d.ts +1 -0
  10. package/dist/Classes/index.js +1 -0
  11. package/dist/Classes/store.d.ts +5 -9
  12. package/dist/Extensions/Array/array.extension.d.ts +42 -0
  13. package/dist/Extensions/Array/array.extension.js +57 -0
  14. package/dist/Extensions/Array/crud.extension.d.ts +24 -0
  15. package/dist/Extensions/Array/crud.extension.js +28 -0
  16. package/dist/Extensions/Array/index.d.ts +20 -0
  17. package/dist/Extensions/Array/index.js +40 -0
  18. package/dist/Extensions/Array/loop.extension.d.ts +18 -0
  19. package/dist/Extensions/Array/loop.extension.js +23 -0
  20. package/dist/Extensions/Array/random.extension.d.ts +23 -0
  21. package/dist/Extensions/Array/random.extension.js +35 -0
  22. package/dist/Extensions/Array/sort.extension.d.ts +27 -0
  23. package/dist/Extensions/Array/sort.extension.js +31 -0
  24. package/dist/Extensions/Array/string.extension.d.ts +13 -0
  25. package/dist/Extensions/Array/string.extension.js +14 -0
  26. package/dist/Extensions/Array.d.ts +17 -3
  27. package/dist/Extensions/Array.js +0 -12
  28. package/dist/Extensions/Function.d.ts +17 -2
  29. package/dist/Extensions/Function.js +15 -2
  30. package/dist/Extensions/Number.d.ts +13 -0
  31. package/dist/Extensions/Number.js +40 -0
  32. package/dist/Extensions/Object/arrays.extension.d.ts +17 -0
  33. package/dist/Extensions/Object/arrays.extension.js +13 -0
  34. package/dist/Extensions/Object/booleans.extension.d.ts +18 -0
  35. package/dist/Extensions/Object/booleans.extension.js +37 -0
  36. package/dist/Extensions/Object/extracts.extension.d.ts +38 -0
  37. package/dist/Extensions/Object/extracts.extension.js +72 -0
  38. package/dist/Extensions/Object/index.d.ts +8 -47
  39. package/dist/Extensions/Object/index.js +31 -33
  40. package/dist/Extensions/Object/properties.extension.d.ts +6 -0
  41. package/dist/Extensions/Object/properties.extension.js +4 -0
  42. package/dist/Extensions/Object/properties.js +1 -2
  43. package/dist/Extensions/String/case.extension.d.ts +12 -0
  44. package/dist/Extensions/String/case.extension.js +55 -0
  45. package/dist/Extensions/String/index.d.ts +4 -0
  46. package/dist/Extensions/String/index.js +30 -0
  47. package/dist/Extensions/index.d.ts +1 -12
  48. package/dist/Extensions/index.js +1 -9
  49. package/dist/Types/Able.d.ts +16 -0
  50. package/dist/Types/Able.js +2 -0
  51. package/dist/Types/Array.d.ts +6 -0
  52. package/dist/Types/Array.js +2 -0
  53. package/dist/Types/C#.d.ts +8 -0
  54. package/dist/Types/C#.js +2 -0
  55. package/dist/Types/Date.d.ts +1 -1
  56. package/dist/Types/Events.d.ts +2 -2
  57. package/dist/Types/Function.d.ts +5 -0
  58. package/dist/Types/Function.js +2 -0
  59. package/dist/Types/Object.d.ts +4 -0
  60. package/dist/Types/Object.js +2 -0
  61. package/dist/Types/PropertiesWith.d.ts +21 -0
  62. package/dist/Types/String.d.ts +1 -0
  63. package/dist/Types/String.js +2 -0
  64. package/dist/Types/TransformTypes.d.ts +9 -0
  65. package/dist/Types/index.d.ts +6 -28
  66. package/dist/Types/index.js +6 -0
  67. package/dist/Utils/ApiUtil/ApiTypes.d.ts +15 -0
  68. package/dist/Utils/ApiUtil/ApiTypes.js +15 -0
  69. package/dist/Utils/ApiUtil/RequestUtil.d.ts +19 -0
  70. package/dist/Utils/ApiUtil/RequestUtil.js +73 -0
  71. package/dist/Utils/ApiUtil/index.d.ts +20 -0
  72. package/dist/Utils/ApiUtil/index.js +33 -0
  73. package/dist/Utils/ApiUtils/ApiTypes.d.ts +15 -0
  74. package/dist/Utils/ApiUtils/ApiTypes.js +15 -0
  75. package/dist/Utils/ApiUtils/RequestUtil.d.ts +19 -0
  76. package/dist/Utils/ApiUtils/RequestUtil.js +73 -0
  77. package/dist/Utils/ApiUtils/index.d.ts +20 -0
  78. package/dist/Utils/ApiUtils/index.js +33 -0
  79. package/dist/Utils/ColorUtils.d.ts +11 -0
  80. package/dist/Utils/ColorUtils.js +93 -0
  81. package/dist/Utils/FormUtil.d.ts +6 -0
  82. package/dist/Utils/FormUtil.js +35 -0
  83. package/dist/Utils/FormUtils.d.ts +6 -0
  84. package/dist/Utils/FormUtils.js +35 -0
  85. package/dist/Utils/NumberUtils.d.ts +1 -0
  86. package/dist/Utils/NumberUtils.js +7 -0
  87. package/dist/Utils/PatcherUtils.d.ts +6 -0
  88. package/dist/Utils/PatcherUtils.js +80 -0
  89. package/dist/Utils/StringUtils.d.ts +3 -0
  90. package/dist/Utils/StringUtils.js +47 -0
  91. package/dist/Utils/TimeUtils/debounce.util.d.ts +22 -0
  92. package/dist/Utils/TimeUtils/debounce.util.js +78 -0
  93. package/dist/Utils/TimeUtils/functions.util.d.ts +4 -0
  94. package/dist/Utils/TimeUtils/functions.util.js +21 -0
  95. package/dist/Utils/TimeUtils/index.d.ts +15 -0
  96. package/dist/Utils/TimeUtils/index.js +34 -0
  97. package/dist/Utils/TimeUtils/throttle.util.d.ts +15 -0
  98. package/dist/Utils/TimeUtils/throttle.util.js +43 -0
  99. package/dist/Utils/index.d.ts +7 -0
  100. package/dist/Utils/index.js +23 -0
  101. package/package.json +4 -2
  102. package/src/Classes/DanhoLogger.ts +78 -0
  103. package/src/Classes/Events/Event.ts +96 -96
  104. package/src/Classes/Events/EventCollection.ts +90 -90
  105. package/src/Classes/Events/EventEmitter.ts +68 -68
  106. package/src/Classes/Time/Date.ts +219 -216
  107. package/src/Classes/Time/Time.ts +109 -104
  108. package/src/Classes/Time/TimeSpan.ts +171 -171
  109. package/src/Classes/index.ts +1 -0
  110. package/src/Classes/store.ts +22 -22
  111. package/src/Extensions/Array/array.extension.ts +103 -0
  112. package/src/Extensions/Array/crud.extension.ts +46 -0
  113. package/src/Extensions/Array/index.ts +15 -0
  114. package/src/Extensions/Array/loop.extension.ts +38 -0
  115. package/src/Extensions/Array/random.extension.ts +56 -0
  116. package/src/Extensions/Array/sort.extension.ts +52 -0
  117. package/src/Extensions/Array/string.extension.ts +22 -0
  118. package/src/Extensions/Document.ts +39 -39
  119. package/src/Extensions/Function.ts +37 -10
  120. package/src/Extensions/Map.ts +56 -56
  121. package/src/Extensions/Number.ts +50 -0
  122. package/src/Extensions/Object/arrays.extension.ts +27 -0
  123. package/src/Extensions/Object/booleans.extension.ts +46 -0
  124. package/src/Extensions/Object/extracts.extension.ts +102 -0
  125. package/src/Extensions/Object/index.ts +9 -80
  126. package/src/Extensions/Object/properties.extension.ts +11 -0
  127. package/src/Extensions/Object/properties.ts +35 -36
  128. package/src/Extensions/String/case.extension.ts +95 -0
  129. package/src/Extensions/String/index.ts +5 -0
  130. package/src/Extensions/index.ts +2 -20
  131. package/src/Interfaces/ElementOptions.ts +7 -7
  132. package/src/Interfaces/IReplacement.ts +2 -2
  133. package/src/Types/Able.ts +22 -0
  134. package/src/Types/Array.ts +7 -0
  135. package/src/Types/C#.ts +9 -0
  136. package/src/Types/Date.ts +1 -1
  137. package/src/Types/Events.ts +12 -12
  138. package/src/Types/Function.ts +10 -0
  139. package/src/Types/Object.ts +4 -0
  140. package/src/Types/PropertiesWith.ts +35 -4
  141. package/src/Types/String.ts +1 -0
  142. package/src/Types/TransformTypes.ts +23 -5
  143. package/src/Types/index.ts +7 -41
  144. package/src/Utils/ApiUtils/ApiTypes.ts +43 -0
  145. package/src/Utils/ApiUtils/RequestUtil.ts +87 -0
  146. package/src/Utils/ApiUtils/index.ts +39 -0
  147. package/src/Utils/ColorUtils.ts +102 -0
  148. package/src/Utils/FormUtils.ts +33 -0
  149. package/src/Utils/NumberUtils.ts +3 -0
  150. package/src/Utils/PatcherUtils.ts +111 -0
  151. package/src/Utils/StringUtils.ts +44 -0
  152. package/src/Utils/TimeUtils/debounce.util.ts +85 -0
  153. package/src/Utils/TimeUtils/functions.util.ts +18 -0
  154. package/src/Utils/TimeUtils/index.ts +9 -0
  155. package/src/Utils/TimeUtils/throttle.util.ts +44 -0
  156. package/src/Utils/index.ts +8 -0
  157. package/src/Extensions/Array.ts +0 -95
  158. package/src/Extensions/String.ts +0 -54
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ColorUtils = exports.generateRandomColor = exports.convert = void 0;
4
+ function convert(value, fromOrTo, to) {
5
+ const from = typeof value === 'string' ? fromOrTo : 'rgb';
6
+ const target = typeof value === 'string' ? to : fromOrTo;
7
+ if (from === target)
8
+ return value;
9
+ switch (from) {
10
+ case 'hsl': {
11
+ switch (target) {
12
+ case 'hex': return hslToHex(value);
13
+ case 'rgb': return hexToRgb(hslToHex(value));
14
+ }
15
+ }
16
+ case 'rgb': {
17
+ switch (target) {
18
+ case 'hex': return hslToHex(rgbToHsl(value));
19
+ case 'hsl': return rgbToHsl(value);
20
+ }
21
+ }
22
+ case 'hex': {
23
+ switch (target) {
24
+ case 'rgb': return hexToRgb(value);
25
+ case 'hsl': return rgbToHsl(hexToRgb(value));
26
+ }
27
+ }
28
+ default: return value;
29
+ }
30
+ }
31
+ exports.convert = convert;
32
+ function generateRandomColor() {
33
+ return `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`;
34
+ }
35
+ exports.generateRandomColor = generateRandomColor;
36
+ exports.ColorUtils = {
37
+ convert,
38
+ generateRandomColor
39
+ };
40
+ exports.default = exports.ColorUtils;
41
+ function hslToHex(value) {
42
+ if (!value)
43
+ return "#000000";
44
+ const match = value.match(/hsl\((\d+), (\d+)%, (\d+)%\)/);
45
+ if (!match)
46
+ return value;
47
+ let [_, h, s, l] = match.map(Number);
48
+ l /= 100;
49
+ const a = s * Math.min(l, 1 - l) / 100;
50
+ const format = (value) => {
51
+ const k = (value + h / 30) % 12;
52
+ const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
53
+ return Math.round(255 * color).toString(16).padStart(2, '0');
54
+ };
55
+ return `#${format(0)}${format(8)}${format(4)}`;
56
+ }
57
+ function hexToRgb(hex) {
58
+ if (!hex)
59
+ return [0, 0, 0];
60
+ const match = hex.match(/[a-f0-9]{6}|[a-f0-9]{3}/i);
61
+ if (!match)
62
+ return [0, 0, 0];
63
+ let colorString = match[0];
64
+ if (match[0].length === 3)
65
+ colorString = colorString.split('').map(char => char + char).join('');
66
+ const integer = parseInt(colorString, 16);
67
+ const r = (integer >> 16) & 0xFF;
68
+ const g = (integer >> 8) & 0xFF;
69
+ const b = integer & 0xFF;
70
+ return [r, g, b];
71
+ }
72
+ function rgbToHsl(rgb) {
73
+ let [r, g, b] = rgb.map(x => x / 255);
74
+ const max = Math.max(r, g, b), min = Math.min(r, g, b);
75
+ let h = 0, s = 0, l = (max + min) / 2;
76
+ if (max !== min) {
77
+ const d = max - min;
78
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
79
+ switch (max) {
80
+ case r:
81
+ h = (g - b) / d + (g < b ? 6 : 0);
82
+ break;
83
+ case g:
84
+ h = (b - r) / d + 2;
85
+ break;
86
+ case b:
87
+ h = (r - g) / d + 4;
88
+ break;
89
+ }
90
+ h /= 6;
91
+ }
92
+ return `hsl(${Math.round(h * 360)}, ${Math.round(s * 100)}%, ${Math.round(l * 100)}%)`;
93
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Serialize a form into an object
3
+ * @param form The form to serialize
4
+ * @returns An object of type T from the form
5
+ */
6
+ export declare function serializeForm<T extends object>(form: HTMLFormElement, log?: boolean): T;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeForm = void 0;
4
+ /**
5
+ * Serialize a form into an object
6
+ * @param form The form to serialize
7
+ * @returns An object of type T from the form
8
+ */
9
+ function serializeForm(form, log = false) {
10
+ const children = Array.from(form.children);
11
+ const formData = children.reduce((acc, child) => {
12
+ // Find inputs and selects
13
+ const elements = Array.from(child.querySelectorAll('input, select'));
14
+ if (['INPUT', 'SELECT'].includes(child.tagName))
15
+ elements.push(child);
16
+ for (const element of Array.from(elements)) {
17
+ if (element.type === 'submit')
18
+ continue; // Ignore submit buttons
19
+ const name = element.getAttribute('name');
20
+ if (!name) {
21
+ console.error('[DanhoLibraryRJS] [FormUtil]: name attribute is required', { element });
22
+ throw new Error('name attribute is required');
23
+ }
24
+ const value = element.value;
25
+ if (value === null)
26
+ console.warn(`${name}.value returned null`, { element });
27
+ if (log)
28
+ console.log(`[DanhoLibraryRJS] [FormUtil]`, { name, value });
29
+ acc[name] = /^\d$/.test(value) ? parseInt(value) : value;
30
+ }
31
+ return acc;
32
+ }, {});
33
+ return formData;
34
+ }
35
+ exports.serializeForm = serializeForm;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Serialize a form into an object
3
+ * @param form The form to serialize
4
+ * @returns An object of type T from the form
5
+ */
6
+ export declare function serializeForm<T extends object>(form: HTMLFormElement, log?: boolean): T;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeForm = void 0;
4
+ /**
5
+ * Serialize a form into an object
6
+ * @param form The form to serialize
7
+ * @returns An object of type T from the form
8
+ */
9
+ function serializeForm(form, log = false) {
10
+ const children = Array.from(form.children);
11
+ const formData = children.reduce((acc, child) => {
12
+ // Find inputs and selects
13
+ const elements = Array.from(child.querySelectorAll('input, select'));
14
+ if (['INPUT', 'SELECT'].includes(child.tagName))
15
+ elements.push(child);
16
+ for (const element of Array.from(elements)) {
17
+ if (element.type === 'submit')
18
+ continue; // Ignore submit buttons
19
+ const name = element.getAttribute('name');
20
+ if (!name) {
21
+ console.error('[DanhoLibraryJS] [FormUtil]: name attribute is required', { element });
22
+ throw new Error('name attribute is required');
23
+ }
24
+ const value = element.value;
25
+ if (value === null)
26
+ console.warn(`${name}.value returned null`, { element });
27
+ if (log)
28
+ console.log(`[DanhoLibraryJS] [FormUtil]`, { name, value });
29
+ acc[name] = /^\d$/.test(value) ? parseInt(value) : value;
30
+ }
31
+ return acc;
32
+ }, {});
33
+ return formData;
34
+ }
35
+ exports.serializeForm = serializeForm;
@@ -0,0 +1 @@
1
+ export declare function between(min: number, max: number): number;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.between = void 0;
4
+ function between(min, max) {
5
+ return Math.floor(Math.random() * (max - min + 1)) + min;
6
+ }
7
+ exports.between = between;
@@ -0,0 +1,6 @@
1
+ type PatchEvent = 'before' | 'instead' | 'after';
2
+ type PatcherReplacement<TTarget extends object, TProperty extends keyof TTarget, TPatchEvent extends PatchEvent> = (TPatchEvent extends 'before' ? TTarget[TProperty] extends (...args: infer TArgs) => any ? (...args: TArgs) => TArgs | void : (current: TTarget[TProperty], update: TTarget[TProperty]) => TTarget[TProperty] | void : TPatchEvent extends 'instead' ? TTarget[TProperty] extends (...args: infer TArgs) => infer TReturn ? (...args: TArgs) => TReturn : (current: TTarget[TProperty], update: TTarget[TProperty]) => TTarget[TProperty] : TTarget[TProperty] extends (...args: infer TArgs) => any ? (...args: TArgs) => void : (previous: TTarget[TProperty], updated: TTarget[TProperty]) => void);
3
+ export declare function patch<TTarget extends object, TProperty extends keyof TTarget, TPatchEvent extends PatchEvent, TPatchReplacement extends PatcherReplacement<TTarget, TProperty, TPatchEvent>>(target: TTarget, property: TProperty, event: TPatchEvent, replacement: TPatchReplacement): (() => TTarget[TProperty] & Function) | (() => TTarget) | undefined;
4
+ export declare function unpatch<TTarget extends object, TProperty extends keyof TTarget>(target: TTarget, property: TProperty): void;
5
+ export declare function unpatchAll(): void;
6
+ export {};
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unpatchAll = exports.unpatch = exports.patch = void 0;
4
+ const PATCHES = new Map();
5
+ function patch(target, property, event, replacement) {
6
+ if (!target || !property || !replacement)
7
+ return;
8
+ else if (!(property in target))
9
+ throw Error(`Property "${String(property)}" does not exist on target`);
10
+ const original = target[property];
11
+ if (!PATCHES.has(target))
12
+ PATCHES.set(target, new Map());
13
+ const targetPatches = PATCHES.get(target);
14
+ if (!targetPatches.has(property))
15
+ targetPatches.set(property, []);
16
+ const unpatch = (() => {
17
+ if (typeof original === 'function') {
18
+ switch (event) {
19
+ case 'before': {
20
+ target[property] = function (...args) {
21
+ const updatedArgs = replacement.apply(target, args);
22
+ return original.apply(target, updatedArgs || args);
23
+ };
24
+ break;
25
+ }
26
+ case 'instead': {
27
+ target[property] = function (...args) {
28
+ return replacement.apply(target, args);
29
+ };
30
+ break;
31
+ }
32
+ case 'after': {
33
+ target[property] = function (...args) {
34
+ const result = original.apply(target, args);
35
+ replacement.apply(target, args);
36
+ return result;
37
+ };
38
+ break;
39
+ }
40
+ }
41
+ return () => target[property] = original;
42
+ }
43
+ else {
44
+ let currentValue = target[property];
45
+ const propertyDescriptor = Object.getOwnPropertyDescriptor(target, property);
46
+ const valuePatch = (update) => {
47
+ update ??= currentValue;
48
+ const result = event !== 'after' ? replacement(currentValue, update) || currentValue : currentValue;
49
+ if (event === 'after')
50
+ replacement(currentValue, update);
51
+ return result;
52
+ };
53
+ Object.defineProperty(target, property, {
54
+ get: () => valuePatch(),
55
+ set: valuePatch,
56
+ });
57
+ return () => Object.defineProperty(target, property, propertyDescriptor);
58
+ }
59
+ })();
60
+ targetPatches.get(property).push({ event, replacement, original, unpatch });
61
+ return unpatch;
62
+ }
63
+ exports.patch = patch;
64
+ function unpatch(target, property) {
65
+ const targetPatches = PATCHES.get(target);
66
+ if (!targetPatches?.has(property))
67
+ return;
68
+ targetPatches.get(property).forEach(patch => patch.unpatch());
69
+ targetPatches.delete(property);
70
+ }
71
+ exports.unpatch = unpatch;
72
+ function unpatchAll() {
73
+ PATCHES.forEach(properties => {
74
+ properties.forEach(patches => {
75
+ patches.forEach(patch => patch.unpatch());
76
+ });
77
+ });
78
+ PATCHES.clear();
79
+ }
80
+ exports.unpatchAll = unpatchAll;
@@ -0,0 +1,3 @@
1
+ export declare function classNames(...args: Array<any>): string;
2
+ export declare function randomId(length?: number): string;
3
+ export declare function pluralize(countable: number | ArrayLike<any> | Map<any, any>, singular: string, plural?: string): string;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pluralize = exports.randomId = exports.classNames = void 0;
4
+ function classNames(...args) {
5
+ return args.reduce((acc, arg) => {
6
+ if (!arg)
7
+ return acc;
8
+ switch (typeof arg) {
9
+ case 'string': {
10
+ acc += arg;
11
+ break;
12
+ }
13
+ case 'object': {
14
+ if (Array.isArray(arg)) {
15
+ acc += classNames(...arg);
16
+ break;
17
+ }
18
+ else {
19
+ acc += Object.entries(arg).reduce((objAcc, [key, value]) => objAcc + (value ? key : ''), '');
20
+ break;
21
+ }
22
+ }
23
+ default: {
24
+ acc += `${arg}`;
25
+ break;
26
+ }
27
+ }
28
+ return acc + ' ';
29
+ }, '').trim();
30
+ }
31
+ exports.classNames = classNames;
32
+ function randomId(length = 16) {
33
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
34
+ return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');
35
+ }
36
+ exports.randomId = randomId;
37
+ function pluralize(countable, singular, plural) {
38
+ const count = (typeof countable === 'number' ? countable
39
+ : countable instanceof Map ? countable.size
40
+ : countable.length);
41
+ if (count === 1)
42
+ return singular;
43
+ if (plural)
44
+ return plural;
45
+ return `${singular}s`;
46
+ }
47
+ exports.pluralize = pluralize;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Delay running the callback until delay milliseconds have passed without the function being called again.
3
+ * If the function is called again before the delay has passed, the timer resets.
4
+ *
5
+ * @param debounceId A unique identifier for the debounce instance. Call with the same ID to reset the timer.
6
+ * @param callback The callback to run after the delay has passed without the function being called again
7
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
8
+ * @param signal Optional AbortSignal to cancel the debounced operation
9
+ * @returns A promise that resolves to the result of the callback when it is eventually called, or rejects if aborted
10
+ */
11
+ export declare function debounce<T>(debounceId: string, callback: () => T, delay: number, signal?: AbortSignal): Promise<T>;
12
+ /**
13
+ * Wrap a callback in a debounce, allowing it to be called multiple times, but only run after the delay has passed without it being called again
14
+ * @param callback The callback to run after the delay has passed without the function being called again
15
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
16
+ * @returns An object containing the debounced function, an AbortController to cancel operations, and the abort method for convenience
17
+ */
18
+ export declare function wrapInDebounce<T>(callback: (...args: T[]) => void, delay: number): {
19
+ debounced: (...args: T[]) => Promise<void>;
20
+ controller: AbortController;
21
+ abort: () => void;
22
+ };
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapInDebounce = exports.debounce = void 0;
4
+ const DebounceManager = {
5
+ timers: new Map(),
6
+ abortListeners: new Map(),
7
+ };
8
+ /**
9
+ * Delay running the callback until delay milliseconds have passed without the function being called again.
10
+ * If the function is called again before the delay has passed, the timer resets.
11
+ *
12
+ * @param debounceId A unique identifier for the debounce instance. Call with the same ID to reset the timer.
13
+ * @param callback The callback to run after the delay has passed without the function being called again
14
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
15
+ * @param signal Optional AbortSignal to cancel the debounced operation
16
+ * @returns A promise that resolves to the result of the callback when it is eventually called, or rejects if aborted
17
+ */
18
+ function debounce(debounceId, callback, delay, signal) {
19
+ const { timers, abortListeners } = DebounceManager;
20
+ // Clean up existing debounce with the same ID
21
+ if (timers.has(debounceId)) {
22
+ clearTimeout(timers.get(debounceId));
23
+ timers.delete(debounceId);
24
+ const existingListener = abortListeners.get(debounceId);
25
+ if (existingListener && signal) {
26
+ signal.removeEventListener('abort', existingListener);
27
+ }
28
+ abortListeners.delete(debounceId);
29
+ }
30
+ const promise = new Promise((resolve, reject) => {
31
+ // Check if already aborted
32
+ if (signal?.aborted) {
33
+ const error = new DOMException('Debounce aborted', 'AbortError');
34
+ reject(error);
35
+ return;
36
+ }
37
+ const timeoutId = setTimeout(() => {
38
+ timers.delete(debounceId);
39
+ abortListeners.delete(debounceId);
40
+ try {
41
+ resolve(callback());
42
+ }
43
+ catch (error) {
44
+ reject(error);
45
+ }
46
+ }, delay);
47
+ timers.set(debounceId, timeoutId);
48
+ // Set up abort listener
49
+ if (signal) {
50
+ const abortListener = () => {
51
+ clearTimeout(timeoutId);
52
+ timers.delete(debounceId);
53
+ abortListeners.delete(debounceId);
54
+ const error = new DOMException('Debounce aborted', 'AbortError');
55
+ reject(error);
56
+ };
57
+ signal.addEventListener('abort', abortListener, { once: true });
58
+ abortListeners.set(debounceId, abortListener);
59
+ }
60
+ });
61
+ return promise;
62
+ }
63
+ exports.debounce = debounce;
64
+ /**
65
+ * Wrap a callback in a debounce, allowing it to be called multiple times, but only run after the delay has passed without it being called again
66
+ * @param callback The callback to run after the delay has passed without the function being called again
67
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
68
+ * @returns An object containing the debounced function, an AbortController to cancel operations, and the abort method for convenience
69
+ */
70
+ function wrapInDebounce(callback, delay) {
71
+ const controller = new AbortController();
72
+ return {
73
+ debounced: (...args) => debounce(Symbol().toString(), () => callback(...args), delay, controller.signal),
74
+ controller,
75
+ abort: () => controller.abort(),
76
+ };
77
+ }
78
+ exports.wrapInDebounce = wrapInDebounce;
@@ -0,0 +1,4 @@
1
+ export declare function wait<T>(time: number): Promise<void>;
2
+ export declare function wait<T>(callback: (...args: any[]) => T, time: number): Promise<T>;
3
+ export declare function getUnixTime(date: Date | string): number;
4
+ export declare function getUnixTime(timestamp: number): number;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUnixTime = exports.wait = void 0;
4
+ function wait(callbackOrTime, time) {
5
+ const callback = typeof callbackOrTime === 'function' ? callbackOrTime : (() => undefined);
6
+ time ??= callbackOrTime;
7
+ return new Promise((resolve, reject) => {
8
+ try {
9
+ setTimeout(() => resolve(callback()), time);
10
+ }
11
+ catch (err) {
12
+ reject(err);
13
+ }
14
+ });
15
+ }
16
+ exports.wait = wait;
17
+ function getUnixTime(arg) {
18
+ const timestamp = typeof arg === 'number' ? arg : new Date(arg).getTime();
19
+ return Math.floor(timestamp / 1000);
20
+ }
21
+ exports.getUnixTime = getUnixTime;
@@ -0,0 +1,15 @@
1
+ export declare const TimeUtils: {
2
+ throttle<T>(throttleId: string, callback: () => T, cooldown: number): T;
3
+ wrapInThrottle<T_1>(callback: (...args: T_1[]) => void, cooldown: number): (...args: T_1[]) => void;
4
+ isThrottleOnCooldown(throttleId: string): boolean;
5
+ debounce<T_2>(debounceId: string, callback: () => T_2, delay: number, signal?: AbortSignal | undefined): Promise<T_2>;
6
+ wrapInDebounce<T_3>(callback: (...args: T_3[]) => void, delay: number): {
7
+ debounced: (...args: T_3[]) => Promise<void>;
8
+ controller: AbortController;
9
+ abort: () => void;
10
+ };
11
+ wait<T_4>(time: number): Promise<void>;
12
+ wait<T_5>(callback: (...args: any[]) => T_5, time: number): Promise<T_5>;
13
+ getUnixTime(date: string | Date): number;
14
+ getUnixTime(timestamp: number): number;
15
+ };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.TimeUtils = void 0;
27
+ const Debounce = __importStar(require("./debounce.util"));
28
+ const Functions = __importStar(require("./functions.util"));
29
+ const Throttle = __importStar(require("./throttle.util"));
30
+ exports.TimeUtils = {
31
+ ...Functions,
32
+ ...Debounce,
33
+ ...Throttle,
34
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Allow callback to be run once every cooldown period, regardless of calls
3
+ * @param callback The callback to run
4
+ * @param cooldown The delay to wait before allowing the callback to be called again
5
+ * @returns A throttled version of the callback
6
+ */
7
+ export declare function throttle<T>(throttleId: string, callback: () => T, cooldown: number): T;
8
+ /**
9
+ * Wrap a callback in a throttle, allowing it to be called multiple times, but only run once every cooldown period
10
+ * @param callback The callback to run
11
+ * @param cooldown The delay to wait before allowing the callback to be called again
12
+ * @returns A throttled version of the callback that can be called multiple times, but will only run once every cooldown period
13
+ */
14
+ export declare function wrapInThrottle<T>(callback: (...args: T[]) => void, cooldown: number): (...args: T[]) => void;
15
+ export declare function isThrottleOnCooldown(throttleId: string): boolean;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isThrottleOnCooldown = exports.wrapInThrottle = exports.throttle = void 0;
4
+ const ThrottleManager = {
5
+ cooldowns: new Map(),
6
+ results: new Map(),
7
+ };
8
+ /**
9
+ * Allow callback to be run once every cooldown period, regardless of calls
10
+ * @param callback The callback to run
11
+ * @param cooldown The delay to wait before allowing the callback to be called again
12
+ * @returns A throttled version of the callback
13
+ */
14
+ function throttle(throttleId, callback, cooldown) {
15
+ const { cooldowns, results } = ThrottleManager;
16
+ const isCooldown = cooldowns.get(throttleId) ?? false;
17
+ if (isCooldown)
18
+ return results.get(throttleId);
19
+ const result = callback();
20
+ setTimeout(() => {
21
+ cooldowns.delete(throttleId);
22
+ results.delete(throttleId);
23
+ }, cooldown);
24
+ cooldowns.set(throttleId, true);
25
+ results.set(throttleId, result);
26
+ return result;
27
+ }
28
+ exports.throttle = throttle;
29
+ /**
30
+ * Wrap a callback in a throttle, allowing it to be called multiple times, but only run once every cooldown period
31
+ * @param callback The callback to run
32
+ * @param cooldown The delay to wait before allowing the callback to be called again
33
+ * @returns A throttled version of the callback that can be called multiple times, but will only run once every cooldown period
34
+ */
35
+ function wrapInThrottle(callback, cooldown) {
36
+ const throttleId = Symbol().toString();
37
+ return (...args) => throttle(throttleId, () => callback(...args), cooldown);
38
+ }
39
+ exports.wrapInThrottle = wrapInThrottle;
40
+ function isThrottleOnCooldown(throttleId) {
41
+ return ThrottleManager.cooldowns.get(throttleId) ?? false;
42
+ }
43
+ exports.isThrottleOnCooldown = isThrottleOnCooldown;
@@ -0,0 +1,7 @@
1
+ export * from './ApiUtils';
2
+ export * from './TimeUtils';
3
+ export * from './ColorUtils';
4
+ export * from './FormUtils';
5
+ export * from './NumberUtils';
6
+ export * from './PatcherUtils';
7
+ export * from './StringUtils';
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ApiUtils"), exports);
18
+ __exportStar(require("./TimeUtils"), exports);
19
+ __exportStar(require("./ColorUtils"), exports);
20
+ __exportStar(require("./FormUtils"), exports);
21
+ __exportStar(require("./NumberUtils"), exports);
22
+ __exportStar(require("./PatcherUtils"), exports);
23
+ __exportStar(require("./StringUtils"), exports);
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "danholibraryjs",
3
-
4
- "version": "1.10.0",
3
+ "publishConfig": {
4
+ "registry": "https://registry.npmjs.org/"
5
+ },
6
+ "version": "2.0.0",
5
7
  "description": "Library for Javascript.",
6
8
  "main": "dist/index.js",
7
9
  "types": "dist/index.d.ts",