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.
- package/dist/Classes/DanhoLogger.d.ts +23 -0
- package/dist/Classes/DanhoLogger.js +65 -0
- package/dist/Classes/Events/EventEmitter.d.ts +1 -1
- package/dist/Classes/Events/EventEmitter.js +1 -1
- package/dist/Classes/Time/Date.d.ts +1 -0
- package/dist/Classes/Time/Date.js +4 -1
- package/dist/Classes/Time/Time.d.ts +5 -4
- package/dist/Classes/Time/Time.js +7 -4
- package/dist/Classes/index.d.ts +1 -0
- package/dist/Classes/index.js +1 -0
- package/dist/Classes/store.d.ts +5 -9
- package/dist/Extensions/Array/array.extension.d.ts +42 -0
- package/dist/Extensions/Array/array.extension.js +57 -0
- package/dist/Extensions/Array/crud.extension.d.ts +24 -0
- package/dist/Extensions/Array/crud.extension.js +28 -0
- package/dist/Extensions/Array/index.d.ts +20 -0
- package/dist/Extensions/Array/index.js +40 -0
- package/dist/Extensions/Array/loop.extension.d.ts +18 -0
- package/dist/Extensions/Array/loop.extension.js +23 -0
- package/dist/Extensions/Array/random.extension.d.ts +23 -0
- package/dist/Extensions/Array/random.extension.js +35 -0
- package/dist/Extensions/Array/sort.extension.d.ts +27 -0
- package/dist/Extensions/Array/sort.extension.js +31 -0
- package/dist/Extensions/Array/string.extension.d.ts +13 -0
- package/dist/Extensions/Array/string.extension.js +14 -0
- package/dist/Extensions/Array.d.ts +17 -3
- package/dist/Extensions/Array.js +0 -12
- package/dist/Extensions/Function.d.ts +17 -2
- package/dist/Extensions/Function.js +15 -2
- package/dist/Extensions/Number.d.ts +13 -0
- package/dist/Extensions/Number.js +40 -0
- package/dist/Extensions/Object/arrays.extension.d.ts +17 -0
- package/dist/Extensions/Object/arrays.extension.js +13 -0
- package/dist/Extensions/Object/booleans.extension.d.ts +18 -0
- package/dist/Extensions/Object/booleans.extension.js +37 -0
- package/dist/Extensions/Object/extracts.extension.d.ts +38 -0
- package/dist/Extensions/Object/extracts.extension.js +72 -0
- package/dist/Extensions/Object/index.d.ts +8 -47
- package/dist/Extensions/Object/index.js +31 -33
- package/dist/Extensions/Object/properties.extension.d.ts +6 -0
- package/dist/Extensions/Object/properties.extension.js +4 -0
- package/dist/Extensions/Object/properties.js +1 -2
- package/dist/Extensions/String/case.extension.d.ts +12 -0
- package/dist/Extensions/String/case.extension.js +55 -0
- package/dist/Extensions/String/index.d.ts +4 -0
- package/dist/Extensions/String/index.js +30 -0
- package/dist/Extensions/index.d.ts +1 -12
- package/dist/Extensions/index.js +1 -9
- package/dist/Types/Able.d.ts +16 -0
- package/dist/Types/Able.js +2 -0
- package/dist/Types/Array.d.ts +6 -0
- package/dist/Types/Array.js +2 -0
- package/dist/Types/C#.d.ts +8 -0
- package/dist/Types/C#.js +2 -0
- package/dist/Types/Date.d.ts +1 -1
- package/dist/Types/Events.d.ts +2 -2
- package/dist/Types/Function.d.ts +5 -0
- package/dist/Types/Function.js +2 -0
- package/dist/Types/Object.d.ts +4 -0
- package/dist/Types/Object.js +2 -0
- package/dist/Types/PropertiesWith.d.ts +21 -0
- package/dist/Types/String.d.ts +1 -0
- package/dist/Types/String.js +2 -0
- package/dist/Types/TransformTypes.d.ts +9 -0
- package/dist/Types/index.d.ts +6 -28
- package/dist/Types/index.js +6 -0
- package/dist/Utils/ApiUtil/ApiTypes.d.ts +15 -0
- package/dist/Utils/ApiUtil/ApiTypes.js +15 -0
- package/dist/Utils/ApiUtil/RequestUtil.d.ts +19 -0
- package/dist/Utils/ApiUtil/RequestUtil.js +73 -0
- package/dist/Utils/ApiUtil/index.d.ts +20 -0
- package/dist/Utils/ApiUtil/index.js +33 -0
- package/dist/Utils/ApiUtils/ApiTypes.d.ts +15 -0
- package/dist/Utils/ApiUtils/ApiTypes.js +15 -0
- package/dist/Utils/ApiUtils/RequestUtil.d.ts +19 -0
- package/dist/Utils/ApiUtils/RequestUtil.js +73 -0
- package/dist/Utils/ApiUtils/index.d.ts +20 -0
- package/dist/Utils/ApiUtils/index.js +33 -0
- package/dist/Utils/ColorUtils.d.ts +11 -0
- package/dist/Utils/ColorUtils.js +93 -0
- package/dist/Utils/FormUtil.d.ts +6 -0
- package/dist/Utils/FormUtil.js +35 -0
- package/dist/Utils/FormUtils.d.ts +6 -0
- package/dist/Utils/FormUtils.js +35 -0
- package/dist/Utils/NumberUtils.d.ts +1 -0
- package/dist/Utils/NumberUtils.js +7 -0
- package/dist/Utils/PatcherUtils.d.ts +6 -0
- package/dist/Utils/PatcherUtils.js +80 -0
- package/dist/Utils/StringUtils.d.ts +3 -0
- package/dist/Utils/StringUtils.js +47 -0
- package/dist/Utils/TimeUtils/debounce.util.d.ts +22 -0
- package/dist/Utils/TimeUtils/debounce.util.js +78 -0
- package/dist/Utils/TimeUtils/functions.util.d.ts +4 -0
- package/dist/Utils/TimeUtils/functions.util.js +21 -0
- package/dist/Utils/TimeUtils/index.d.ts +15 -0
- package/dist/Utils/TimeUtils/index.js +34 -0
- package/dist/Utils/TimeUtils/throttle.util.d.ts +15 -0
- package/dist/Utils/TimeUtils/throttle.util.js +43 -0
- package/dist/Utils/index.d.ts +7 -0
- package/dist/Utils/index.js +23 -0
- package/package.json +4 -2
- package/src/Classes/DanhoLogger.ts +78 -0
- package/src/Classes/Events/Event.ts +96 -96
- package/src/Classes/Events/EventCollection.ts +90 -90
- package/src/Classes/Events/EventEmitter.ts +68 -68
- package/src/Classes/Time/Date.ts +219 -216
- package/src/Classes/Time/Time.ts +109 -104
- package/src/Classes/Time/TimeSpan.ts +171 -171
- package/src/Classes/index.ts +1 -0
- package/src/Classes/store.ts +22 -22
- package/src/Extensions/Array/array.extension.ts +103 -0
- package/src/Extensions/Array/crud.extension.ts +46 -0
- package/src/Extensions/Array/index.ts +15 -0
- package/src/Extensions/Array/loop.extension.ts +38 -0
- package/src/Extensions/Array/random.extension.ts +56 -0
- package/src/Extensions/Array/sort.extension.ts +52 -0
- package/src/Extensions/Array/string.extension.ts +22 -0
- package/src/Extensions/Document.ts +39 -39
- package/src/Extensions/Function.ts +37 -10
- package/src/Extensions/Map.ts +56 -56
- package/src/Extensions/Number.ts +50 -0
- package/src/Extensions/Object/arrays.extension.ts +27 -0
- package/src/Extensions/Object/booleans.extension.ts +46 -0
- package/src/Extensions/Object/extracts.extension.ts +102 -0
- package/src/Extensions/Object/index.ts +9 -80
- package/src/Extensions/Object/properties.extension.ts +11 -0
- package/src/Extensions/Object/properties.ts +35 -36
- package/src/Extensions/String/case.extension.ts +95 -0
- package/src/Extensions/String/index.ts +5 -0
- package/src/Extensions/index.ts +2 -20
- package/src/Interfaces/ElementOptions.ts +7 -7
- package/src/Interfaces/IReplacement.ts +2 -2
- package/src/Types/Able.ts +22 -0
- package/src/Types/Array.ts +7 -0
- package/src/Types/C#.ts +9 -0
- package/src/Types/Date.ts +1 -1
- package/src/Types/Events.ts +12 -12
- package/src/Types/Function.ts +10 -0
- package/src/Types/Object.ts +4 -0
- package/src/Types/PropertiesWith.ts +35 -4
- package/src/Types/String.ts +1 -0
- package/src/Types/TransformTypes.ts +23 -5
- package/src/Types/index.ts +7 -41
- package/src/Utils/ApiUtils/ApiTypes.ts +43 -0
- package/src/Utils/ApiUtils/RequestUtil.ts +87 -0
- package/src/Utils/ApiUtils/index.ts +39 -0
- package/src/Utils/ColorUtils.ts +102 -0
- package/src/Utils/FormUtils.ts +33 -0
- package/src/Utils/NumberUtils.ts +3 -0
- package/src/Utils/PatcherUtils.ts +111 -0
- package/src/Utils/StringUtils.ts +44 -0
- package/src/Utils/TimeUtils/debounce.util.ts +85 -0
- package/src/Utils/TimeUtils/functions.util.ts +18 -0
- package/src/Utils/TimeUtils/index.ts +9 -0
- package/src/Utils/TimeUtils/throttle.util.ts +44 -0
- package/src/Utils/index.ts +8 -0
- package/src/Extensions/Array.ts +0 -95
- package/src/Extensions/String.ts +0 -54
package/src/Classes/store.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Arrayable, TransformType } from "../Types";
|
|
2
2
|
import { EventEmitter } from "./Events";
|
|
3
3
|
|
|
4
|
-
export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State
|
|
5
|
-
export type Actions<State, ActionTypes extends Record<string, any[]>> = {
|
|
6
|
-
|
|
4
|
+
export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State;
|
|
5
|
+
export type Actions<State, ActionTypes extends Record<string, any[]>> = {
|
|
6
|
+
[Action in keyof ActionTypes]?: Arrayable<Reducer<State, ActionTypes, Action>>
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -70,29 +70,29 @@ export type Actions<State, ActionTypes extends Record<string, any[]>> = {
|
|
|
70
70
|
* ```
|
|
71
71
|
*/
|
|
72
72
|
export class Store<
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
State extends object,
|
|
74
|
+
ActionTypes extends Record<string, any[]>,
|
|
75
|
+
StoreActions extends Actions<State, ActionTypes> = Actions<State, ActionTypes>
|
|
76
76
|
> extends EventEmitter<
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
Record<keyof StoreActions, ActionTypes[keyof ActionTypes]>
|
|
78
|
+
& Record<'stateChange', [previous: State, current: State]>
|
|
79
79
|
> {
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
constructor(state: State, actions: StoreActions = {} as StoreActions) {
|
|
81
|
+
super(new Map(...Object.entries(actions).map(([action, reducers]) => [action, reducers as any])));
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
this._state = state;
|
|
84
|
+
}
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
private _state: State;
|
|
87
|
+
public get state(): State {
|
|
88
|
+
return this._state;
|
|
89
|
+
}
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
public dispatch<Action extends keyof ActionTypes>(action: Action, ...args: ActionTypes[Action]): State {
|
|
92
|
+
const previous = { ...this._state };
|
|
93
|
+
this._state = super.emit<State, Action>(action, ...args as any).reduce((state, returned) => ({ ...state, ...returned }), this.state);
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
super.emit<void, 'stateChange'>('stateChange', ...[previous, this.state] as any);
|
|
96
|
+
return this.state;
|
|
97
|
+
}
|
|
98
98
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Arrayable } from "../../Types";
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Array<T> {
|
|
5
|
+
/**
|
|
6
|
+
* Returns the first `count` elements from the array.
|
|
7
|
+
* @param count Number of elements to take.
|
|
8
|
+
* @returns Array containing the first `count` elements.
|
|
9
|
+
*/
|
|
10
|
+
take(count: number): Array<T>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns a new array with only unique elements from the original array.
|
|
14
|
+
* @returns An array containing only unique elements.
|
|
15
|
+
*/
|
|
16
|
+
unique(): Array<T>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Splits the array into chunks of a specified size or by a splitter function.
|
|
20
|
+
* @param chunkSizeOrSplitter The size of each chunk or a function that determines where to split the array.
|
|
21
|
+
* @returns An array of arrays, where each sub-array is a chunk of the original array.
|
|
22
|
+
*/
|
|
23
|
+
splitBy(chunkSizeOrSplitter: number | ((value: T, index: number, array: Array<T>) => boolean)): Array<Array<T>>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Groups the elements of the array based on a key selector function.
|
|
27
|
+
* @param keySelector A function that selects a key for each element in the array.
|
|
28
|
+
* @returns A Map where each key is a group identifier and the value is an array of elements in that group.
|
|
29
|
+
*/
|
|
30
|
+
groupBy<K>(keySelector: (value: T, index: number, array: Array<T>) => K): Map<K, Array<T>>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ArrayConstructor {
|
|
34
|
+
/**
|
|
35
|
+
* Forces an arrayable object into an array
|
|
36
|
+
* @param arrayable The arrayable object to force into an array
|
|
37
|
+
* @returns An array containing the elements of the input or the single value.
|
|
38
|
+
*/
|
|
39
|
+
forceArray<T>(arrayable: Arrayable<T>): Array<T>;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function take<T>(this: Array<T>, count: number): Array<T> {
|
|
44
|
+
return this.slice(0, count);
|
|
45
|
+
}
|
|
46
|
+
Array.prototype.take = take;
|
|
47
|
+
|
|
48
|
+
export function forceArray<T>(arrayable: Arrayable<T>): Array<T> {
|
|
49
|
+
return Array.isArray(arrayable) ? arrayable : [arrayable];
|
|
50
|
+
}
|
|
51
|
+
Array.forceArray = forceArray;
|
|
52
|
+
|
|
53
|
+
export function unique<T>(this: Array<T>): Array<T> {
|
|
54
|
+
return [...new Set(this)];
|
|
55
|
+
}
|
|
56
|
+
Array.prototype.unique = unique;
|
|
57
|
+
|
|
58
|
+
export function splitBy<T>(this: Array<T>, chunkSize: number): Array<Array<T>>;
|
|
59
|
+
export function splitBy<T>(this: Array<T>, splitter: (value: T, index: number, array: Array<T>) => boolean): Array<Array<T>>;
|
|
60
|
+
export function splitBy<T>(this: Array<T>, chunkSizeOrSplitter: number | ((value: T, index: number, array: Array<T>) => boolean)): Array<Array<T>> {
|
|
61
|
+
const chunkSize = typeof chunkSizeOrSplitter === 'number' ? chunkSizeOrSplitter : undefined;
|
|
62
|
+
const splitter = typeof chunkSizeOrSplitter === 'function' ? chunkSizeOrSplitter : undefined;
|
|
63
|
+
|
|
64
|
+
const result: Array<Array<T>> = [];
|
|
65
|
+
|
|
66
|
+
if (chunkSize !== undefined) {
|
|
67
|
+
for (let i = 0; i < this.length; i += chunkSize) {
|
|
68
|
+
result.push(this.slice(i, i + chunkSize));
|
|
69
|
+
}
|
|
70
|
+
} else if (splitter !== undefined) {
|
|
71
|
+
const chunk = new Array<T>();
|
|
72
|
+
|
|
73
|
+
for (let i = 0; i < this.length; i++) {
|
|
74
|
+
const value = this[i];
|
|
75
|
+
if (splitter(value, i, this)) {
|
|
76
|
+
if (chunk.length) {
|
|
77
|
+
result.push(chunk.splice(0, chunk.length));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
chunk.push(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (chunk.length) {
|
|
85
|
+
result.push(chunk);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
Array.prototype.splitBy = splitBy;
|
|
92
|
+
|
|
93
|
+
export function groupBy<T, K>(this: Array<T>, keySelector: (value: T, index: number, array: Array<T>) => K): Map<K, Array<T>> {
|
|
94
|
+
return this.reduce((map, item, index, array) => {
|
|
95
|
+
const key = keySelector(item, index, array);
|
|
96
|
+
const group = map.get(key) ?? new Array<T>();
|
|
97
|
+
|
|
98
|
+
group.push(item);
|
|
99
|
+
map.set(key, group);
|
|
100
|
+
return map;
|
|
101
|
+
}, new Map<K, Array<T>>());
|
|
102
|
+
}
|
|
103
|
+
Array.prototype.groupBy = groupBy;
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
}
|
|
23
|
+
|
|
24
|
+
export function add<T>(this: Array<T>, ...items: Array<T>) {
|
|
25
|
+
this.push(...items);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
Array.prototype.add = add;
|
|
29
|
+
|
|
30
|
+
export function update<T>(this: Array<T>, old: T | number | UpdateFinder<T>, updated: T) {
|
|
31
|
+
const item = typeof old === 'number' ? this[old]
|
|
32
|
+
: typeof old === 'function' ? this.find(old as UpdateFinder<T>)
|
|
33
|
+
: old;
|
|
34
|
+
if (!item) throw new Error('Old was not found in array!');
|
|
35
|
+
|
|
36
|
+
const index = this.indexOf(item);
|
|
37
|
+
return this[index] = updated;
|
|
38
|
+
}
|
|
39
|
+
Array.prototype.update = update;
|
|
40
|
+
|
|
41
|
+
export function remove<T>(this: Array<T>, value: T | number): Array<T> {
|
|
42
|
+
const index = typeof value === 'number' ? value : this.indexOf(value);
|
|
43
|
+
if (index > -1) this.splice(index, 1);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
Array.prototype.remove = remove;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as Array from './array.extension';
|
|
2
|
+
import * as CRUD from './crud.extension';
|
|
3
|
+
import * as Loop from './loop.extension';
|
|
4
|
+
import * as Random from './random.extension';
|
|
5
|
+
import * as Sort from './sort.extension';
|
|
6
|
+
import * as String from './string.extension';
|
|
7
|
+
|
|
8
|
+
export const ArrayExtensions = {
|
|
9
|
+
...Array,
|
|
10
|
+
...CRUD,
|
|
11
|
+
...Loop,
|
|
12
|
+
...Random,
|
|
13
|
+
...Sort,
|
|
14
|
+
...String,
|
|
15
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
/**
|
|
14
|
+
* For every x in array, execute callback
|
|
15
|
+
* @param every i.e every 2nd item in array
|
|
16
|
+
* @param callback Function to execute. This includes a collection of items prior to last callback run
|
|
17
|
+
* @returns Array of results
|
|
18
|
+
*/
|
|
19
|
+
export function nth<T, U>(
|
|
20
|
+
this: Array<T>,
|
|
21
|
+
every: number,
|
|
22
|
+
callback: (item: T, index: number, collection: Array<T>, self: Array<T>) => U
|
|
23
|
+
): Array<U> {
|
|
24
|
+
const result = new Array<U>();
|
|
25
|
+
let collection = new Array<T>();
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < this.length; i++) {
|
|
28
|
+
collection.push(this[i]);
|
|
29
|
+
|
|
30
|
+
if ((i + 1) % every === 0) {
|
|
31
|
+
result.push(callback(this[i], i, collection, this));
|
|
32
|
+
collection = new Array<T>();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
Array.prototype.nth = nth;
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
/**
|
|
10
|
+
* Returns a new array with the elements of the original array in random order.
|
|
11
|
+
* @returns A new array with the elements shuffled.
|
|
12
|
+
*/
|
|
13
|
+
shuffle(): Array<T>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Selects a random item from an array based on specified weights.
|
|
17
|
+
* @param items An array of tuples where each tuple contains an item and its corresponding weight.
|
|
18
|
+
* @returns A randomly selected item based on the provided weights.
|
|
19
|
+
*/
|
|
20
|
+
randomWithPercentages(items: [item: T, weight: number][]): T;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function random<T>(this: Array<T>): T {
|
|
25
|
+
const randomIndex = Math.floor(Math.random() * this.length);
|
|
26
|
+
return this[randomIndex];
|
|
27
|
+
}
|
|
28
|
+
Array.prototype.random = random;
|
|
29
|
+
|
|
30
|
+
export function shuffle<T>(this: Array<T>): Array<T> {
|
|
31
|
+
return this.sort(() => Math.random() - 0.5);
|
|
32
|
+
}
|
|
33
|
+
Array.prototype.shuffle = shuffle;
|
|
34
|
+
|
|
35
|
+
export function randomWithPercentages<T>(items: [item: T, weight: number][]): T {
|
|
36
|
+
if (items.length === 0) throw new Error('Items array cannot be empty');
|
|
37
|
+
|
|
38
|
+
// Calculate total weight in case weights don't sum to 100
|
|
39
|
+
const totalWeight = items.reduce((sum, [, weight]) => sum + weight, 0);
|
|
40
|
+
if (totalWeight === 0) throw new Error('Total weight must be greater than zero');
|
|
41
|
+
|
|
42
|
+
// Generate random number between 0 and totalWeight
|
|
43
|
+
const random = Math.random() * totalWeight;
|
|
44
|
+
|
|
45
|
+
// Find the item that corresponds to this random value
|
|
46
|
+
let currentWeight = 0;
|
|
47
|
+
for (const [item, weight] of items) {
|
|
48
|
+
currentWeight += weight;
|
|
49
|
+
if (random <= currentWeight) {
|
|
50
|
+
return item;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throw new Error('Unable to select an item based on weights');
|
|
55
|
+
}
|
|
56
|
+
Array.prototype.randomWithPercentages = randomWithPercentages;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
type Comparator<T> = (a: T, b: T) => number;
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Array<T> {
|
|
5
|
+
/**
|
|
6
|
+
* Sorts the array using multiple comparators.
|
|
7
|
+
* @param comparators Functions to compare elements.
|
|
8
|
+
* @returns Sorted array.
|
|
9
|
+
*/
|
|
10
|
+
orderBy(...comparators: Array<Comparator<T>>): Array<T>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sorts the array using multiple comparators in descending order.
|
|
14
|
+
* @param comparators Functions to compare elements.
|
|
15
|
+
* @returns Sorted array in descending order.
|
|
16
|
+
*/
|
|
17
|
+
orderByDescending(...comparators: Array<Comparator<T>>): Array<T>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sorts the array by specified properties.
|
|
21
|
+
* @param properties Properties to sort by.
|
|
22
|
+
* @returns Sorted array.
|
|
23
|
+
*/
|
|
24
|
+
sortByProperty(...properties: Array<keyof T>): Array<T>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function orderBy<T>(this: Array<T>, ...comparators: Array<Comparator<T>>): Array<T> {
|
|
29
|
+
return this.sort((a, b) => {
|
|
30
|
+
for (const comparator of comparators) {
|
|
31
|
+
const result = comparator(a, b);
|
|
32
|
+
if (result !== 0) return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return 0;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
Array.prototype.orderBy = orderBy;
|
|
39
|
+
|
|
40
|
+
export function orderByDescending<T>(this: Array<T>, ...comparators: Array<Comparator<T>>): Array<T> {
|
|
41
|
+
return this.orderBy(...comparators).reverse();
|
|
42
|
+
}
|
|
43
|
+
Array.prototype.orderByDescending = orderByDescending;
|
|
44
|
+
|
|
45
|
+
export function sortByProperty<T extends object>(this: Array<T>, ...properties: Array<keyof T>): Array<T> {
|
|
46
|
+
return this.orderBy(...properties.map(property => (a: T, b: T) => {
|
|
47
|
+
if (a[property] < b[property]) return -1;
|
|
48
|
+
if (a[property] > b[property]) return 1;
|
|
49
|
+
return 0;
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
Array.prototype.sortByProperty = sortByProperty;
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
|
|
14
|
+
export function join<T>(this: Array<T>, separator = ',', endSeparator = '&'): string {
|
|
15
|
+
const validArgs = this.filter(arg => !Object.isNullOrUndefined(arg) && arg !== '');
|
|
16
|
+
if (!validArgs.length) return '';
|
|
17
|
+
if (validArgs.length === 1) return validArgs.shift()!.toString();
|
|
18
|
+
|
|
19
|
+
const lastArg = validArgs.pop()!;
|
|
20
|
+
const combinedArgs = validArgs.join(separator);
|
|
21
|
+
return `${combinedArgs}${endSeparator ? ` ${endSeparator} ` : ''}${lastArg}`;
|
|
22
|
+
}
|
|
@@ -1,58 +1,58 @@
|
|
|
1
1
|
import { IElement } from "../Types";
|
|
2
2
|
|
|
3
3
|
declare global {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
4
|
+
interface Document {
|
|
5
|
+
/**
|
|
6
|
+
* Creates an element like Document#createElement, however with construction options to assign values in construction instead of after construction.
|
|
7
|
+
* @param tagName HTMLElement tag name
|
|
8
|
+
* @param options Construction options, instead of assigning values after construction
|
|
9
|
+
* @param children Child elements
|
|
10
|
+
*/
|
|
11
|
+
createProperElement<Tag extends keyof HTMLElementTagNameMap>(tagName: Tag, options?: Partial<HTMLElementTagNameMap[Tag]>, ...children: Array<IElement>): HTMLElementTagNameMap[Tag];
|
|
12
|
+
createProperElement<Tag extends keyof HTMLElementTagNameMap>(tagName: Tag, ...children: Array<IElement>): HTMLElementTagNameMap[Tag];
|
|
13
|
+
createElementFromString<K extends keyof HTMLElementTagNameMap>(html: string, parentTag?: K): HTMLElementTagNameMap[K];
|
|
14
|
+
}
|
|
15
|
+
interface HTMLCollection {
|
|
16
|
+
/**
|
|
17
|
+
* Converts HTMLCollection to Element[]
|
|
18
|
+
*/
|
|
19
|
+
array(): Element[];
|
|
20
|
+
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function createElement<Tag extends keyof HTMLElementTagNameMap>(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
this: Document, tagName: Tag,
|
|
25
|
+
options?: Partial<HTMLElementTagNameMap[Tag]> | string,
|
|
26
|
+
...children: Array<IElement>
|
|
27
27
|
): HTMLElementTagNameMap[Tag] {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
|
|
29
|
+
const element = Object.assign(document.createElement(tagName), typeof options === 'string' ? {} : options);
|
|
30
|
+
children ??= typeof options === 'string' ? [options] as Array<IElement> : [];
|
|
31
|
+
typeof options === 'string' && children.unshift(options);
|
|
32
|
+
|
|
33
|
+
if (!children.length) return element;
|
|
34
|
+
else if (typeof children === 'string') element.innerHTML = children;
|
|
35
|
+
else if (children instanceof Array) children.forEach(child => (
|
|
36
|
+
typeof child === 'string' ?
|
|
37
|
+
element.innerHTML += child :
|
|
38
|
+
element.appendChild(child)
|
|
39
|
+
));
|
|
40
|
+
else element.appendChild(children);
|
|
41
|
+
return element;
|
|
42
42
|
|
|
43
43
|
}
|
|
44
44
|
Document.prototype.createProperElement = createElement;
|
|
45
45
|
|
|
46
46
|
function createElementFromString<Tag extends keyof HTMLElementTagNameMap>(this: Document, html: string, tag?: Tag): HTMLElementTagNameMap[Tag] {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (!html.startsWith(`<${tag}`)) html = `<${tag}>${html}</${tag}>`;
|
|
48
|
+
return new DOMParser().parseFromString(html, 'text/html').body.firstElementChild as HTMLElementTagNameMap[Tag];
|
|
49
49
|
}
|
|
50
50
|
Document.prototype.createElementFromString = createElementFromString;
|
|
51
51
|
|
|
52
|
-
HTMLCollection.prototype.array = function(this: HTMLCollection) {
|
|
53
|
-
|
|
52
|
+
HTMLCollection.prototype.array = function (this: HTMLCollection) {
|
|
53
|
+
return Array.from(this);
|
|
54
54
|
};
|
|
55
55
|
|
|
56
56
|
export const DocumentExtensions = {
|
|
57
|
-
|
|
57
|
+
createElement, createElementFromString
|
|
58
58
|
};
|
|
@@ -1,18 +1,45 @@
|
|
|
1
|
+
import { Functionable } from "../Types";
|
|
2
|
+
|
|
1
3
|
declare global {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
interface Function {
|
|
5
|
+
/**
|
|
6
|
+
* Checks if obj is a function
|
|
7
|
+
* @param obj Object to check
|
|
8
|
+
*/
|
|
9
|
+
is(obj: any): obj is Function;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a Functionable<T> to T, if it's a function, it will be called with the provided arguments and its return value will be returned, otherwise the value itself will be returned.
|
|
13
|
+
* @param functionable The Functionable<T> to resolve
|
|
14
|
+
* @param args Arguments to call the function with if functionable is a function
|
|
15
|
+
*/
|
|
16
|
+
resolveFunctionable<T, TArgs extends any[] = any[]>(functionable: Functionable<T, TArgs>, ...args: TArgs): T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Forces a Functionable<T> to be a function, if it's already a function, it will be returned as is, otherwise a function that returns the value will be returned.
|
|
20
|
+
* @param functionable The Functionable<T> to force to be a function
|
|
21
|
+
*/
|
|
22
|
+
forceFunction<T, TArgs extends any[] = any[]>(functionable: Functionable<T, TArgs>): (...args: TArgs) => T;
|
|
23
|
+
}
|
|
9
24
|
}
|
|
10
25
|
|
|
11
|
-
function is(obj: any): obj is Function {
|
|
12
|
-
|
|
26
|
+
export function is(obj: any): obj is Function {
|
|
27
|
+
return typeof obj === 'function';
|
|
13
28
|
}
|
|
14
29
|
Function.is = is;
|
|
15
30
|
|
|
31
|
+
export function resolveFunctionable<T, TArgs extends any[] = any[]>(functionable: Functionable<T, TArgs>, ...args: TArgs): T {
|
|
32
|
+
return is(functionable) ? functionable(...args) : functionable;
|
|
33
|
+
}
|
|
34
|
+
Function.resolveFunctionable = resolveFunctionable;
|
|
35
|
+
|
|
36
|
+
export function forceFunction<T, TArgs extends any[] = any[]>(functionable: Functionable<T, TArgs>): (...args: TArgs) => T {
|
|
37
|
+
return is(functionable) ? functionable : () => functionable;
|
|
38
|
+
}
|
|
39
|
+
Function.forceFunction = forceFunction;
|
|
40
|
+
|
|
16
41
|
export const FunctionExtensions = {
|
|
17
|
-
|
|
42
|
+
is,
|
|
43
|
+
resolveFunctionable,
|
|
44
|
+
forceFunction
|
|
18
45
|
};
|