danholibraryjs 1.7.0 → 1.10.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/.gitattributes +2 -2
- package/dist/Classes/Events/Event.js +1 -1
- package/dist/Classes/Events/EventCollection.js +1 -1
- package/dist/Classes/Events/EventEmitter.d.ts +28 -4
- package/dist/Classes/Events/EventEmitter.js +24 -0
- package/dist/Classes/Time/Date.d.ts +5 -5
- package/dist/Classes/Time/Time.d.ts +3 -3
- package/dist/Classes/Time/TimeProperties.d.ts +2 -2
- package/dist/Classes/Time/TimeSpan.d.ts +1 -1
- package/dist/Classes/index.d.ts +1 -0
- package/dist/Classes/index.js +1 -0
- package/dist/Classes/store.d.ts +79 -0
- package/dist/Classes/store.js +84 -0
- package/dist/Extensions/Array.d.ts +8 -1
- package/dist/Extensions/Array.js +47 -10
- package/dist/Extensions/Document.d.ts +27 -0
- package/dist/Extensions/Document.js +32 -0
- package/dist/Extensions/Function.d.ts +14 -0
- package/dist/Extensions/Function.js +10 -0
- package/dist/Extensions/Map.d.ts +17 -1
- package/dist/Extensions/Map.js +24 -13
- package/dist/Extensions/Object/index.d.ts +49 -0
- package/dist/Extensions/Object/index.js +38 -0
- package/dist/Extensions/Object/properties.d.ts +28 -0
- package/dist/Extensions/Object/properties.js +20 -0
- package/dist/Extensions/String.d.ts +11 -1
- package/dist/Extensions/String.js +15 -7
- package/dist/Extensions/index.d.ts +5 -17
- package/dist/Extensions/index.js +8 -49
- package/dist/Functions/GetCSSProperty.d.ts +1 -1
- package/dist/Functions/GetNestedProperty.d.ts +9 -0
- package/dist/Functions/GetNestedProperty.js +23 -0
- package/dist/Functions/index.d.ts +1 -0
- package/dist/Functions/index.js +1 -0
- package/dist/Interfaces/ElementOptions.d.ts +4 -4
- package/dist/Types/BetterTypes.d.ts +3 -3
- package/dist/Types/Date.d.ts +6 -6
- package/dist/Types/Events.d.ts +2 -2
- package/dist/Types/PropertiesWith.d.ts +2 -2
- package/dist/Types/TransformTypes.d.ts +2 -2
- package/dist/Types/index.d.ts +21 -6
- package/docs/Classes.md +33 -0
- package/docs/Extensions.md +7 -0
- package/docs/Functions.md +8 -0
- package/docs/Types.md +30 -17
- package/package.json +6 -3
- package/src/Classes/Events/Event.ts +1 -1
- package/src/Classes/Events/EventCollection.ts +1 -1
- package/src/Classes/Events/EventEmitter.ts +40 -5
- package/src/Classes/index.ts +2 -1
- package/src/Classes/store.ts +98 -0
- package/src/Extensions/Array.ts +49 -11
- package/src/Extensions/Document.ts +58 -0
- package/src/Extensions/Function.ts +18 -0
- package/src/Extensions/Map.ts +24 -9
- package/src/Extensions/Object/index.ts +82 -0
- package/src/Extensions/Object/properties.ts +51 -0
- package/src/Extensions/String.ts +17 -6
- package/src/Extensions/index.ts +7 -66
- package/src/Functions/GetNestedProperty.ts +29 -0
- package/src/Functions/index.ts +1 -0
- package/src/Interfaces/ElementOptions.ts +2 -2
- package/src/Types/index.ts +27 -2
- package/tsconfig.json +2 -2
- package/Time.xlsx +0 -0
- package/dist/Extensions/Object.d.ts +0 -16
- package/dist/Extensions/Object.js +0 -8
- package/src/Extensions/Object.ts +0 -25
package/.gitattributes
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# Auto detect text files and perform LF normalization
|
|
2
|
-
* text=auto
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
|
+
* text=auto
|
|
@@ -53,7 +53,7 @@ class Event {
|
|
|
53
53
|
*/
|
|
54
54
|
on(listener, prepend = false) {
|
|
55
55
|
if (this.limit > 0 && this._listeners.length + 1 > this.limit) {
|
|
56
|
-
throw new Error(`Event limit, ${this.limit}, reached for event ${this.name}!`);
|
|
56
|
+
throw new Error(`Event limit, ${this.limit}, reached for event ${this.name.toString()}!`);
|
|
57
57
|
}
|
|
58
58
|
if (prepend)
|
|
59
59
|
this._listeners = [listener, ...this._listeners];
|
|
@@ -99,7 +99,7 @@ class EventCollection {
|
|
|
99
99
|
}
|
|
100
100
|
const event = this.get(eventName);
|
|
101
101
|
if (!event)
|
|
102
|
-
throw new Error(`Unknown event, ${eventName}!`);
|
|
102
|
+
throw new Error(`Unknown event, ${eventName.toString()}!`);
|
|
103
103
|
event.limit = limit;
|
|
104
104
|
this._events.set(eventName, event);
|
|
105
105
|
return this;
|
|
@@ -4,6 +4,30 @@ import { BaseEvent, EventHandler } from '../../Types';
|
|
|
4
4
|
* @borrows EventCollection
|
|
5
5
|
* @borrows BaseEvent
|
|
6
6
|
* @borrows EventHandler
|
|
7
|
+
*
|
|
8
|
+
* @example ```ts
|
|
9
|
+
* import { EventEmitter } from 'danholibraryjs';
|
|
10
|
+
*
|
|
11
|
+
* type EventTypes = {
|
|
12
|
+
* create: [username: string, password: string],
|
|
13
|
+
* update: [id: string, user: User],
|
|
14
|
+
* delete: [id: string, reason?: string],
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* const emitter = new EventEmitter<EventTypes>(new Map([
|
|
18
|
+
* ['create', (username, password) => {
|
|
19
|
+
* return { username, password };
|
|
20
|
+
* }],
|
|
21
|
+
* ['update', (id, user) => {
|
|
22
|
+
* return { id, ...user };
|
|
23
|
+
* }]
|
|
24
|
+
* ]));
|
|
25
|
+
*
|
|
26
|
+
* const onDelete = (id: string, reason?: string) => console.log(`User ${id} was deleted because ${reason}`);
|
|
27
|
+
* emitter.on('delete', onDelete);
|
|
28
|
+
* emitter.emit('delete', '1', 'No longer needed');
|
|
29
|
+
* emitter.off('delete', onDelete);
|
|
30
|
+
* ```
|
|
7
31
|
*/
|
|
8
32
|
export declare class EventEmitter<Events extends BaseEvent<string, Array<any>>> {
|
|
9
33
|
/**@param events Map<name: string, handlers: EventHandler[]>*/
|
|
@@ -16,21 +40,21 @@ export declare class EventEmitter<Events extends BaseEvent<string, Array<any>>>
|
|
|
16
40
|
* @param listener Callback function to run, when event occurs
|
|
17
41
|
* @returns this
|
|
18
42
|
*/
|
|
19
|
-
on<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>):
|
|
43
|
+
on<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>): EventEmitter<Events>;
|
|
20
44
|
/**
|
|
21
45
|
* Adds listener to event collection, and runs listener once when event is emitted
|
|
22
46
|
* @param event Event to handle
|
|
23
47
|
* @param listener Callback function to run, when event occurs
|
|
24
48
|
* @returns this
|
|
25
49
|
*/
|
|
26
|
-
once<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>):
|
|
50
|
+
once<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>): EventEmitter<Events>;
|
|
27
51
|
/**
|
|
28
52
|
* Removes listener(s) from event
|
|
29
53
|
* @param event Event to get collection of listeners | "all"
|
|
30
54
|
* @param listener If left null, removes all listeners tied to event, else only removes listener from event
|
|
31
55
|
* @returns this
|
|
32
56
|
*/
|
|
33
|
-
off<Return extends any, Event extends keyof Events>(event?: Event | 'all', listener?: EventHandler<Events, Event, Return>):
|
|
57
|
+
off<Return extends any, Event extends keyof Events>(event?: Event | 'all', listener?: EventHandler<Events, Event, Return>): EventEmitter<Events>;
|
|
34
58
|
/**
|
|
35
59
|
* Emits event and runs all listeners tied to event
|
|
36
60
|
* @param event Event to emit
|
|
@@ -45,6 +69,6 @@ export declare class EventEmitter<Events extends BaseEvent<string, Array<any>>>
|
|
|
45
69
|
* @param limit Limit of events to keep. If you want to limit amount of events saved, use 'all'.
|
|
46
70
|
* @returns this with the new limit
|
|
47
71
|
*/
|
|
48
|
-
limit<Event extends keyof Events>(event: 'all' | Event, limit: number):
|
|
72
|
+
limit<Event extends keyof Events>(event: 'all' | Event, limit: number): EventEmitter<Events>;
|
|
49
73
|
}
|
|
50
74
|
export default EventEmitter;
|
|
@@ -10,6 +10,30 @@ const EventCollection_1 = __importDefault(require("./EventCollection"));
|
|
|
10
10
|
* @borrows EventCollection
|
|
11
11
|
* @borrows BaseEvent
|
|
12
12
|
* @borrows EventHandler
|
|
13
|
+
*
|
|
14
|
+
* @example ```ts
|
|
15
|
+
* import { EventEmitter } from 'danholibraryjs';
|
|
16
|
+
*
|
|
17
|
+
* type EventTypes = {
|
|
18
|
+
* create: [username: string, password: string],
|
|
19
|
+
* update: [id: string, user: User],
|
|
20
|
+
* delete: [id: string, reason?: string],
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* const emitter = new EventEmitter<EventTypes>(new Map([
|
|
24
|
+
* ['create', (username, password) => {
|
|
25
|
+
* return { username, password };
|
|
26
|
+
* }],
|
|
27
|
+
* ['update', (id, user) => {
|
|
28
|
+
* return { id, ...user };
|
|
29
|
+
* }]
|
|
30
|
+
* ]));
|
|
31
|
+
*
|
|
32
|
+
* const onDelete = (id: string, reason?: string) => console.log(`User ${id} was deleted because ${reason}`);
|
|
33
|
+
* emitter.on('delete', onDelete);
|
|
34
|
+
* emitter.emit('delete', '1', 'No longer needed');
|
|
35
|
+
* emitter.off('delete', onDelete);
|
|
36
|
+
* ```
|
|
13
37
|
*/
|
|
14
38
|
class EventEmitter {
|
|
15
39
|
/**@param events Map<name: string, handlers: EventHandler[]>*/
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { BetterOmit, PartialExcept, LongMonth, LongDay, ShortDay, ShortMonth } from "../../Types";
|
|
2
2
|
import TimeProperties from "./TimeProperties";
|
|
3
3
|
import TimeSpan, { TimeSpanFormat } from "./TimeSpan";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
type Data = PartialExcept<BetterOmit<TimeProperties<true>, 'weeks'>, 'years' | 'months'>;
|
|
5
|
+
type Double = `${number}${number}`;
|
|
6
|
+
type Quadruple = `${Double}${Double}`;
|
|
7
|
+
type DateFormat = `${Double}/${Double}/${Quadruple}`;
|
|
8
8
|
/**
|
|
9
9
|
* Type used to construct DanhoDate.
|
|
10
10
|
* @Data Partial TimeProperties except years & months
|
|
11
11
|
* @DateFormat string as dd/MM/yyyy
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
13
|
+
export type DanhoDateConstructor = Data | DateFormat | number | Date;
|
|
14
14
|
declare class DanhoDate {
|
|
15
15
|
/**
|
|
16
16
|
* Returns the value of the current irl time
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { LongMonth } from "../../Types/Date";
|
|
2
2
|
/** '2s' or 2000 */
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
3
|
+
export type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
|
|
4
|
+
export type TimeString = `${number}${TimeUnit}`;
|
|
5
|
+
export type TimeDelay = number | TimeString;
|
|
6
6
|
export declare const ValidTime: RegExp;
|
|
7
7
|
/**
|
|
8
8
|
* Converts input into milliseconds
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
type TimeKeys = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
|
|
2
|
+
export type TimeProperties<Plural extends boolean = false> = Record<Plural extends true ? `${TimeKeys}s` : TimeKeys, number>;
|
|
3
3
|
export default TimeProperties;
|
|
@@ -4,7 +4,7 @@ import TimeProperties from "./TimeProperties";
|
|
|
4
4
|
/**
|
|
5
5
|
* What properties to include when using TimeSpan.toString(format: TimeSpanFormat): string
|
|
6
6
|
*/
|
|
7
|
-
export
|
|
7
|
+
export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>;
|
|
8
8
|
/**
|
|
9
9
|
* Timespan between 2 dates.
|
|
10
10
|
* @borrows TimeSpanValue
|
package/dist/Classes/index.d.ts
CHANGED
package/dist/Classes/index.js
CHANGED
|
@@ -16,3 +16,4 @@ 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("./store"), exports);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Arrayable } from "../Types";
|
|
2
|
+
import { EventEmitter } from "./Events";
|
|
3
|
+
export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State;
|
|
4
|
+
/**
|
|
5
|
+
* EventEmitter, but it stores state and handles state change with reducers
|
|
6
|
+
*
|
|
7
|
+
* @Initialization Actions & initial state must be defined in type parameters. InitialState must be provided in constructor, whereas reducer is optional.
|
|
8
|
+
* The ActionType must have properties as strings and values as arrays.
|
|
9
|
+
*
|
|
10
|
+
* @HandlingActions Reducers can be added through constructor or using Store.on('action', reducer) or Store.once('action', reducer).
|
|
11
|
+
* Every state change must return the next state, apart from 'stateChange', which returns void/any
|
|
12
|
+
* Emit/Dispatch an action using Store.dispatch('action', ...args), ...args being the parameters from the ActionType.
|
|
13
|
+
* Store.emit should NOT be used, as it doesn't update the Store's state.
|
|
14
|
+
*
|
|
15
|
+
* Reducer functions can be removed using Store.off('action', reducer);
|
|
16
|
+
*
|
|
17
|
+
* @borrows EventEmitter
|
|
18
|
+
* @borrows Arrayable
|
|
19
|
+
*
|
|
20
|
+
* @example ```ts
|
|
21
|
+
* import { Store } from 'danholibraryjs';
|
|
22
|
+
*
|
|
23
|
+
* type Todo = {
|
|
24
|
+
* id: string,
|
|
25
|
+
* text: string,
|
|
26
|
+
* completed: boolean
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* type TodoActions = {
|
|
30
|
+
* create: [text: string],
|
|
31
|
+
* update: [id: string, text: string],
|
|
32
|
+
* toggleComplete: [id: string, force?: boolean],
|
|
33
|
+
* delete: [id: string],
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* const store = new Store<Array<Todo>, TodoActions>(new Array<Todo>(), new Map([
|
|
37
|
+
* create: (state, text) => {
|
|
38
|
+
* return [...state, {
|
|
39
|
+
* id: Math.random().toString(),
|
|
40
|
+
* text,
|
|
41
|
+
* completed: false
|
|
42
|
+
* }];
|
|
43
|
+
* },
|
|
44
|
+
* toggleComplete: (state, id, force) => {
|
|
45
|
+
* const todo = state.find(todo => todo.id === id);
|
|
46
|
+
* if (!todo) return state;
|
|
47
|
+
*
|
|
48
|
+
* return state.map(todo => (
|
|
49
|
+
* todo.id === id ? {
|
|
50
|
+
* ...todo,
|
|
51
|
+
* completed: force === undefined ? !todo.completed : force
|
|
52
|
+
* } : todo
|
|
53
|
+
* ));
|
|
54
|
+
* }
|
|
55
|
+
* ]));
|
|
56
|
+
*
|
|
57
|
+
* store.on('delete', (state, id) => {
|
|
58
|
+
* return state.filter(todo => todo.id !== id);
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* store.on('stateChange', (prevState, currentState) => console.log('State change', prevState, currentState));
|
|
62
|
+
*
|
|
63
|
+
* store.dispatch('create', 'Make store!');
|
|
64
|
+
*
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
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
|
+
});
|
|
75
|
+
private _state;
|
|
76
|
+
get state(): State;
|
|
77
|
+
dispatch<Action extends keyof ActionTypes>(action: Action, ...args: ActionTypes[Action]): State;
|
|
78
|
+
}
|
|
79
|
+
export default Store;
|
|
@@ -0,0 +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,5 +1,5 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
|
|
2
|
+
type UpdateFinder<T> = (item: T, index: number, self: Array<T>) => boolean;
|
|
3
3
|
declare global {
|
|
4
4
|
interface Array<T> {
|
|
5
5
|
/**
|
|
@@ -27,5 +27,12 @@ declare global {
|
|
|
27
27
|
* @param i Index of item
|
|
28
28
|
*/
|
|
29
29
|
index(i: number): T;
|
|
30
|
+
/**
|
|
31
|
+
* For every x in array, execute callback
|
|
32
|
+
* @param every i.e every 2nd item in array
|
|
33
|
+
* @param callback Function to execute
|
|
34
|
+
* @returns Array of results
|
|
35
|
+
*/
|
|
36
|
+
nth<U>(every: number, callback: (collection: Array<T>, index: number, self: this) => U): Array<U>;
|
|
30
37
|
}
|
|
31
38
|
}
|
package/dist/Extensions/Array.js
CHANGED
|
@@ -1,26 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.ArrayExtensions = void 0;
|
|
4
|
+
function add(...items) {
|
|
4
5
|
this.push(...items);
|
|
5
6
|
return this;
|
|
6
|
-
}
|
|
7
|
-
Array.prototype.
|
|
8
|
-
|
|
7
|
+
}
|
|
8
|
+
Array.prototype.add = add;
|
|
9
|
+
function update(old, updated) {
|
|
10
|
+
const item = typeof old === 'number' ? this[old]
|
|
11
|
+
: typeof old === 'function' ? this.find(old)
|
|
12
|
+
: old;
|
|
9
13
|
if (!item)
|
|
10
14
|
throw new Error('Old was not found in array!');
|
|
11
15
|
const index = this.indexOf(item);
|
|
12
16
|
return this[index] = updated;
|
|
13
|
-
}
|
|
14
|
-
Array.prototype.
|
|
17
|
+
}
|
|
18
|
+
Array.prototype.update = update;
|
|
19
|
+
function remove(value) {
|
|
15
20
|
const index = typeof value === 'number' ? value : this.indexOf(value);
|
|
16
21
|
if (index > -1)
|
|
17
22
|
this.splice(index, 1);
|
|
18
23
|
return this;
|
|
19
|
-
}
|
|
20
|
-
Array.prototype.
|
|
24
|
+
}
|
|
25
|
+
Array.prototype.remove = remove;
|
|
26
|
+
function random() {
|
|
21
27
|
const randomIndex = Math.round(Math.random() * this.length);
|
|
22
28
|
return this[randomIndex];
|
|
23
|
-
}
|
|
24
|
-
Array.prototype.
|
|
29
|
+
}
|
|
30
|
+
Array.prototype.random = random;
|
|
31
|
+
function index(i) {
|
|
25
32
|
return this[i < 0 ? this.length + i : i];
|
|
33
|
+
}
|
|
34
|
+
Array.prototype.index = index;
|
|
35
|
+
function nth(every, callback) {
|
|
36
|
+
const result = new Array();
|
|
37
|
+
let collection = new Array();
|
|
38
|
+
for (let i = 0; i < this.length; i++) {
|
|
39
|
+
collection.push(this[i]);
|
|
40
|
+
if (i % every === 0) {
|
|
41
|
+
result.push(callback(this[i], i, collection, this));
|
|
42
|
+
collection = new Array();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
Array.prototype.nth = nth;
|
|
48
|
+
exports.ArrayExtensions = {
|
|
49
|
+
add, update, remove,
|
|
50
|
+
random, index, nth
|
|
51
|
+
};
|
|
52
|
+
Array.prototype.nth = function (every, callback) {
|
|
53
|
+
const result = new Array();
|
|
54
|
+
let collection = new Array();
|
|
55
|
+
for (let i = 0; i < this.length; i++) {
|
|
56
|
+
collection.push(this[i]);
|
|
57
|
+
if (i % every === 0) {
|
|
58
|
+
result.push(callback(collection, i, this));
|
|
59
|
+
collection = new Array();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
26
63
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IElement } from "../Types";
|
|
2
|
+
declare global {
|
|
3
|
+
interface Document {
|
|
4
|
+
/**
|
|
5
|
+
* Creates an element like Document#createElement, however with construction options to assign values in construction instead of after construction.
|
|
6
|
+
* @param tagName HTMLElement tag name
|
|
7
|
+
* @param options Construction options, instead of assigning values after construction
|
|
8
|
+
* @param children Child elements
|
|
9
|
+
*/
|
|
10
|
+
createProperElement<Tag extends keyof HTMLElementTagNameMap>(tagName: Tag, options?: Partial<HTMLElementTagNameMap[Tag]>, ...children: Array<IElement>): HTMLElementTagNameMap[Tag];
|
|
11
|
+
createProperElement<Tag extends keyof HTMLElementTagNameMap>(tagName: Tag, ...children: Array<IElement>): HTMLElementTagNameMap[Tag];
|
|
12
|
+
createElementFromString<K extends keyof HTMLElementTagNameMap>(html: string, parentTag?: K): HTMLElementTagNameMap[K];
|
|
13
|
+
}
|
|
14
|
+
interface HTMLCollection {
|
|
15
|
+
/**
|
|
16
|
+
* Converts HTMLCollection to Element[]
|
|
17
|
+
*/
|
|
18
|
+
array(): Element[];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
declare function createElement<Tag extends keyof HTMLElementTagNameMap>(this: Document, tagName: Tag, options?: Partial<HTMLElementTagNameMap[Tag]> | string, ...children: Array<IElement>): HTMLElementTagNameMap[Tag];
|
|
22
|
+
declare function createElementFromString<Tag extends keyof HTMLElementTagNameMap>(this: Document, html: string, tag?: Tag): HTMLElementTagNameMap[Tag];
|
|
23
|
+
export declare const DocumentExtensions: {
|
|
24
|
+
createElement: typeof createElement;
|
|
25
|
+
createElementFromString: typeof createElementFromString;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentExtensions = void 0;
|
|
4
|
+
function createElement(tagName, options, ...children) {
|
|
5
|
+
const element = Object.assign(document.createElement(tagName), typeof options === 'string' ? {} : options);
|
|
6
|
+
children ??= typeof options === 'string' ? [options] : [];
|
|
7
|
+
typeof options === 'string' && children.unshift(options);
|
|
8
|
+
if (!children.length)
|
|
9
|
+
return element;
|
|
10
|
+
else if (typeof children === 'string')
|
|
11
|
+
element.innerHTML = children;
|
|
12
|
+
else if (children instanceof Array)
|
|
13
|
+
children.forEach(child => (typeof child === 'string' ?
|
|
14
|
+
element.innerHTML += child :
|
|
15
|
+
element.appendChild(child)));
|
|
16
|
+
else
|
|
17
|
+
element.appendChild(children);
|
|
18
|
+
return element;
|
|
19
|
+
}
|
|
20
|
+
Document.prototype.createProperElement = createElement;
|
|
21
|
+
function createElementFromString(html, tag) {
|
|
22
|
+
if (!html.startsWith(`<${tag}`))
|
|
23
|
+
html = `<${tag}>${html}</${tag}>`;
|
|
24
|
+
return new DOMParser().parseFromString(html, 'text/html').body.firstElementChild;
|
|
25
|
+
}
|
|
26
|
+
Document.prototype.createElementFromString = createElementFromString;
|
|
27
|
+
HTMLCollection.prototype.array = function () {
|
|
28
|
+
return Array.from(this);
|
|
29
|
+
};
|
|
30
|
+
exports.DocumentExtensions = {
|
|
31
|
+
createElement, createElementFromString
|
|
32
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Function {
|
|
3
|
+
/**
|
|
4
|
+
* Checks if obj is a function
|
|
5
|
+
* @param obj Object to check
|
|
6
|
+
*/
|
|
7
|
+
is(obj: any): obj is Function;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
declare function is(obj: any): obj is Function;
|
|
11
|
+
export declare const FunctionExtensions: {
|
|
12
|
+
is: typeof is;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
package/dist/Extensions/Map.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export {};
|
|
2
1
|
declare global {
|
|
3
2
|
interface Map<K, V> {
|
|
4
3
|
/**
|
|
@@ -36,3 +35,20 @@ declare global {
|
|
|
36
35
|
includes(value: V, fromIndex?: number): boolean;
|
|
37
36
|
}
|
|
38
37
|
}
|
|
38
|
+
declare function array<K, V>(this: Map<K, V>): Array<[K, V]>;
|
|
39
|
+
declare function map<K, V, EK, EV>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => [EK, EV]): Map<EK, EV>;
|
|
40
|
+
declare function filter<K, V>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => boolean): Map<K, V>;
|
|
41
|
+
declare function keyArr<K, V>(this: Map<K, V>): Array<K>;
|
|
42
|
+
declare function valueArr<K, V>(this: Map<K, V>): Array<V>;
|
|
43
|
+
declare function find<K, V>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => boolean): [K, V] | undefined;
|
|
44
|
+
declare function includes<K, V>(this: Map<K, V>, item: V, fromIndex?: number): boolean;
|
|
45
|
+
export declare const MapExtensions: {
|
|
46
|
+
array: typeof array;
|
|
47
|
+
map: typeof map;
|
|
48
|
+
filter: typeof filter;
|
|
49
|
+
keyArr: typeof keyArr;
|
|
50
|
+
valueArr: typeof valueArr;
|
|
51
|
+
find: typeof find;
|
|
52
|
+
includes: typeof includes;
|
|
53
|
+
};
|
|
54
|
+
export {};
|
package/dist/Extensions/Map.js
CHANGED
|
@@ -1,31 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.MapExtensions = void 0;
|
|
4
|
+
function array() {
|
|
4
5
|
let result = new Array();
|
|
5
6
|
for (const kvp of this) {
|
|
6
7
|
result.push(kvp);
|
|
7
8
|
}
|
|
8
9
|
return result;
|
|
9
|
-
}
|
|
10
|
-
Map.prototype.
|
|
10
|
+
}
|
|
11
|
+
Map.prototype.array = array;
|
|
12
|
+
function map(callback) {
|
|
11
13
|
return this.array()
|
|
12
14
|
.map(([k, v], i) => callback(v, k, i, this))
|
|
13
15
|
.reduce((map, [key, value]) => map.set(key, value), new Map());
|
|
14
|
-
}
|
|
15
|
-
Map.prototype.
|
|
16
|
+
}
|
|
17
|
+
Map.prototype.map = map;
|
|
18
|
+
function filter(callback) {
|
|
16
19
|
return this.array()
|
|
17
20
|
.filter(([k, v], i) => callback(v, k, i, this))
|
|
18
21
|
.reduce((map, [key, value]) => map.set(key, value), new Map());
|
|
19
|
-
}
|
|
20
|
-
Map.prototype.
|
|
22
|
+
}
|
|
23
|
+
Map.prototype.filter = filter;
|
|
24
|
+
function keyArr() {
|
|
21
25
|
return this.array().map(([k]) => k);
|
|
22
|
-
}
|
|
23
|
-
Map.prototype.
|
|
26
|
+
}
|
|
27
|
+
Map.prototype.keyArr = keyArr;
|
|
28
|
+
function valueArr() {
|
|
24
29
|
return this.array().map(([_, v]) => v);
|
|
25
|
-
}
|
|
26
|
-
Map.prototype.
|
|
30
|
+
}
|
|
31
|
+
Map.prototype.valueArr = valueArr;
|
|
32
|
+
function find(callback) {
|
|
27
33
|
return this.array().find(([k, v], i) => callback(v, k, i, this));
|
|
28
|
-
}
|
|
29
|
-
Map.prototype.
|
|
34
|
+
}
|
|
35
|
+
Map.prototype.find = find;
|
|
36
|
+
function includes(item, fromIndex) {
|
|
30
37
|
return this.valueArr().includes(item, fromIndex);
|
|
38
|
+
}
|
|
39
|
+
Map.prototype.includes = includes;
|
|
40
|
+
exports.MapExtensions = {
|
|
41
|
+
array, map, filter, keyArr, valueArr, find, includes
|
|
31
42
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ValueOf } from "../../Types";
|
|
2
|
+
import { Properties } from "./properties";
|
|
3
|
+
declare global {
|
|
4
|
+
interface ObjectConstructor {
|
|
5
|
+
/**
|
|
6
|
+
* Destructures object into array of [property, value]
|
|
7
|
+
* @param from Object to destruct
|
|
8
|
+
*/
|
|
9
|
+
array<From extends {} = {}>(from: From): Array<[keyof From, ValueOf<From>]>;
|
|
10
|
+
/**
|
|
11
|
+
* Omits properties from object, but for some reason the correct term is "extract"
|
|
12
|
+
* @param from Object to extract properties from
|
|
13
|
+
* @param props Properties to extract/Omit
|
|
14
|
+
*/
|
|
15
|
+
extract<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Omit<From, Props>;
|
|
16
|
+
/**
|
|
17
|
+
* Pick properties from object, but for some reason the correct term is "exclude"
|
|
18
|
+
* @param from Object to exclude properties from
|
|
19
|
+
* @param props Properties to exclude/pick
|
|
20
|
+
*/
|
|
21
|
+
exclude<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Pick<From, Props>;
|
|
22
|
+
/**
|
|
23
|
+
* Returns true if object is empty
|
|
24
|
+
* @param obj Object to check
|
|
25
|
+
*/
|
|
26
|
+
isNullOrUndefined(obj: any): obj is null | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Destructures object into array of property keys
|
|
29
|
+
* @param from Object to destruct
|
|
30
|
+
*/
|
|
31
|
+
keysOf<From extends {} = {}>(from: From): Array<keyof From>;
|
|
32
|
+
omit<From extends {}, Exclude extends keyof From>(from: From, ...exclude: Exclude[]): Omit<From, Exclude>;
|
|
33
|
+
properties: Properties;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
declare function array<From extends {} = {}>(this: object, from: From): Array<[keyof From, ValueOf<From>]>;
|
|
37
|
+
declare function extract<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Omit<From, Props>;
|
|
38
|
+
declare function exclude<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Pick<From, Props>;
|
|
39
|
+
declare function isNullOrUndefined(obj: any): obj is null | undefined;
|
|
40
|
+
declare function keysOf<From extends {} = {}>(this: object, from: From): Array<keyof From>;
|
|
41
|
+
export declare const ObjectExtensions: {
|
|
42
|
+
properties: Properties;
|
|
43
|
+
array: typeof array;
|
|
44
|
+
extract: typeof extract;
|
|
45
|
+
exclude: typeof exclude;
|
|
46
|
+
isNullOrUndefined: typeof isNullOrUndefined;
|
|
47
|
+
keysOf: typeof keysOf;
|
|
48
|
+
};
|
|
49
|
+
export {};
|