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,23 @@
1
+ type LogOptions = {
2
+ name?: string;
3
+ color?: string;
4
+ preferCollapsed?: boolean;
5
+ };
6
+ export default class DanhoLogger {
7
+ name: string;
8
+ color?: string;
9
+ private _preferCollapsed;
10
+ constructor(options?: LogOptions);
11
+ private _getPrefix;
12
+ private _log;
13
+ log(...args: any[]): this;
14
+ warn(...args: any[]): this;
15
+ error(...args: any[]): this;
16
+ group(...args: any[]): this;
17
+ groupCollapsed(...args: any[]): this;
18
+ groupEnd(...args: any[]): this;
19
+ time(label: string): this;
20
+ timeEnd(label: string): this;
21
+ table(data: any, columns?: string[]): this;
22
+ }
23
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class DanhoLogger {
4
+ name;
5
+ color;
6
+ _preferCollapsed;
7
+ constructor(options = {}) {
8
+ this.name = options.name || 'DanhoLogger';
9
+ this.color = options.color;
10
+ this._preferCollapsed = options.preferCollapsed ?? false;
11
+ }
12
+ _getPrefix() {
13
+ return {
14
+ prefix: `%c[${this.name}]`,
15
+ style: `color: ${this.color || 'inherit'}; font-weight: bold;`
16
+ };
17
+ }
18
+ _log(type, ...args) {
19
+ const { prefix, style } = this._getPrefix();
20
+ console[type](prefix, style, ...args);
21
+ return this;
22
+ }
23
+ log(...args) {
24
+ return this._log('log', ...args);
25
+ }
26
+ warn(...args) {
27
+ return this._log('warn', ...args);
28
+ }
29
+ error(...args) {
30
+ return this._log('error', ...args);
31
+ }
32
+ group(...args) {
33
+ const { prefix, style } = this._getPrefix();
34
+ const groupMethod = this._preferCollapsed ? console.groupCollapsed : console.group;
35
+ groupMethod(prefix, style, ...args);
36
+ return this;
37
+ }
38
+ groupCollapsed(...args) {
39
+ const { prefix, style } = this._getPrefix();
40
+ console.groupCollapsed(prefix, style, ...args);
41
+ return this;
42
+ }
43
+ groupEnd(...args) {
44
+ if (args.length)
45
+ console.log(...args);
46
+ console.groupEnd();
47
+ return this;
48
+ }
49
+ time(label) {
50
+ const { prefix } = this._getPrefix();
51
+ console.time(`${prefix} ${label}`);
52
+ return this;
53
+ }
54
+ timeEnd(label) {
55
+ const { prefix } = this._getPrefix();
56
+ console.timeEnd(`${prefix} ${label}`);
57
+ return this;
58
+ }
59
+ table(data, columns) {
60
+ this.log();
61
+ console.table(data, columns);
62
+ return this;
63
+ }
64
+ }
65
+ exports.default = DanhoLogger;
@@ -60,7 +60,7 @@ export declare class EventEmitter<Events extends BaseEvent<string, Array<any>>>
60
60
  * @param event Event to emit
61
61
  * @param args Arguments for the event
62
62
  * @fires event
63
- * @returns Array of listeners' reponses
63
+ * @returns Array of listeners' responses
64
64
  */
65
65
  emit<Return extends any, Event extends keyof Events>(event: Event, ...args: Events[Event]): Array<Return>;
66
66
  /**
@@ -77,7 +77,7 @@ class EventEmitter {
77
77
  * @param event Event to emit
78
78
  * @param args Arguments for the event
79
79
  * @fires event
80
- * @returns Array of listeners' reponses
80
+ * @returns Array of listeners' responses
81
81
  */
