danholibraryjs 1.11.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 (204) hide show
  1. package/dist/Classes/DanhoLogger.d.ts +23 -0
  2. package/dist/Classes/DanhoLogger.js +65 -0
  3. package/dist/Classes/Events/Event.d.ts +66 -66
  4. package/dist/Classes/Events/Event.js +114 -114
  5. package/dist/Classes/Events/EventCollection.d.ts +57 -57
  6. package/dist/Classes/Events/EventCollection.js +109 -109
  7. package/dist/Classes/Events/EventEmitter.d.ts +74 -74
  8. package/dist/Classes/Events/EventEmitter.js +97 -97
  9. package/dist/Classes/Events/index.d.ts +3 -3
  10. package/dist/Classes/Events/index.js +19 -19
  11. package/dist/Classes/Time/Date.d.ts +148 -147
  12. package/dist/Classes/Time/Date.js +241 -238
  13. package/dist/Classes/Time/Time.d.ts +66 -65
  14. package/dist/Classes/Time/Time.js +120 -117
  15. package/dist/Classes/Time/TimeProperties.d.ts +3 -3
  16. package/dist/Classes/Time/TimeProperties.js +2 -2
  17. package/dist/Classes/Time/TimeSpan.d.ts +123 -123
  18. package/dist/Classes/Time/TimeSpan.js +179 -179
  19. package/dist/Classes/Time/index.d.ts +4 -4
  20. package/dist/Classes/Time/index.js +20 -20
  21. package/dist/Classes/index.d.ts +4 -3
  22. package/dist/Classes/index.js +20 -19
  23. package/dist/Classes/store.d.ts +75 -75
  24. package/dist/Classes/store.js +84 -84
  25. package/dist/Extensions/Array/array.extension.d.ts +42 -0
  26. package/dist/Extensions/Array/array.extension.js +57 -0
  27. package/dist/Extensions/Array/crud.extension.d.ts +24 -0
  28. package/dist/Extensions/Array/crud.extension.js +28 -0
  29. package/dist/Extensions/Array/index.d.ts +20 -0
  30. package/dist/Extensions/Array/index.js +40 -0
  31. package/dist/Extensions/Array/loop.extension.d.ts +18 -0
  32. package/dist/Extensions/Array/loop.extension.js +23 -0
  33. package/dist/Extensions/Array/random.extension.d.ts +23 -0
  34. package/dist/Extensions/Array/random.extension.js +35 -0
  35. package/dist/Extensions/Array/sort.extension.d.ts +27 -0
  36. package/dist/Extensions/Array/sort.extension.js +31 -0
  37. package/dist/Extensions/Array/string.extension.d.ts +13 -0
  38. package/dist/Extensions/Array/string.extension.js +14 -0
  39. package/dist/Extensions/Array.d.ts +52 -52
  40. package/dist/Extensions/Array.js +51 -51
  41. package/dist/Extensions/Document.d.ts +27 -27
  42. package/dist/Extensions/Document.js +32 -32
  43. package/dist/Extensions/Function.d.ts +29 -14
  44. package/dist/Extensions/Function.js +23 -10
  45. package/dist/Extensions/Map.d.ts +54 -54
  46. package/dist/Extensions/Map.js +42 -42
  47. package/dist/Extensions/Number.d.ts +13 -0
  48. package/dist/Extensions/Number.js +40 -0
  49. package/dist/Extensions/Object/arrays.extension.d.ts +17 -0
  50. package/dist/Extensions/Object/arrays.extension.js +13 -0
  51. package/dist/Extensions/Object/booleans.extension.d.ts +18 -0
  52. package/dist/Extensions/Object/booleans.extension.js +37 -0
  53. package/dist/Extensions/Object/extracts.extension.d.ts +38 -0
  54. package/dist/Extensions/Object/extracts.extension.js +72 -0
  55. package/dist/Extensions/Object/index.d.ts +10 -49
  56. package/dist/Extensions/Object/index.js +36 -38
  57. package/dist/Extensions/Object/properties.d.ts +28 -28
  58. package/dist/Extensions/Object/properties.extension.d.ts +6 -0
  59. package/dist/Extensions/Object/properties.extension.js +4 -0
  60. package/dist/Extensions/Object/properties.js +19 -20
  61. package/dist/Extensions/String/case.extension.d.ts +12 -0
  62. package/dist/Extensions/String/case.extension.js +55 -0
  63. package/dist/Extensions/String/index.d.ts +4 -0
  64. package/dist/Extensions/String/index.js +30 -0
  65. package/dist/Extensions/String.d.ts +36 -36
  66. package/dist/Extensions/String.js +25 -25
  67. package/dist/Extensions/index.d.ts +6 -17
  68. package/dist/Extensions/index.js +22 -30
  69. package/dist/Functions/CopyToClipboard.d.ts +7 -7
  70. package/dist/Functions/CopyToClipboard.js +15 -15
  71. package/dist/Functions/GetCSSProperty.d.ts +15 -15
  72. package/dist/Functions/GetCSSProperty.js +26 -26
  73. package/dist/Functions/GetNestedProperty.d.ts +9 -9
  74. package/dist/Functions/GetNestedProperty.js +23 -23
  75. package/dist/Functions/HTMLEvent.d.ts +11 -11
  76. package/dist/Functions/HTMLEvent.js +14 -14
  77. package/dist/Functions/SetNavigationSelected.d.ts +9 -9
  78. package/dist/Functions/SetNavigationSelected.js +25 -25
  79. package/dist/Functions/index.d.ts +5 -5
  80. package/dist/Functions/index.js +21 -21
  81. package/dist/Interfaces/ElementOptions.d.ts +15 -15
  82. package/dist/Interfaces/ElementOptions.js +2 -2
  83. package/dist/Interfaces/IReplacement.d.ts +12 -12
  84. package/dist/Interfaces/IReplacement.js +2 -2
  85. package/dist/Interfaces/index.d.ts +2 -2
  86. package/dist/Interfaces/index.js +18 -18
  87. package/dist/Types/Able.d.ts +16 -0
  88. package/dist/Types/Able.js +2 -0
  89. package/dist/Types/Array.d.ts +6 -0
  90. package/dist/Types/Array.js +2 -0
  91. package/dist/Types/BetterTypes.d.ts +9 -9
  92. package/dist/Types/BetterTypes.js +2 -2
  93. package/dist/Types/C#.d.ts +8 -0
  94. package/dist/Types/C#.js +2 -0
  95. package/dist/Types/Date.d.ts +6 -6
  96. package/dist/Types/Date.js +2 -2
  97. package/dist/Types/Events.d.ts +10 -10
  98. package/dist/Types/Events.js +2 -2
  99. package/dist/Types/Function.d.ts +5 -0
  100. package/dist/Types/Function.js +2 -0
  101. package/dist/Types/Object.d.ts +4 -0
  102. package/dist/Types/Object.js +2 -0
  103. package/dist/Types/PropertiesWith.d.ts +34 -13
  104. package/dist/Types/PropertiesWith.js +2 -2
  105. package/dist/Types/String.d.ts +1 -0
  106. package/dist/Types/String.js +2 -0
  107. package/dist/Types/TransformTypes.d.ts +22 -16
  108. package/dist/Types/TransformTypes.js +2 -2
  109. package/dist/Types/index.d.ts +24 -65
  110. package/dist/Types/index.js +27 -21
  111. package/dist/Utils/ApiUtil/ApiTypes.d.ts +15 -15
  112. package/dist/Utils/ApiUtil/ApiTypes.js +15 -15
  113. package/dist/Utils/ApiUtil/RequestUtil.d.ts +19 -19
  114. package/dist/Utils/ApiUtil/RequestUtil.js +73 -73
  115. package/dist/Utils/ApiUtil/index.d.ts +20 -20
  116. package/dist/Utils/ApiUtil/index.js +33 -33
  117. package/dist/Utils/ApiUtils/ApiTypes.d.ts +15 -0
  118. package/dist/Utils/ApiUtils/ApiTypes.js +15 -0
  119. package/dist/Utils/ApiUtils/RequestUtil.d.ts +19 -0
  120. package/dist/Utils/ApiUtils/RequestUtil.js +73 -0
  121. package/dist/Utils/ApiUtils/index.d.ts +20 -0
  122. package/dist/Utils/ApiUtils/index.js +33 -0
  123. package/dist/Utils/ColorUtils.d.ts +11 -0
  124. package/dist/Utils/ColorUtils.js +93 -0
  125. package/dist/Utils/FormUtil.d.ts +6 -6
  126. package/dist/Utils/FormUtil.js +35 -35
  127. package/dist/Utils/FormUtils.d.ts +6 -0
  128. package/dist/Utils/FormUtils.js +35 -0
  129. package/dist/Utils/NumberUtils.d.ts +1 -0
  130. package/dist/Utils/NumberUtils.js +7 -0
  131. package/dist/Utils/PatcherUtils.d.ts +6 -0
  132. package/dist/Utils/PatcherUtils.js +80 -0
  133. package/dist/Utils/StringUtils.d.ts +3 -0
  134. package/dist/Utils/StringUtils.js +47 -0
  135. package/dist/Utils/TimeUtils/debounce.util.d.ts +22 -0
  136. package/dist/Utils/TimeUtils/debounce.util.js +78 -0
  137. package/dist/Utils/TimeUtils/functions.util.d.ts +4 -0
  138. package/dist/Utils/TimeUtils/functions.util.js +21 -0
  139. package/dist/Utils/TimeUtils/index.d.ts +15 -0
  140. package/dist/Utils/TimeUtils/index.js +34 -0
  141. package/dist/Utils/TimeUtils/throttle.util.d.ts +15 -0
  142. package/dist/Utils/TimeUtils/throttle.util.js +43 -0
  143. package/dist/Utils/index.d.ts +7 -2
  144. package/dist/Utils/index.js +23 -18
  145. package/dist/index.d.ts +5 -5
  146. package/dist/index.js +21 -21
  147. package/package.json +4 -2
  148. package/src/Classes/DanhoLogger.ts +78 -0
  149. package/src/Classes/Events/Event.ts +96 -96
  150. package/src/Classes/Events/EventCollection.ts +90 -90
  151. package/src/Classes/Events/EventEmitter.ts +68 -68
  152. package/src/Classes/Time/Date.ts +219 -216
  153. package/src/Classes/Time/Time.ts +109 -104
  154. package/src/Classes/Time/TimeSpan.ts +171 -171
  155. package/src/Classes/index.ts +1 -0
  156. package/src/Classes/store.ts +22 -22
  157. package/src/Extensions/Array/array.extension.ts +103 -0
  158. package/src/Extensions/Array/crud.extension.ts +46 -0
  159. package/src/Extensions/Array/index.ts +15 -0
  160. package/src/Extensions/Array/loop.extension.ts +38 -0
  161. package/src/Extensions/Array/random.extension.ts +56 -0
  162. package/src/Extensions/Array/sort.extension.ts +52 -0
  163. package/src/Extensions/Array/string.extension.ts +22 -0
  164. package/src/Extensions/Document.ts +39 -39
  165. package/src/Extensions/Function.ts +37 -10
  166. package/src/Extensions/Map.ts +56 -56
  167. package/src/Extensions/Number.ts +50 -0
  168. package/src/Extensions/Object/arrays.extension.ts +27 -0
  169. package/src/Extensions/Object/booleans.extension.ts +46 -0
  170. package/src/Extensions/Object/extracts.extension.ts +102 -0
  171. package/src/Extensions/Object/index.ts +9 -80
  172. package/src/Extensions/Object/properties.extension.ts +11 -0
  173. package/src/Extensions/Object/properties.ts +35 -36
  174. package/src/Extensions/String/case.extension.ts +95 -0
  175. package/src/Extensions/String/index.ts +5 -0
  176. package/src/Extensions/index.ts +2 -20
  177. package/src/Interfaces/ElementOptions.ts +7 -7
  178. package/src/Interfaces/IReplacement.ts +2 -2
  179. package/src/Types/Able.ts +22 -0
  180. package/src/Types/Array.ts +7 -0
  181. package/src/Types/C#.ts +9 -0
  182. package/src/Types/Date.ts +1 -1
  183. package/src/Types/Events.ts +12 -12
  184. package/src/Types/Function.ts +10 -0
  185. package/src/Types/Object.ts +4 -0
  186. package/src/Types/PropertiesWith.ts +35 -4
  187. package/src/Types/String.ts +1 -0
  188. package/src/Types/TransformTypes.ts +21 -13
  189. package/src/Types/index.ts +7 -69
  190. package/src/Utils/{ApiUtil → ApiUtils}/ApiTypes.ts +2 -1
  191. package/src/Utils/{ApiUtil → ApiUtils}/index.ts +1 -1
  192. package/src/Utils/ColorUtils.ts +102 -0
  193. package/src/Utils/{FormUtil.ts → FormUtils.ts} +2 -2
  194. package/src/Utils/NumberUtils.ts +3 -0
  195. package/src/Utils/PatcherUtils.ts +111 -0
  196. package/src/Utils/StringUtils.ts +44 -0
  197. package/src/Utils/TimeUtils/debounce.util.ts +85 -0
  198. package/src/Utils/TimeUtils/functions.util.ts +18 -0
  199. package/src/Utils/TimeUtils/index.ts +9 -0
  200. package/src/Utils/TimeUtils/throttle.util.ts +44 -0
  201. package/src/Utils/index.ts +8 -2
  202. package/src/Extensions/Array.ts +0 -95
  203. package/src/Extensions/String.ts +0 -54
  204. /package/src/Utils/{ApiUtil → ApiUtils}/RequestUtil.ts +0 -0
