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,111 @@
1
+ type PatchEvent = 'before' | 'instead' | 'after';
2
+ type PatcherReplacement<TTarget extends object, TProperty extends keyof TTarget, TPatchEvent extends PatchEvent> = (
3
+ TPatchEvent extends 'before'
4
+ ? TTarget[TProperty] extends (...args: infer TArgs) => any
5
+ ? (...args: TArgs) => TArgs | void
6
+ : (current: TTarget[TProperty], update: TTarget[TProperty]) => TTarget[TProperty] | void
7
+ : TPatchEvent extends 'instead'
8
+ ? TTarget[TProperty] extends (...args: infer TArgs) => infer TReturn
9
+ ? (...args: TArgs) => TReturn
10
+ : (current: TTarget[TProperty], update: TTarget[TProperty]) => TTarget[TProperty]
11
+ : TTarget[TProperty] extends (...args: infer TArgs) => any
12
+ ? (...args: TArgs) => void
13
+ : (previous: TTarget[TProperty], updated: TTarget[TProperty]) => void
14
+ )
15
+
16
+ type Patch = {
17
+ event: PatchEvent;
18
+ replacement: any;
19
+ original: any;
20
+ unpatch: () => void;
21
+ }
22
+ const PATCHES = new Map<object, Map<string | symbol | number, Array<Patch>>>();
23
+
24
+ export function patch<
25
+ TTarget extends object,
26
+ TProperty extends keyof TTarget,
27
+ TPatchEvent extends PatchEvent,
28
+ TPatchReplacement extends PatcherReplacement<TTarget, TProperty, TPatchEvent>
29
+ >(target: TTarget, property: TProperty, event: TPatchEvent, replacement: TPatchReplacement) {
30
+ if (!target || !property || !replacement) return;
31
+ else if (!(property in target)) throw Error(`Property "${String(property)}" does not exist on target`);
32
+
33
+ const original = target[property];
34
+ if (!PATCHES.has(target)) PATCHES.set(target, new Map());
35
+ const targetPatches = PATCHES.get(target)!;
36
+
37
+ if (!targetPatches.has(property)) targetPatches.set(property, []);
38
+
39
+ const unpatch = (() => {
40
+ if (typeof original === 'function') {
41
+ switch (event) {
42
+ case 'before': {
43
+ target[property] = function (...args: any[]) {
44
+ const updatedArgs = (replacement as any).apply(target, args);
45
+ return (original as any).apply(target, updatedArgs || args);
46
+ } as TTarget[TProperty];
47
+ break;
48
+ }
49
+ case 'instead': {
50
+ target[property] = function (...args: any[]) {
51
+ return (replacement as any).apply(target, args);
52
+ } as TTarget[TProperty];
53
+ break;
54
+ }
55
+ case 'after': {
56
+ target[property] = function (...args: any[]) {
57
+ const result = (original as any).apply(target, args);
58
+ (replacement as any).apply(target, args);
59
+ return result;
60
+ } as TTarget[TProperty];
61
+ break;
62
+ }
63
+ }
64
+
65
+ return () => target[property] = original;
66
+ } else {
67
+ let currentValue = target[property];
68
+ const propertyDescriptor = Object.getOwnPropertyDescriptor(target, property);
69
+
70
+ const valuePatch = (update?: TTarget[TProperty]) => {
71
+ update ??= currentValue;
72
+ const result = event !== 'after' ? (replacement as any)(currentValue, update) || currentValue : currentValue;
73
+
74
+ if (event === 'after') (replacement as any)(currentValue, update);
75
+
76
+ return result;
77
+ }
78
+
79
+ Object.defineProperty(target, property, {
80
+ get: () => valuePatch(),
81
+ set: valuePatch,
82
+ });
83
+
84
+ return () => Object.defineProperty(target, property, propertyDescriptor!);
85
+ }
86
+ })();
87
+
88
+ targetPatches.get(property)!.push({ event, replacement, original, unpatch });
89
+ return unpatch;
90
+ }
91
+
92
+ export function unpatch<
93
+ TTarget extends object,
94
+ TProperty extends keyof TTarget
95
+ >(target: TTarget, property: TProperty) {
96
+ const targetPatches = PATCHES.get(target);
97
+ if (!targetPatches?.has(property)) return;
98
+
99
+ targetPatches.get(property)!.forEach(patch => patch.unpatch());
100
+ targetPatches.delete(property);
101
+ }
102
+
103
+ export function unpatchAll() {
104
+ PATCHES.forEach(properties => {
105
+ properties.forEach(patches => {
106
+ patches.forEach(patch => patch.unpatch());
107
+ });
108
+ });
109
+
110
+ PATCHES.clear();
111
+ }
@@ -0,0 +1,44 @@
1
+ export function classNames(...args: Array<any>): string {
2
+ return args.reduce((acc, arg) => {
3
+ if (!arg) return acc;
4
+
5
+ switch (typeof arg) {
6
+ case 'string': {
7
+ acc += arg;
8
+ break;
9
+ }
10
+ case 'object': {
11
+ if (Array.isArray(arg)) {
12
+ acc += classNames(...arg);
13
+ break;
14
+ } else {
15
+ acc += Object.entries(arg).reduce((objAcc, [key, value]) => objAcc + (value ? key : ''), '');
16
+ break;
17
+ }
18
+ }
19
+ default: {
20
+ acc += `${arg}`;
21
+ break;
22
+ }
23
+ }
24
+
25
+ return acc + ' ';
26
+ }, '').trim();
27
+ }
28
+
29
+ export function randomId(length = 16): string {
30
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
31
+ return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');
32
+ }
33
+
34
+ export function pluralize(countable: number | ArrayLike<any> | Map<any, any>, singular: string, plural?: string): string {
35
+ const count = (
36
+ typeof countable === 'number' ? countable
37
+ : countable instanceof Map ? countable.size
38
+ : countable.length
39
+ );
40
+
41
+ if (count === 1) return singular;
42
+ if (plural) return plural;
43
+ return `${singular}s`;
44
+ }
@@ -0,0 +1,85 @@
1
+ const DebounceManager = {
2
+ timers: new Map<string, number>(),
3
+ abortListeners: new Map<string, () => void>(),
4
+ };
5
+
6
+ /**
7
+ * Delay running the callback until delay milliseconds have passed without the function being called again.
8
+ * If the function is called again before the delay has passed, the timer resets.
9
+ *
10
+ * @param debounceId A unique identifier for the debounce instance. Call with the same ID to reset the timer.
11
+ * @param callback The callback to run after the delay has passed without the function being called again
12
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
13
+ * @param signal Optional AbortSignal to cancel the debounced operation
14
+ * @returns A promise that resolves to the result of the callback when it is eventually called, or rejects if aborted
15
+ */
16
+ export function debounce<T>(debounceId: string, callback: () => T, delay: number, signal?: AbortSignal) {
17
+ const { timers, abortListeners } = DebounceManager;
18
+
19
+ // Clean up existing debounce with the same ID
20
+ if (timers.has(debounceId)) {
21
+ clearTimeout(timers.get(debounceId));
22
+ timers.delete(debounceId);
23
+
24
+ const existingListener = abortListeners.get(debounceId);
25
+ if (existingListener && signal) {
26
+ signal.removeEventListener('abort', existingListener);
27
+ }
28
+ abortListeners.delete(debounceId);
29
+ }
30
+
31
+ const promise = new Promise<T>((resolve, reject) => {
32
+ // Check if already aborted
33
+ if (signal?.aborted) {
34
+ const error = new DOMException('Debounce aborted', 'AbortError');
35
+ reject(error);
36
+ return;
37
+ }
38
+
39
+ const timeoutId = setTimeout(() => {
40
+ timers.delete(debounceId);
41
+ abortListeners.delete(debounceId);
42
+
43
+ try {
44
+ resolve(callback());
45
+ } catch (error) {
46
+ reject(error);
47
+ }
48
+ }, delay);
49
+
50
+ timers.set(debounceId, timeoutId);
51
+
52
+ // Set up abort listener
53
+ if (signal) {
54
+ const abortListener = () => {
55
+ clearTimeout(timeoutId);
56
+ timers.delete(debounceId);
57
+ abortListeners.delete(debounceId);
58
+
59
+ const error = new DOMException('Debounce aborted', 'AbortError');
60
+ reject(error);
61
+ };
62
+
63
+ signal.addEventListener('abort', abortListener, { once: true });
64
+ abortListeners.set(debounceId, abortListener);
65
+ }
66
+ });
67
+
68
+ return promise;
69
+ }
70
+
71
+ /**
72
+ * 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
73
+ * @param callback The callback to run after the delay has passed without the function being called again
74
+ * @param delay The delay in milliseconds to wait before running the callback after the last call
75
+ * @returns An object containing the debounced function, an AbortController to cancel operations, and the abort method for convenience
76
+ */
77
+ export function wrapInDebounce<T>(callback: (...args: T[]) => void, delay: number) {
78
+ const controller = new AbortController();
79
+
80
+ return {
81
+ debounced: (...args: T[]) => debounce(Symbol().toString(), () => callback(...args), delay, controller.signal),
82
+ controller,
83
+ abort: () => controller.abort(),
84
+ };
85
+ }
@@ -0,0 +1,18 @@
1
+ export function wait<T>(time: number): Promise<void>;
2
+ export function wait<T>(callback: (...args: any[]) => T, time: number): Promise<T>;
3
+ export function wait<T>(callbackOrTime: ((...args: any[]) => T) | number, time?: number) {
4
+ const callback = typeof callbackOrTime === 'function' ? callbackOrTime : (() => undefined);
5
+ time ??= callbackOrTime as number;
6
+
7
+ return new Promise<T>((resolve, reject) => {
8
+ try { setTimeout(() => resolve(callback() as T), time); }
9
+ catch (err) { reject(err); }
10
+ });
11
+ }
12
+
13
+ export function getUnixTime(date: Date | string): number;
14
+ export function getUnixTime(timestamp: number): number;
15
+ export function getUnixTime(arg: Date | string | number): number {
16
+ const timestamp = typeof arg === 'number' ? arg : new Date(arg).getTime();
17
+ return Math.floor(timestamp / 1000);
18
+ }
@@ -0,0 +1,9 @@
1
+ import * as Debounce from './debounce.util';
2
+ import * as Functions from './functions.util';
3
+ import * as Throttle from './throttle.util';
4
+
5
+ export const TimeUtils = {
6
+ ...Functions,
7
+ ...Debounce,
8
+ ...Throttle,
9
+ };
@@ -0,0 +1,44 @@
1
+ const ThrottleManager = {
2
+ cooldowns: new Map<string, boolean>(),
3
+ results: new Map<string, any>(),
4
+ };
5
+
6
+ /**
7
+ * Allow callback to be run once every cooldown period, regardless of calls
8
+ * @param callback The callback to run
9
+ * @param cooldown The delay to wait before allowing the callback to be called again
10
+ * @returns A throttled version of the callback
11
+ */
12
+ export function throttle<T>(throttleId: string, callback: () => T, cooldown: number) {
13
+ const { cooldowns, results } = ThrottleManager;
14
+
15
+ const isCooldown = cooldowns.get(throttleId) ?? false;
16
+ if (isCooldown) return results.get(throttleId) as T;
17
+
18
+ const result = callback();
19
+
20
+ setTimeout(() => {
21
+ cooldowns.delete(throttleId);
22
+ results.delete(throttleId);
23
+ }, cooldown);
24
+
25
+ cooldowns.set(throttleId, true);
26
+ results.set(throttleId, result);
27
+
28
+ return result;
29
+ }
30
+
31
+ /**
32
+ * Wrap a callback in a throttle, allowing it to be called multiple times, but only run once every cooldown period
33
+ * @param callback The callback to run
34
+ * @param cooldown The delay to wait before allowing the callback to be called again
35
+ * @returns A throttled version of the callback that can be called multiple times, but will only run once every cooldown period
36
+ */
37
+ export function wrapInThrottle<T>(callback: (...args: T[]) => void, cooldown: number) {
38
+ const throttleId = Symbol().toString();
39
+ return (...args: T[]) => throttle(throttleId, () => callback(...args), cooldown);
40
+ }
41
+
42
+ export function isThrottleOnCooldown(throttleId: string) {
43
+ return ThrottleManager.cooldowns.get(throttleId) ?? false;
44
+ }
@@ -0,0 +1,8 @@
1
+ export * from './ApiUtils';
2
+ export * from './TimeUtils';
3
+
4
+ export * from './ColorUtils';
5
+ export * from './FormUtils';
6
+ export * from './NumberUtils';
7
+ export * from './PatcherUtils';
8
+ export * from './StringUtils';
@@ -1,95 +0,0 @@
1
- export type UpdateFinder<T> = (item: T, index: number, self: Array<T>) => boolean
2
-
3
- declare global {
4
- interface Array<T> {
5
- /**
6
- * Pushes items to array and returns self with new items
7
- * @param items Items to add to array
8
- */
9
- add(...items: Array<T>): this
10
- /**
11
- * Update an item in array
12
- * @param old The old value or index to update
13
- * @param updated Updated value
14
- */
15
- update(old: T | number | UpdateFinder<T>, updated: T): T
16
- /**
17
- * Removes item from array and returns self without item
18
- * @param item Item or index to remove
19
- */
20
- remove(item: T | number): this
21
- /**
22
- * Returns a random element from array
23
- */
24
- random(): T
25
- /**
26
- * Returns item matching index. If negative number, subtracts number from length
27
- * @param i Index of item
28
- */
29
- index(i: number): T,
30
- /**
31
- * For every x in array, execute callback
32
- * @param every i.e every 2nd item in array
33
- * @param callback Function to execute
34
- * @returns Array of results
35
- */
36
- nth<U>(every: number, callback: (item: T, index: number, collection: Array<T>, self: this) => U): Array<U>
37
- }
38
- }
39
-
40
- function add<T>(this: Array<T>, ...items: Array<T>) {
41
- this.push(...items);
42
- return this;
43
- }
44
- Array.prototype.add = add;
45
-
46
- function update<T>(this: Array<T>, old: T | number | UpdateFinder<T>, updated: T) {
47
- const item = typeof old === 'number' ? this[old]
48
- : typeof old === 'function' ? this.find(old as UpdateFinder<T>)
49
- : old;
50
- if (!item) throw new Error('Old was not found in array!');
51
-
52
- const index = this.indexOf(item);
53
- return this[index] = updated;
54
- }
55
- Array.prototype.update = update;
56
-
57
- function remove<T>(this: Array<T>, value: T | number): Array<T> {
58
- const index = typeof value === 'number' ? value : this.indexOf(value);
59
- if (index > -1) this.splice(index, 1);
60
- return this;
61
- }
62
- Array.prototype.remove = remove;
63
-
64
- function random<T>(this: Array<T>): T {
65
- const randomIndex = Math.round(Math.random() * this.length);
66
- return this[randomIndex];
67
- }
68
- Array.prototype.random = random;
69
-
70
- function index<T>(this: Array<T>, i: number): T {
71
- return this[i < 0 ? this.length + i : i];
72
- }
73
- Array.prototype.index = index;
74
-
75
- function nth<T, U>(this: Array<T>, every: number, callback: (item: T, index: number, collection: Array<T>, self: Array<T>) => U): Array<U> {
76
- const result = new Array<U>();
77
- let collection = new Array<T>();
78
-
79
- for (let i = 0; i < this.length; i++) {
80
- collection.push(this[i]);
81
-
82
- if (i % every === 0) {
83
- result.push(callback(this[i], i, collection, this));
84
- collection = new Array<T>();
85
- }
86
- }
87
-
88
- return result;
89
- }
90
- Array.prototype.nth = nth;
91
-
92
- export const ArrayExtensions = {
93
- add, update, remove,
94
- random, index, nth
95
- };
@@ -1,54 +0,0 @@
1
- import IReplacement from "../Interfaces/IReplacement";
2
-
3
- declare global {
4
- interface String {
5
- /**
6
- * Uppercases first letter of string
7
- */
8
- toPascalCase(): string
9
- /**
10
- * Replaces "replacer" (default: ' ') with "replacement" (default: '_')
11
- * @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
12
- */
13
- toSnakeCase(replaceOptions?: IReplacement): string
14
- /**
15
- * Replaces "replacer" (default: ' ') with "replacement" (default: '-')
16
- * @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
17
- */
18
- toKebabCase(replaceOptions?: IReplacement): string,
19
- /**
20
- * String.substring but accepting negative numbers to cut from length
21
- * @param start Start of string. 0 indexed, if negative number, subtracts number from length
22
- * @param end End of string. 0 indexed, if negative number, substracts number from length
23
- */
24
- clip(start: number, end?: number): string
25
- }
26
- }
27
-
28
- function toPascalCase(this: string) {
29
- return this.substring(0, 1).toUpperCase() + this.substring(1);
30
- }
31
- String.prototype.toPascalCase = toPascalCase;
32
-
33
- function spaceReplacer(self: string, replacer: string | RegExp, replacement: string) {
34
- return self.replace(new RegExp(`${typeof replacer == 'string' ? replacer : replacer.source}+`), replacement);
35
- }
36
-
37
- function toSnakeCase(this: string, replaceOptions: IReplacement) {
38
- return spaceReplacer(this, replaceOptions.replacer || ' ', replaceOptions.replacement || '_')
39
- }
40
- String.prototype.toSnakeCase = toSnakeCase;
41
-
42
- function toKebabCase(this: string, replaceOptions: IReplacement) {
43
- return spaceReplacer(this, replaceOptions.replacer || ' ', replaceOptions.replacement || '-');
44
- }
45
- String.prototype.toKebabCase = toKebabCase;
46
-
47
- function clip(this: string, start: number, end?: number) {
48
- return this.substring(start < 0 ? this.length - start : start, end && end < 0 ? this.length + end : end);
49
- }
50
- String.prototype.clip = clip;
51
-
52
- export const StringExtensions = {
53
- toPascalCase, toSnakeCase, toKebabCase, clip
54
- };