82
82
  emit(event, ...args) {
83
83
  return this._events.emit(event, ...args);
@@ -55,6 +55,7 @@ declare class DanhoDate {
55
55
  */
56
56
  get hours(): number;
57
57
  set hours(value: number);
58
+ get isAM(): boolean;
58
59
  get isPM(): boolean;
59
60
  /**
60
61
  * Minutes of the date
@@ -126,8 +126,11 @@ class DanhoDate {
126
126
  */
127
127
  get hours() { return this.date.getHours(); }
128
128
  set hours(value) { this.date.setHours(value); }
129
+ get isAM() {
130
+ return this.hours < 12;
131
+ }
129
132
  get isPM() {
130
- return this.hours > 11;
133
+ return this.hours >= 12;
131
134
  }
132
135
  /**
133
136
  * Minutes of the date
@@ -1,3 +1,4 @@
1
+ import { Autocomplete } from "../../Types";
1
2
  import { LongMonth } from "../../Types/Date";
2
3
  /** '2s' or 2000 */
3
4
  export type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
@@ -5,11 +6,11 @@ export type TimeString = `${number}${TimeUnit}`;
5
6
  export type TimeDelay = number | TimeString;
6
7
  export declare const ValidTime: RegExp;
7
8
  /**
8
- * Converts input into milliseconds
9
+ * Converts input into milliseconds. Supports multiple time units in one string by space separation. E.g. 1h 30m
9
10
  * @param input Input to convert to ms. 1s | 2m | 3h | 1M | 60000
10
11
  * @returns Millisecond value of input
11
12
  */
12
- export declare function ms(input: TimeDelay): number;
13
+ export declare function ms(input: Autocomplete<TimeDelay>): number;
13
14
  /**
14
15
  * Time utility class
15
16
  * @borrows TimeDelay
@@ -35,7 +36,7 @@ export declare class Time {
35
36
  static DoubleDigit(value: number): string;
36
37
  static th(value: number, includeValue?: boolean): string;
37
38
  /**
38
- * Array of names of the months. 0 idnexed
39
+ * Array of names of the months. 0 indexed
39
40
  */
40
41
  static get MonthNames(): LongMonth[];
41
42
  /**
@@ -60,6 +61,6 @@ export declare class Time {
60
61
  static get year(): number;
61
62
  /** Average month in milliseconds */
62
63
  static get avgMonth(): number;
63
- static ms(input: TimeDelay): number;
64
+ static ms(input: Autocomplete<TimeDelay>): number;
64
65
  }
65
66
  export default Time;
@@ -3,11 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Time = exports.ms = exports.ValidTime = void 0;
4
4
  exports.ValidTime = /^(\d+(?:\.|,)?\d*)(ms|s|m|h|d|w|M|y)$/;
5
5
  /**
6
- * Converts input into milliseconds
6
+ * Converts input into milliseconds. Supports multiple time units in one string by space separation. E.g. 1h 30m
7
7
  * @param input Input to convert to ms. 1s | 2m | 3h | 1M | 60000
8
8
  * @returns Millisecond value of input
9
9
  */
10
10
  function ms(input) {
11
+ return input.toString().split(' ').reduce((result, unit) => result += msSingular(unit), 0);
12
+ }
13
+ exports.ms = ms;
14
+ function msSingular(input) {
11
15
  if (typeof input === 'number')
12
16
  return input;
13
17
  const match = input.match(exports.ValidTime);
@@ -28,7 +32,6 @@ function ms(input) {
28
32
  ]);
29
33
  return parseInt(value) * units.get(unit);
30
34
  }
31
- exports.ms = ms;
32
35
  /**
33
36
  * Time utility class
34
37
  * @borrows TimeDelay
@@ -73,10 +76,10 @@ class Time {
73
76
  return includeValue ? value + th : th;
74
77
  }
75
78
  /**
76
- * Array of names of the months. 0 idnexed
79
+ * Array of names of the months. 0 indexed
77
80
  */
78
81
  static get MonthNames() {
79
- return new Array('Janurary', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
82
+ return new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
80
83
  }
81
84
  /**
82
85
  * Array of names of the days of the week. 0 indexed
@@ -1,3 +1,4 @@
1
1
  export * from './Events';
2
2
  export * from './Time';
3
+ export * from './DanhoLogger';
3
4
  export * from './store';
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./Events"), exports);
18
18
  __exportStar(require("./Time"), exports);
19
+ __exportStar(require("./DanhoLogger"), exports);
19
20
  __exportStar(require("./store"), exports);
@@ -1,6 +1,9 @@
1
1
  import { Arrayable } from "../Types";
2
2
  import { EventEmitter } from "./Events";
3
3
  export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State;
4
+ export type Actions<State, ActionTypes extends Record<string, any[]>> = {
5
+ [Action in keyof ActionTypes]?: Arrayable<Reducer<State, ActionTypes, Action>>;
6
+ };
4
7
  /**
5
8
  * EventEmitter, but it stores state and handles state change with reducers
6
9
  *
@@ -64,16 +67,9 @@ export type Reducer<State, Types extends Record<string, any[]>, Action extends k
64
67
  *
65
68
  * ```
66
69
  */
67
- export declare class Store<State extends object, ActionTypes extends Record<string, any[]>, Actions extends {
68
- [Action in keyof ActionTypes]: Array<Reducer<State, ActionTypes, Action>>;
69
- } = {
70
- [Action in keyof ActionTypes]: Array<Reducer<State, ActionTypes, Action>>;
71
- }> extends EventEmitter<Record<keyof Actions, ActionTypes[keyof ActionTypes]> & Record<'stateChange', [previous: State, current: State]>> {
72
- constructor(state: State, actions?: {
73
- [Action in keyof ActionTypes]?: Arrayable<Reducer<State, ActionTypes, Action>>;
74
- });
70
+ export declare class Store<State extends object, ActionTypes extends Record<string, any[]>, StoreActions extends Actions<State, ActionTypes> = Actions<State, ActionTypes>> extends EventEmitter<Record<keyof StoreActions, ActionTypes[keyof ActionTypes]> & Record<'stateChange', [previous: State, current: State]>> {
71
+ constructor(state: State, actions?: StoreActions);
75
72
  private _state;
76
73
  get state(): State;
77
74
  dispatch<Action extends keyof ActionTypes>(action: Action, ...args: ActionTypes[Action]): State;
78
75
  }
79
- export default 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;