@@ -1,84 +1,84 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Store = void 0;
4
- const Events_1 = require("./Events");
5
- /**
6
- * EventEmitter, but it stores state and handles state change with reducers
7
- *
8
- * @Initialization Actions & initial state must be defined in type parameters. InitialState must be provided in constructor, whereas reducer is optional.
9
- * The ActionType must have properties as strings and values as arrays.
10
- *
11
- * @HandlingActions Reducers can be added through constructor or using Store.on('action', reducer) or Store.once('action', reducer).
12
- * Every state change must return the next state, apart from 'stateChange', which returns void/any
13
- * Emit/Dispatch an action using Store.dispatch('action', ...args), ...args being the parameters from the ActionType.
14
- * Store.emit should NOT be used, as it doesn't update the Store's state.
15
- *
16
- * Reducer functions can be removed using Store.off('action', reducer);
17
- *
18
- * @borrows EventEmitter
19
- * @borrows Arrayable
20
- *
21
- * @example ```ts
22
- * import { Store } from 'danholibraryjs';
23
- *
24
- * type Todo = {
25
- * id: string,
26
- * text: string,
27
- * completed: boolean
28
- * }
29
- *
30
- * type TodoActions = {
31
- * create: [text: string],
32
- * update: [id: string, text: string],
33
- * toggleComplete: [id: string, force?: boolean],
34
- * delete: [id: string],
35
- * }
36
- *
37
- * const store = new Store<Array<Todo>, TodoActions>(new Array<Todo>(), new Map([
38
- * create: (state, text) => {
39
- * return [...state, {
40
- * id: Math.random().toString(),
41
- * text,
42
- * completed: false
43
- * }];
44
- * },
45
- * toggleComplete: (state, id, force) => {
46
- * const todo = state.find(todo => todo.id === id);
47
- * if (!todo) return state;
48
- *
49
- * return state.map(todo => (
50
- * todo.id === id ? {
51
- * ...todo,
52
- * completed: force === undefined ? !todo.completed : force
53
- * } : todo
54
- * ));
55
- * }
56
- * ]));
57
- *
58
- * store.on('delete', (state, id) => {
59
- * return state.filter(todo => todo.id !== id);
60
- * });
61
- *
62
- * store.on('stateChange', (prevState, currentState) => console.log('State change', prevState, currentState));
63
- *
64
- * store.dispatch('create', 'Make store!');
65
- *
66
- * ```
67
- */
68
- class Store extends Events_1.EventEmitter {
69
- constructor(state, actions = {}) {
70
- super(new Map(...Object.entries(actions).map(([action, reducers]) => [action, reducers])));
71
- this._state = state;
72
- }
73
- _state;
74
- get state() {
75
- return this._state;
76
- }
77
- dispatch(action, ...args) {
78
- const previous = { ...this._state };
79
- this._state = super.emit(action, ...args).reduce((state, returned) => ({ ...state, ...returned }), this.state);
80
- super.emit('stateChange', ...[previous, this.state]);
81
- return this.state;
82
- }
83
- }
84
- exports.Store = Store;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Store = void 0;
4
+ const Events_1 = require("./Events");
5
+ /**
6
+ * EventEmitter, but it stores state and handles state change with reducers
7
+ *
8
+ * @Initialization Actions & initial state must be defined in type parameters. InitialState must be provided in constructor, whereas reducer is optional.
9
+ * The ActionType must have properties as strings and values as arrays.
10
+ *
11
+ * @HandlingActions Reducers can be added through constructor or using Store.on('action', reducer) or Store.once('action', reducer).
12
+ * Every state change must return the next state, apart from 'stateChange', which returns void/any
13
+ * Emit/Dispatch an action using Store.dispatch('action', ...args), ...args being the parameters from the ActionType.
14
+ * Store.emit should NOT be used, as it doesn't update the Store's state.
15
+ *
16
+ * Reducer functions can be removed using Store.off('action', reducer);
17
+ *
18
+ * @borrows EventEmitter
19
+ * @borrows Arrayable
20
+ *
21
+ * @example ```ts
22
+ * import { Store } from 'danholibraryjs';
23
+ *
24
+ * type Todo = {
25
+ * id: string,
26
+ * text: string,
27
+ * completed: boolean
28
+ * }
29
+ *
30
+ * type TodoActions = {
31
+ * create: [text: string],
32
+ * update: [id: string, text: string],
33
+ * toggleComplete: [id: string, force?: boolean],
34
+ * delete: [id: string],
35
+ * }
36
+ *
37
+ * const store = new Store<Array<Todo>, TodoActions>(new Array<Todo>(), new Map([
38
+ * create: (state, text) => {
39
+ * return [...state, {
40
+ * id: Math.random().toString(),
41
+ * text,
42
+ * completed: false
43
+ * }];
44
+ * },
45
+ * toggleComplete: (state, id, force) => {
46
+ * const todo = state.find(todo => todo.id === id);
47
+ * if (!todo) return state;
48
+ *
49
+ * return state.map(todo => (
50
+ * todo.id === id ? {
51
+ * ...todo,
52
+ * completed: force === undefined ? !todo.completed : force
53
+ * } : todo
54
+ * ));
55
+ * }
56
+ * ]));
57
+ *
58
+ * store.on('delete', (state, id) => {
59
+ * return state.filter(todo => todo.id !== id);
60
+ * });
61
+ *
62
+ * store.on('stateChange', (prevState, currentState) => console.log('State change', prevState, currentState));
63
+ *
64
+ * store.dispatch('create', 'Make store!');
65
+ *
66
+ * ```
67
+ */
68
+ class Store extends Events_1.EventEmitter {
69
+ constructor(state, actions = {}) {
70
+ super(new Map(...Object.entries(actions).map(([action, reducers]) => [action, reducers])));
71
+ this._state = state;
72
+ }
73
+ _state;
74
+ get state() {
75
+ return this._state;
76
+ }
77
+ dispatch(action, ...args) {
78
+ const previous = { ...this._state };
79
+ this._state = super.emit(action, ...args).reduce((state, returned) => ({ ...state, ...returned }), this.state);
80
+ super.emit('stateChange', ...[previous, this.state]);
81
+ return this.state;
82
+ }
83
+ }
84
+ exports.Store = Store;
@@ -0,0 +1,42 @@
1
+ import { Arrayable } from "../../Types";
2
+ declare global {
3
+ interface Array<T> {
4
+ /**
5
+ * Returns the first `count` elements from the array.
6
+ * @param count Number of elements to take.
7
+ * @returns Array containing the first `count` elements.
8
+ */
9
+ take(count: number): Array<T>;
10
+ /**
11
+ * Returns a new array with only unique elements from the original array.
12
+ * @returns An array containing only unique elements.
13
+ */
14
+ unique(): Array<T>;
15
+ /**
16
+ * Splits the array into chunks of a specified size or by a splitter function.
17
+ * @param chunkSizeOrSplitter The size of each chunk or a function that determines where to split the array.
18
+ * @returns An array of arrays, where each sub-array is a chunk of the original array.
19
+ */
20
+ splitBy(chunkSizeOrSplitter: number | ((value: T, index: number, array: Array<T>) => boolean)): Array<Array<T>>;
21
+ /**
22
+ * Groups the elements of the array based on a key selector function.
23
+ * @param keySelector A function that selects a key for each element in the array.
24
+ * @returns A Map where each key is a group identifier and the value is an array of elements in that group.
25
+ */
26
+ groupBy<K>(keySelector: (value: T, index: number, array: Array<T>) => K): Map<K, Array<T>>;
27
+ }
28
+ interface ArrayConstructor {
29
+ /**
30
+ * Forces an arrayable object into an array
31
+ * @param arrayable The arrayable object to force into an array
32
+ * @returns An array containing the elements of the input or the single value.
33
+ */
34
+ forceArray<T>(arrayable: Arrayable<T>): Array<T>;
35
+ }
36
+ }
37
+ export declare function take<T>(this: Array<T>, count: number): Array<T>;
38
+ export declare function forceArray<T>(arrayable: Arrayable<T>): Array<T>;
39
+ export declare function unique<T>(this: Array<T>): Array<T>;
40
+ export declare function splitBy<T>(this: Array<T>, chunkSize: number): Array<Array<T>>;
41
+ export declare function splitBy<T>(this: Array<T>, splitter: (value: T, index: number, array: Array<T>) => boolean): Array<Array<T>>;
42
+ export declare function groupBy<T, K>(this: Array<T>, keySelector: (value: T, index: number, array: Array<T>) => K): Map<K, Array<T>>;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupBy = exports.splitBy = exports.unique = exports.forceArray = exports.take = void 0;
4
+ function take(count) {
5
+ return this.slice(0, count);
6
+ }
7
+ exports.take = take;
8
+ Array.prototype.take = take;
9
+ function forceArray(arrayable) {
10
+ return Array.isArray(arrayable) ? arrayable : [arrayable];
11
+ }
12
+ exports.forceArray = forceArray;
13
+ Array.forceArray = forceArray;
14
+ function unique() {
15
+ return [...new Set(this)];
16
+ }
17
+ exports.unique = unique;
18
+ Array.prototype.unique = unique;
19
+ function splitBy(chunkSizeOrSplitter) {
20
+ const chunkSize = typeof chunkSizeOrSplitter === 'number' ? chunkSizeOrSplitter : undefined;
21
+ const splitter = typeof chunkSizeOrSplitter === 'function' ? chunkSizeOrSplitter : undefined;
22
+ const result = [];
23
+ if (chunkSize !== undefined) {
24
+ for (let i = 0; i < this.length; i += chunkSize) {
25
+ result.push(this.slice(i, i + chunkSize));
26
+ }
27
+ }
28
+ else if (splitter !== undefined) {
29
+ const chunk = new Array();
30
+ for (let i = 0; i < this.length; i++) {
31
+ const value = this[i];
32
+ if (splitter(value, i, this)) {
33
+ if (chunk.length) {
34
+ result.push(chunk.splice(0, chunk.length));
35
+ }
36
+ }
37
+ chunk.push(value);
38
+ }
39
+ if (chunk.length) {
40
+ result.push(chunk);
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+ exports.splitBy = splitBy;
46
+ Array.prototype.splitBy = splitBy;
47
+ function groupBy(keySelector) {
48
+ return this.reduce((map, item, index, array) => {
49
+ const key = keySelector(item, index, array);
50
+ const group = map.get(key) ?? new Array();
51
+ group.push(item);
52
+ map.set(key, group);
53
+ return map;
54
+ }, new Map());
55
+ }
56
+ exports.groupBy = groupBy;
57
+ Array.prototype.groupBy = groupBy;
@@ -0,0 +1,24 @@
1
+ export type UpdateFinder<T> = (item: T, index: number, self: Array<T>) => boolean;
2
+ declare global {
3
+ interface Array<T> {
4
+ /**
5
+ * Pushes items to array and returns self with new items
6
+ * @param items Items to add to array
7
+ */
8
+ add(...items: Array<T>): this;
9
+ /**
10
+ * Update an item in array
11
+ * @param old The old value or index to update
12
+ * @param updated Updated value
13
+ */
14
+ update(old: T | number | UpdateFinder<T>, updated: T): T;
15
+ /**
16
+ * Removes item from array and returns self without item
17
+ * @param item Item or index to remove
18
+ */
19
+ remove(item: T | number): this;
20
+ }
21
+ }
22
+ export declare function add<T>(this: Array<T>, ...items: Array<T>): T[];
23
+ export declare function update<T>(this: Array<T>, old: T | number | UpdateFinder<T>, updated: T): T;
24
+ export declare function remove<T>(this: Array<T>, value: T | number): Array<T>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.remove = exports.update = exports.add = void 0;
4
+ function add(...items) {
5
+ this.push(...items);
6
+ return this;
7
+ }
8
+ exports.add = add;
9
+ Array.prototype.add = add;
10
+ function update(old, updated) {
11
+ const item = typeof old === 'number' ? this[old]
12
+ : typeof old === 'function' ? this.find(old)
13
+ : old;
14
+ if (!item)
15
+ throw new Error('Old was not found in array!');
16
+ const index = this.indexOf(item);
17
+ return this[index] = updated;
18
+ }
19
+ exports.update = update;
20
+ Array.prototype.update = update;
21
+ function remove(value) {
22
+ const index = typeof value === 'number' ? value : this.indexOf(value);
23
+ if (index > -1)
24
+ this.splice(index, 1);
25
+ return this;
26
+ }
27
+ exports.remove = remove;
28
+ Array.prototype.remove = remove;
@@ -0,0 +1,20 @@
1
+ import * as CRUD from './crud.extension';
2
+ export declare const ArrayExtensions: {
3
+ join<T>(this: T[], separator?: string, endSeparator?: string): string;
4
+ orderBy<T_1>(this: T_1[], ...comparators: ((a: T_1, b: T_1) => number)[]): T_1[];
5
+ orderByDescending<T_2>(this: T_2[], ...comparators: ((a: T_2, b: T_2) => number)[]): T_2[];
6
+ sortByProperty<T_3 extends object>(this: T_3[], ...properties: (keyof T_3)[]): T_3[];
7
+ random<T_4>(this: T_4[]): T_4;
8
+ shuffle<T_5>(this: T_5[]): T_5[];
9
+ randomWithPercentages<T_6>(items: [item: T_6, weight: number][]): T_6;
10
+ nth<T_7, U>(this: T_7[], every: number, callback: (item: T_7, index: number, collection: T_7[], self: T_7[]) => U): U[];
11
+ add<T_8>(this: T_8[], ...items: T_8[]): T_8[];
12
+ update<T_9>(this: T_9[], old: number | T_9 | CRUD.UpdateFinder<T_9>, updated: T_9): T_9;
13
+ remove<T_10>(this: T_10[], value: number | T_10): T_10[];
14
+ take<T_11>(this: T_11[], count: number): T_11[];
15
+ forceArray<T_12>(arrayable: import("../..").Arrayable<T_12>): T_12[];
16
+ unique<T_13>(this: T_13[]): T_13[];
17
+ splitBy<T_14>(this: T_14[], chunkSize: number): T_14[][];
18
+ splitBy<T_15>(this: T_15[], splitter: (value: T_15, index: number, array: T_15[]) => boolean): T_15[][];
19
+ groupBy<T_16, K>(this: T_16[], keySelector: (value: T_16, index: number, array: T_16[]) => K): Map<K, T_16[]>;
20
+ };
@@ -0,0 +1,40 @@
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.ArrayExtensions = void 0;
27
+ const Array = __importStar(require("./array.extension"));
28
+ const CRUD = __importStar(require("./crud.extension"));
29
+ const Loop = __importStar(require("./loop.extension"));
30
+ const Random = __importStar(require("./random.extension"));
31
+ const Sort = __importStar(require("./sort.extension"));
32
+ const String = __importStar(require("./string.extension"));
33
+ exports.ArrayExtensions = {
34
+ ...Array,
35
+ ...CRUD,
36
+ ...Loop,
37
+ ...Random,
38
+ ...Sort,
39
+ ...String,
40
+ };
@@ -0,0 +1,18 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ /**
4
+ * For every x in array, execute callback
5
+ * @param every i.e every 2nd item in array
6
+ * @param callback Function to execute
7
+ * @returns Array of results
8
+ */
9
+ nth<U>(every: number, callback: (item: T, index: number, collection: Array<T>, self: this) => U): Array<U>;
10
+ }
11
+ }
12
+ /**
13
+ * For every x in array, execute callback
14
+ * @param every i.e every 2nd item in array
15
+ * @param callback Function to execute. This includes a collection of items prior to last callback run
16
+ * @returns Array of results
17
+ */
18
+ export declare function nth<T, U>(this: Array<T>, every: number, callback: (item: T, index: number, collection: Array<T>, self: Array<T>) => U): Array<U>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nth = void 0;
4
+ /**
5
+ * For every x in array, execute callback
6
+ * @param every i.e every 2nd item in array
7
+ * @param callback Function to execute. This includes a collection of items prior to last callback run
8
+ * @returns Array of results
9
+ */
10
+ function nth(every, callback) {
11
+ const result = new Array();
12
+ let collection = new Array();
13
+ for (let i = 0; i < this.length; i++) {
14
+ collection.push(this[i]);
15
+ if ((i + 1) % every === 0) {
16
+ result.push(callback(this[i], i, collection, this));
17
+ collection = new Array();
18
+ }
19
+ }
20
+ return result;
21
+ }
22
+ exports.nth = nth;
23
+ Array.prototype.nth = nth;
@@ -0,0 +1,23 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ /**
4
+ * Returns a random element from the array.
5
+ * @returns A random element from the array.
6
+ */
7
+ random(): T;
8
+ /**
9
+ * Returns a new array with the elements of the original array in random order.
10
+ * @returns A new array with the elements shuffled.
11
+ */
12
+ shuffle(): Array<T>;
13
+ /**
14
+ * Selects a random item from an array based on specified weights.
15
+ * @param items An array of tuples where each tuple contains an item and its corresponding weight.
16
+ * @returns A randomly selected item based on the provided weights.
17
+ */
18
+ randomWithPercentages(items: [item: T, weight: number][]): T;
19
+ }
20
+ }
21
+ export declare function random<T>(this: Array<T>): T;
22
+ export declare function shuffle<T>(this: Array<T>): Array<T>;
23
+ export declare function randomWithPercentages<T>(items: [item: T, weight: number][]): T;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomWithPercentages = exports.shuffle = exports.random = void 0;
4
+ function random() {
5
+ const randomIndex = Math.floor(Math.random() * this.length);
6
+ return this[randomIndex];
7
+ }
8
+ exports.random = random;
9
+ Array.prototype.random = random;
10
+ function shuffle() {
11
+ return this.sort(() => Math.random() - 0.5);
12
+ }
13
+ exports.shuffle = shuffle;
14
+ Array.prototype.shuffle = shuffle;
15
+ function randomWithPercentages(items) {
16
+ if (items.length === 0)
17
+ throw new Error('Items array cannot be empty');
18
+ // Calculate total weight in case weights don't sum to 100
19
+ const totalWeight = items.reduce((sum, [, weight]) => sum + weight, 0);
20
+ if (totalWeight === 0)
21
+ throw new Error('Total weight must be greater than zero');
22
+ // Generate random number between 0 and totalWeight
23
+ const random = Math.random() * totalWeight;
24
+ // Find the item that corresponds to this random value
25
+ let currentWeight = 0;
26
+ for (const [item, weight] of items) {
27
+ currentWeight += weight;
28
+ if (random <= currentWeight) {
29
+ return item;
30
+ }
31
+ }
32
+ throw new Error('Unable to select an item based on weights');
33
+ }
34
+ exports.randomWithPercentages = randomWithPercentages;
35
+ Array.prototype.randomWithPercentages = randomWithPercentages;
@@ -0,0 +1,27 @@
1
+ type Comparator<T> = (a: T, b: T) => number;
2
+ declare global {
3
+ interface Array<T> {
4
+ /**
5
+ * Sorts the array using multiple comparators.
6
+ * @param comparators Functions to compare elements.
7
+ * @returns Sorted array.
8
+ */
9
+ orderBy(...comparators: Array<Comparator<T>>): Array<T>;
10
+ /**
11
+ * Sorts the array using multiple comparators in descending order.
12
+ * @param comparators Functions to compare elements.
13
+ * @returns Sorted array in descending order.
14
+ */
15
+ orderByDescending(...comparators: Array<Comparator<T>>): Array<T>;
16
+ /**
17
+ * Sorts the array by specified properties.
18
+ * @param properties Properties to sort by.
19
+ * @returns Sorted array.
20
+ */
21
+ sortByProperty(...properties: Array<keyof T>): Array<T>;
22
+ }
23
+ }
24
+ export declare function orderBy<T>(this: Array<T>, ...comparators: Array<Comparator<T>>): Array<T>;
25
+ export declare function orderByDescending<T>(this: Array<T>, ...comparators: Array<Comparator<T>>): Array<T>;
26
+ export declare function sortByProperty<T extends object>(this: Array<T>, ...properties: Array<keyof T>): Array<T>;
27
+ export {};
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortByProperty = exports.orderByDescending = exports.orderBy = void 0;
4
+ function orderBy(...comparators) {
5
+ return this.sort((a, b) => {
6
+ for (const comparator of comparators) {
7
+ const result = comparator(a, b);
8
+ if (result !== 0)
9
+ return result;
10
+ }
11
+ return 0;
12
+ });
13
+ }
14
+ exports.orderBy = orderBy;
15
+ Array.prototype.orderBy = orderBy;
16
+ function orderByDescending(...comparators) {
17
+ return this.orderBy(...comparators).reverse();
18
+ }
19
+ exports.orderByDescending = orderByDescending;
20
+ Array.prototype.orderByDescending = orderByDescending;
21
+ function sortByProperty(...properties) {
22
+ return this.orderBy(...properties.map(property => (a, b) => {
23
+ if (a[property] < b[property])
24
+ return -1;
25
+ if (a[property] > b[property])
26
+ return 1;
27
+ return 0;
28
+ }));
29
+ }
30
+ exports.sortByProperty = sortByProperty;
31
+ Array.prototype.sortByProperty = sortByProperty;
@@ -0,0 +1,13 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ /**
4
+ * Joins the elements of the array into a string, with optional custom separators.
5
+ * @param args An array of strings or undefined values to be used as separators between elements.
6
+ * @param separator The default separator to use between elements if args is not provided or undefined.
7
+ * @param endSeparator The separator to use before the last element.
8
+ * @returns A string with the joined elements.
9
+ */
10
+ join(args?: Array<string | undefined>, separator?: string, endSeparator?: string): string;
11
+ }
12
+ }
13
+ export declare function join<T>(this: Array<T>, separator?: string, endSeparator?: string): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.join = void 0;
4
+ function join(separator = ',', endSeparator = '&') {
5
+ const validArgs = this.filter(arg => !Object.isNullOrUndefined(arg) && arg !== '');
6
+ if (!validArgs.length)
7
+ return '';
8
+ if (validArgs.length === 1)
9
+ return validArgs.shift().toString();
10
+ const lastArg = validArgs.pop();
11
+ const combinedArgs = validArgs.join(separator);
12
+ return `${combinedArgs}${endSeparator ? ` ${endSeparator} ` : ''}${lastArg}`;
13
+ }
14
+ exports.join = join;