danholibraryjs 1.5.0 → 1.8.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/README.md +12 -304
- package/Time.xlsx +0 -0
- package/dist/Classes/{Event.d.ts → Events/Event.d.ts} +9 -8
- package/dist/Classes/{Event.js → Events/Event.js} +17 -9
- package/dist/Classes/{EventCollection.d.ts → Events/EventCollection.d.ts} +10 -10
- package/dist/Classes/{EventCollection.js → Events/EventCollection.js} +28 -29
- package/dist/Classes/{EventEmitter.d.ts → Events/EventEmitter.d.ts} +33 -7
- package/dist/Classes/{EventEmitter.js → Events/EventEmitter.js} +33 -3
- package/dist/Classes/Events/index.d.ts +3 -0
- package/dist/Classes/Events/index.js +19 -0
- package/dist/Classes/Time/Date.d.ts +147 -0
- package/dist/Classes/Time/Date.js +238 -0
- package/dist/Classes/Time/Time.d.ts +65 -0
- package/dist/Classes/Time/Time.js +117 -0
- package/dist/Classes/Time/TimeProperties.d.ts +3 -0
- package/dist/{Interfaces/BaseEventInterface.js → Classes/Time/TimeProperties.js} +0 -0
- package/dist/Classes/Time/TimeSpan.d.ts +123 -0
- package/dist/Classes/Time/TimeSpan.js +179 -0
- package/dist/Classes/Time/index.d.ts +4 -0
- package/dist/Classes/Time/index.js +20 -0
- package/dist/Classes/index.d.ts +2 -3
- package/dist/Classes/index.js +7 -4
- package/dist/Classes/store.d.ts +79 -0
- package/dist/Classes/store.js +85 -0
- package/dist/Extensions/Array.d.ts +31 -0
- package/dist/Extensions/Array.js +26 -0
- package/dist/Extensions/Map.d.ts +38 -0
- package/dist/Extensions/Map.js +31 -0
- package/dist/Extensions/Object.d.ts +16 -0
- package/dist/Extensions/Object.js +8 -0
- package/dist/Extensions/String.d.ts +26 -0
- package/dist/Extensions/String.js +17 -0
- package/dist/Extensions/index.d.ts +29 -0
- package/dist/Extensions/index.js +71 -0
- package/dist/Functions/GetCSSProperty.d.ts +15 -0
- package/dist/Functions/GetCSSProperty.js +26 -0
- package/dist/Functions/HTMLEvent.d.ts +6 -0
- package/dist/Functions/HTMLEvent.js +6 -0
- package/dist/Functions/SetNavigationSelected.js +2 -0
- package/dist/Functions/index.d.ts +1 -0
- package/dist/Functions/index.js +6 -1
- package/dist/Interfaces/ElementOptions.d.ts +12 -15
- package/dist/Interfaces/IReplacement.d.ts +1 -1
- package/dist/Interfaces/index.d.ts +0 -1
- package/dist/Interfaces/index.js +5 -2
- package/dist/Types/BetterTypes.d.ts +9 -0
- package/dist/{Interfaces/IHTMLEvent.js → Types/BetterTypes.js} +0 -0
- package/dist/Types/Date.d.ts +6 -0
- package/dist/Types/{EventHandler.js → Date.js} +0 -0
- package/dist/Types/Events.d.ts +10 -0
- package/dist/Types/{IElement.js → Events.js} +0 -0
- package/dist/Types/PropertiesWith.d.ts +13 -0
- package/dist/Types/{StringRegex.js → PropertiesWith.js} +0 -0
- package/dist/Types/TransformTypes.d.ts +13 -0
- package/dist/Types/TransformTypes.js +2 -0
- package/dist/Types/index.d.ts +31 -3
- package/dist/Types/index.js +10 -4
- package/dist/index.d.ts +5 -5
- package/dist/index.js +19 -10
- package/docs/Classes.md +488 -0
- package/docs/Extensions.md +146 -0
- package/docs/Functions.md +53 -0
- package/docs/Interfaces.md +34 -0
- package/docs/Types.md +137 -0
- package/docs/index.md +29 -0
- package/package.json +22 -21
- package/src/Classes/{Event.ts → Events/Event.ts} +19 -14
- package/src/Classes/Events/EventCollection.ts +109 -0
- package/src/Classes/{EventEmitter.ts → Events/EventEmitter.ts} +37 -11
- package/src/Classes/Events/index.ts +3 -0
- package/src/Classes/Time/Date.ts +264 -0
- package/src/Classes/Time/Time.ts +134 -0
- package/src/Classes/Time/TimeProperties.ts +3 -0
- package/src/Classes/Time/TimeSpan.ts +195 -0
- package/src/Classes/Time/index.ts +4 -0
- package/src/Classes/index.ts +2 -3
- package/src/Classes/store.ts +95 -0
- package/src/Extensions/Array.ts +57 -0
- package/src/Extensions/Map.ts +73 -0
- package/src/Extensions/Object.ts +25 -0
- package/src/Extensions/String.ts +43 -0
- package/src/Extensions/index.ts +83 -0
- package/src/Functions/GetCSSProperty.ts +27 -0
- package/src/Functions/HTMLEvent.ts +6 -0
- package/src/Functions/SetNavigationSelected.ts +1 -0
- package/src/Functions/index.ts +2 -1
- package/src/Interfaces/ElementOptions.ts +18 -14
- package/src/Interfaces/IReplacement.ts +1 -1
- package/src/Interfaces/index.ts +1 -2
- package/src/Types/BetterTypes.ts +10 -0
- package/src/Types/Date.ts +7 -0
- package/src/Types/Events.ts +15 -0
- package/src/Types/PropertiesWith.ts +14 -0
- package/src/Types/TransformTypes.ts +18 -0
- package/src/Types/index.ts +36 -3
- package/src/index.ts +5 -6
- package/tsconfig.json +99 -7
- package/dist/Extensions.d.ts +0 -85
- package/dist/Extensions.js +0 -84
- package/dist/Interfaces/BaseEventInterface.d.ts +0 -4
- package/dist/Interfaces/IHTMLEvent.d.ts +0 -4
- package/dist/Types/EventHandler.d.ts +0 -7
- package/dist/Types/IElement.d.ts +0 -9
- package/dist/Types/StringRegex.d.ts +0 -2
- package/src/Classes/EventCollection.ts +0 -116
- package/src/Extensions.ts +0 -185
- package/src/Interfaces/BaseEventInterface.ts +0 -4
- package/src/Types/EventHandler.ts +0 -12
- package/src/Types/IElement.ts +0 -9
- package/src/Types/StringRegex.ts +0 -2
- package/test.js +0 -20
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Arrayable } from "../Types";
|
|
2
|
+
import { EventEmitter } from "./Events";
|
|
3
|
+
|
|
4
|
+
export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* EventEmitter, but it stores state and handles state change with reducers
|
|
8
|
+
*
|
|
9
|
+
* @Initialization Actions & initial state must be defined in type parameters. InitialState must be provided in constructor, whereas reducer is optional.
|
|
10
|
+
* The ActionType must have properties as strings and values as arrays.
|
|
11
|
+
*
|
|
12
|
+
* @HandlingActions Reducers can be added through constructor or using Store.on('action', reducer) or Store.once('action', reducer).
|
|
13
|
+
* Every state change must return the next state, apart from 'stateChange', which returns void/any
|
|
14
|
+
* Emit/Dispatch an action using Store.dispatch('action', ...args), ...args being the parameters from the ActionType.
|
|
15
|
+
* Store.emit should NOT be used, as it doesn't update the Store's state.
|
|
16
|
+
*
|
|
17
|
+
* Reducer functions can be removed using Store.off('action', reducer);
|
|
18
|
+
*
|
|
19
|
+
* @borrows EventEmitter
|
|
20
|
+
* @borrows Arrayable
|
|
21
|
+
*
|
|
22
|
+
* @example ```ts
|
|
23
|
+
* import { Store } from 'danholibraryjs';
|
|
24
|
+
*
|
|
25
|
+
* type Todo = {
|
|
26
|
+
* id: string,
|
|
27
|
+
* text: string,
|
|
28
|
+
* completed: boolean
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* type TodoActions = {
|
|
32
|
+
* create: [text: string],
|
|
33
|
+
* update: [id: string, text: string],
|
|
34
|
+
* toggleComplete: [id: string, force?: boolean],
|
|
35
|
+
* delete: [id: string],
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* const store = new Store<Array<Todo>, TodoActions>(new Array<Todo>(), new Map([
|
|
39
|
+
* create: (state, text) => {
|
|
40
|
+
* return [...state, {
|
|
41
|
+
* id: Math.random().toString(),
|
|
42
|
+
* text,
|
|
43
|
+
* completed: false
|
|
44
|
+
* }];
|
|
45
|
+
* },
|
|
46
|
+
* toggleComplete: (state, id, force) => {
|
|
47
|
+
* const todo = state.find(todo => todo.id === id);
|
|
48
|
+
* if (!todo) return state;
|
|
49
|
+
*
|
|
50
|
+
* return state.map(todo => (
|
|
51
|
+
* todo.id === id ? {
|
|
52
|
+
* ...todo,
|
|
53
|
+
* completed: force === undefined ? !todo.completed : force
|
|
54
|
+
* } : todo
|
|
55
|
+
* ));
|
|
56
|
+
* }
|
|
57
|
+
* ]));
|
|
58
|
+
*
|
|
59
|
+
* store.on('delete', (state, id) => {
|
|
60
|
+
* return state.filter(todo => todo.id !== id);
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* store.on('stateChange', (prevState, currentState) => console.log('State change', prevState, currentState));
|
|
64
|
+
*
|
|
65
|
+
* store.dispatch('create', 'Make store!');
|
|
66
|
+
*
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export class Store<
|
|
70
|
+
State extends object,
|
|
71
|
+
ActionTypes extends Record<string, any[]>,
|
|
72
|
+
Actions extends
|
|
73
|
+
{ [Action in keyof ActionTypes]: Array<Reducer<State, ActionTypes, Action>> } =
|
|
74
|
+
{ [Action in keyof ActionTypes]: Array<Reducer<State, ActionTypes, Action>> }
|
|
75
|
+
> extends EventEmitter<Record<keyof Actions, ActionTypes[keyof ActionTypes]> & Record<'stateChange', [previous: State, current: State]>> {
|
|
76
|
+
constructor(state: State, actions: { [Action in keyof ActionTypes]?: Arrayable<Reducer<State, ActionTypes, Action>> } = {}) {
|
|
77
|
+
super(new Map(...Object.entries(actions).map(([action, reducers]) => [action, reducers as any])));
|
|
78
|
+
|
|
79
|
+
this._state = state;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private _state: State;
|
|
83
|
+
public get state(): State {
|
|
84
|
+
return this._state;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public dispatch<Action extends keyof ActionTypes>(action: Action, ...args: ActionTypes[Action]): State {
|
|
88
|
+
const previous = { ...this._state };
|
|
89
|
+
this._state = super.emit<State, Action>(action, ...args as any).reduce((state, returned) => ({ ...state, ...returned }), this.state);
|
|
90
|
+
|
|
91
|
+
super.emit<void, 'stateChange'>('stateChange', ...[previous, this.state] as any);
|
|
92
|
+
return this.state;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export default Store;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
|
|
3
|
+
type UpdateFinder<T> = (item: T, index: number, self: Array<T>) => boolean
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface Array<T> {
|
|
7
|
+
/**
|
|
8
|
+
* Pushes items to array and returns self with new items
|
|
9
|
+
* @param items Items to add to array
|
|
10
|
+
*/
|
|
11
|
+
add(...items: Array<T>): this
|
|
12
|
+
/**
|
|
13
|
+
* Update an item in array
|
|
14
|
+
* @param old The old value or index to update
|
|
15
|
+
* @param updated Updated value
|
|
16
|
+
*/
|
|
17
|
+
update(old: T | number | UpdateFinder<T>, updated: T): T
|
|
18
|
+
/**
|
|
19
|
+
* Removes item from array and returns self without item
|
|
20
|
+
* @param item Item or index to remove
|
|
21
|
+
*/
|
|
22
|
+
remove(item: T | number): this
|
|
23
|
+
/**
|
|
24
|
+
* Returns a random element from array
|
|
25
|
+
*/
|
|
26
|
+
random(): T
|
|
27
|
+
/**
|
|
28
|
+
* Returns item matching index. If negative number, subtracts number from length
|
|
29
|
+
* @param i Index of item
|
|
30
|
+
*/
|
|
31
|
+
index(i: number): T
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
Array.prototype.add = function<T>(this: Array<T>, ...items: Array<T>) {
|
|
36
|
+
this.push(...items);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
Array.prototype.update = function<T>(this: Array<T>, old: T | number | UpdateFinder<T>, updated: T) {
|
|
40
|
+
const item = typeof old === 'number' ? this[old] : typeof old === 'function' ? this.find(old as UpdateFinder<T>) : old;
|
|
41
|
+
if (!item) throw new Error('Old was not found in array!');
|
|
42
|
+
|
|
43
|
+
const index = this.indexOf(item);
|
|
44
|
+
return this[index] = updated;
|
|
45
|
+
}
|
|
46
|
+
Array.prototype.remove = function<T>(this: Array<T>, value: T | number): Array<T> {
|
|
47
|
+
const index = typeof value === 'number' ? value : this.indexOf(value);
|
|
48
|
+
if (index > -1) this.splice(index, 1);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
Array.prototype.random = function<T>(this: Array<T>): T {
|
|
52
|
+
const randomIndex = Math.round(Math.random() * this.length);
|
|
53
|
+
return this[randomIndex];
|
|
54
|
+
}
|
|
55
|
+
Array.prototype.index = function<T>(this: Array<T>, i: number): T {
|
|
56
|
+
return this[i < 0 ? this.length + i : i];
|
|
57
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export {}
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Map<K, V> {
|
|
5
|
+
/**
|
|
6
|
+
* Converts into Array<[Key, Value]>
|
|
7
|
+
*/
|
|
8
|
+
array(): Array<[K, V]>
|
|
9
|
+
/**
|
|
10
|
+
* Maps values into new types of generics
|
|
11
|
+
* @param callback Callbacking function to map values
|
|
12
|
+
*/
|
|
13
|
+
map<EK, EV>(callback: (value: V, key: K, index: number, self: this) => [EK, EV]): Map<EK, EV>
|
|
14
|
+
/**
|
|
15
|
+
* Returns array of "accepted" values. Criteria defined in callback param
|
|
16
|
+
* @param callback Callbacking function to filter away unwanted values
|
|
17
|
+
*/
|
|
18
|
+
filter(callback: (value: V, key: K, index: number, self: this) => boolean): Map<K, V>
|
|
19
|
+
/**
|
|
20
|
+
* Returns array of keys
|
|
21
|
+
*/
|
|
22
|
+
keyArr(): Array<K>
|
|
23
|
+
/**
|
|
24
|
+
* Returns array of values
|
|
25
|
+
*/
|
|
26
|
+
valueArr(): Array<V>
|
|
27
|
+
/**
|
|
28
|
+
* Returns first [key, value] match to callback param. Returns undefined if nothing found
|
|
29
|
+
* @param callback Callbacking function to find KeyValuePair
|
|
30
|
+
*/
|
|
31
|
+
find(callback: (value: V, key: K, index: number, self: this) => boolean): [K, V] | undefined
|
|
32
|
+
/**
|
|
33
|
+
* Whether or not map includes a value. Returns true if it does, false if not ¯\_(ツ)_/¯
|
|
34
|
+
* @param value Value that may be includded in map
|
|
35
|
+
* @param fromIndex Start looking for value from specific index+. Default: 0
|
|
36
|
+
*/
|
|
37
|
+
includes(value: V, fromIndex?: number): boolean
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Map.prototype.array = function<K, V>(this: Map<K, V>): Array<[K, V]> {
|
|
42
|
+
let result = new Array<[K, V]>();
|
|
43
|
+
for (const kvp of this) {
|
|
44
|
+
result.push(kvp);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
Map.prototype.map = function<K, V, EK, EV>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => [EK, EV]): Map<EK, EV> {
|
|
49
|
+
return this.array()
|
|
50
|
+
.map(([k, v], i) => callback(v, k, i, this))
|
|
51
|
+
.reduce((map, [key, value]) =>
|
|
52
|
+
map.set(key, value),
|
|
53
|
+
new Map<EK, EV>())
|
|
54
|
+
}
|
|
55
|
+
Map.prototype.filter = function<K, V>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => boolean): Map<K, V> {
|
|
56
|
+
return this.array()
|
|
57
|
+
.filter(([k, v], i) => callback(v, k, i, this))
|
|
58
|
+
.reduce((map, [key, value]) =>
|
|
59
|
+
map.set(key, value),
|
|
60
|
+
new Map<K, V>())
|
|
61
|
+
}
|
|
62
|
+
Map.prototype.keyArr = function<K, V>(this: Map<K, V>): Array<K> {
|
|
63
|
+
return this.array().map(([k]) => k);
|
|
64
|
+
}
|
|
65
|
+
Map.prototype.valueArr = function<K, V>(this: Map<K, V>): Array<V> {
|
|
66
|
+
return this.array().map(([_, v]) => v);
|
|
67
|
+
}
|
|
68
|
+
Map.prototype.find = function<K, V>(this: Map<K, V>, callback: (value: V, key: K, index: number, map: Map<K, V>) => boolean) {
|
|
69
|
+
return this.array().find(([k, v], i) => callback(v, k, i, this));
|
|
70
|
+
}
|
|
71
|
+
Map.prototype.includes = function<K, V>(this: Map<K, V>, item: V, fromIndex?: number) {
|
|
72
|
+
return this.valueArr().includes(item, fromIndex);
|
|
73
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ValueOf } from "../Types";
|
|
2
|
+
|
|
3
|
+
export {};
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface ObjectConstructor {
|
|
7
|
+
/**
|
|
8
|
+
* Destructures object into array of [property, value]
|
|
9
|
+
* @param from Object to destruct
|
|
10
|
+
*/
|
|
11
|
+
array<From = {}>(from: From): Array<[keyof From, ValueOf<From>]>
|
|
12
|
+
/**
|
|
13
|
+
* Destructures object into array of property keys
|
|
14
|
+
* @param from Object to destruct
|
|
15
|
+
*/
|
|
16
|
+
keysOf<From = {}>(from: From): Array<keyof From>
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Object.keysOf = function<From = {}>(this: object, from: From): Array<keyof From> {
|
|
21
|
+
return Object.keys(from) as Array<keyof From>;
|
|
22
|
+
}
|
|
23
|
+
Object.array = function<From = {}>(this: object, from: From): Array<[keyof From, ValueOf<From>]> {
|
|
24
|
+
return Object.keysOf(from).map(prop => [prop, from[prop]]) as Array<[keyof From, ValueOf<From>]>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import IReplacement from "../Interfaces/IReplacement";
|
|
2
|
+
export {}
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
interface String {
|
|
6
|
+
/**
|
|
7
|
+
* Uppercases first letter of string
|
|
8
|
+
*/
|
|
9
|
+
toPascalCase(): string
|
|
10
|
+
/**
|
|
11
|
+
* Replaces "replacer" (default: ' ') with "replacement" (default: '_')
|
|
12
|
+
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
13
|
+
*/
|
|
14
|
+
toSnakeCase(replaceOptions?: IReplacement): string
|
|
15
|
+
/**
|
|
16
|
+
* Replaces "replacer" (default: ' ') with "replacement" (default: '-')
|
|
17
|
+
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
18
|
+
*/
|
|
19
|
+
toKebabCase(replaceOptions?: IReplacement): string,
|
|
20
|
+
/**
|
|
21
|
+
* String.substring but accepting negative numbers to cut from length
|
|
22
|
+
* @param start Start of string. 0 indexed, if negative number, subtracts number from length
|
|
23
|
+
* @param end End of string. 0 indexed, if negative number, substracts number from length
|
|
24
|
+
*/
|
|
25
|
+
clip(start: number, end?: number): string
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
String.prototype.toPascalCase = function(this: string) {
|
|
30
|
+
return this.substring(0, 1).toUpperCase() + this.substring(1);
|
|
31
|
+
}
|
|
32
|
+
function spaceReplacer(self: string, replacer: string | RegExp, replacement: string) {
|
|
33
|
+
return self.replace(new RegExp(`${typeof replacer == 'string' ? replacer : replacer.source}+`), replacement);
|
|
34
|
+
}
|
|
35
|
+
String.prototype.toSnakeCase = function(this: string, replaceOptions: IReplacement) {
|
|
36
|
+
return spaceReplacer(this, replaceOptions.replacer || ' ', replaceOptions.replacement || '_')
|
|
37
|
+
}
|
|
38
|
+
String.prototype.toKebabCase = function(this: string, replaceOptions: IReplacement) {
|
|
39
|
+
return spaceReplacer(this, replaceOptions.replacer || ' ', replaceOptions.replacement || '-');
|
|
40
|
+
}
|
|
41
|
+
String.prototype.clip = function(this: string, start: number, end?: number) {
|
|
42
|
+
return this.substring(start < 0 ? this.length - start : start, end && end < 0 ? this.length + end : end);
|
|
43
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import ElementOptions from "../Interfaces/ElementOptions";
|
|
2
|
+
import { EventHandler, IElement } from "../Types";
|
|
3
|
+
export * from './Array';
|
|
4
|
+
export * from './Map';
|
|
5
|
+
export * from './Object';
|
|
6
|
+
export * from './String';
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
interface BooleanConstructor {
|
|
10
|
+
/**
|
|
11
|
+
* Parses string to boolean. Will only return true if value === "true" otherwise false
|
|
12
|
+
*/
|
|
13
|
+
parseBoolean(value: string): boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Document {
|
|
17
|
+
/**
|
|
18
|
+
* Creates an element like Document#createElement, however with construction options to assign values in construction instead of after construction.
|
|
19
|
+
* @param tagName HTMLElement tag name
|
|
20
|
+
* @param options Construction options, instead of assigning values after construction
|
|
21
|
+
*/
|
|
22
|
+
createProperElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementOptions, ...children: Array<IElement>): HTMLElementTagNameMap[K]
|
|
23
|
+
createFromHtml<K extends keyof HTMLElementTagNameMap>(html: string, parentTag?: K): HTMLElementTagNameMap[K]
|
|
24
|
+
}
|
|
25
|
+
interface HTMLCollection {
|
|
26
|
+
/**
|
|
27
|
+
* Converts HTMLCollection to Element[]
|
|
28
|
+
*/
|
|
29
|
+
array(): Element[]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Boolean.parseBoolean = function(value: string) {
|
|
34
|
+
return value === "true";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
Document.prototype.createProperElement = function<K extends keyof HTMLElementTagNameMap>(this: Document, tagName: K, options?: ElementOptions, ...children: Array<IElement>): HTMLElementTagNameMap[K] {
|
|
39
|
+
let baseElement = document.createElement(tagName);
|
|
40
|
+
if (!options) return baseElement;
|
|
41
|
+
|
|
42
|
+
const { id, class: className, dataset, ...rest } = options;
|
|
43
|
+
if (id) baseElement.id = id;
|
|
44
|
+
if (className) {
|
|
45
|
+
const classNames = Array.isArray(className) ? className : [className];
|
|
46
|
+
classNames.forEach(className => baseElement.classList.add(className));
|
|
47
|
+
}
|
|
48
|
+
children ?? options.children;
|
|
49
|
+
if (children) {
|
|
50
|
+
const childrenElements = Array.isArray(children) ? children : [children];
|
|
51
|
+
childrenElements.forEach(child => baseElement.append(child));
|
|
52
|
+
}
|
|
53
|
+
if (dataset) Object.entries(dataset).forEach(([key, value]) => baseElement.dataset[key] = value);
|
|
54
|
+
|
|
55
|
+
for (const optionKey in rest) {
|
|
56
|
+
const optionValue = rest[optionKey as keyof typeof rest] as EventHandler | object;
|
|
57
|
+
if (optionValue === undefined) continue;
|
|
58
|
+
|
|
59
|
+
if (typeof optionValue === 'function') {
|
|
60
|
+
baseElement.addEventListener(optionKey.substring(2), rest[optionKey as keyof typeof rest] as EventListener);
|
|
61
|
+
} else {
|
|
62
|
+
baseElement.setAttribute(optionKey, optionValue.toString());
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return baseElement;
|
|
67
|
+
}
|
|
68
|
+
Document.prototype.createFromHtml = function<K extends keyof HTMLElementTagNameMap>(this: Document, html: string, parentTag?: K): HTMLElementTagNameMap[K] {
|
|
69
|
+
return new DOMParser().parseFromString(html, 'text/html').body.firstChild as HTMLElementTagNameMap[K];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
HTMLCollection.prototype.array = function(this: HTMLCollection) {
|
|
73
|
+
let result = new Array<Element>();
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < this.length; i++) {
|
|
76
|
+
const item = this.item(i);
|
|
77
|
+
if (item !== null) result.push(item);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
// Used in node.js
|
|
83
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type CSSReturnTypes = {
|
|
2
|
+
string: string,
|
|
3
|
+
number: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Gets the value of "property" in type "type" from query "query"
|
|
8
|
+
* Basically, you can get your --color-primary from :root
|
|
9
|
+
*
|
|
10
|
+
* @param property Name of the property to get
|
|
11
|
+
* @param type Type of the property to parse
|
|
12
|
+
* @param query Query to get the element that has the property
|
|
13
|
+
* @returns Property value converted to type
|
|
14
|
+
*/
|
|
15
|
+
export function GetCSSProperty<Type extends keyof CSSReturnTypes>(property: string, type: Type, query = ":root"): CSSReturnTypes[Type] {
|
|
16
|
+
const rootEl = document.querySelector(query);
|
|
17
|
+
if (!rootEl) throw new Error(`${query} does not exist!`);
|
|
18
|
+
|
|
19
|
+
const rootStyles = getComputedStyle(rootEl);
|
|
20
|
+
const cssProp = rootStyles.getPropertyValue(property);
|
|
21
|
+
if (type === 'string') return cssProp as CSSReturnTypes[Type];
|
|
22
|
+
|
|
23
|
+
const numberValue = parseInt(cssProp);
|
|
24
|
+
if (isNaN(numberValue)) throw new Error(`${property} is not a number!: ${cssProp}`);
|
|
25
|
+
return numberValue as CSSReturnTypes[Type];
|
|
26
|
+
}
|
|
27
|
+
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export function SetNavigationSelected(query: string, ...currentPageClasses: string[]) {
|
|
9
9
|
const header = document.querySelector(query);
|
|
10
|
+
if (!header) throw { message: `Couldn't find header from query, ${query}` }
|
|
10
11
|
const children = header.children.array().filter(c => c.tagName === 'a') as HTMLAnchorElement[];
|
|
11
12
|
const currentPage = document.location.href;
|
|
12
13
|
|
package/src/Functions/index.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { Arrayable, IElement } from "../Types"
|
|
2
|
+
|
|
3
|
+
type Events = Record<
|
|
4
|
+
`on${Capitalize<keyof HTMLElementEventMap>}`,
|
|
5
|
+
(event: Event) => void
|
|
6
|
+
>
|
|
3
7
|
|
|
4
8
|
/**
|
|
5
9
|
* Construction options when creating an HTML element using:
|
|
6
|
-
* @see Document.createProperElement
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
export default ElementOptions
|
|
10
|
+
* @see Document.createProperElement
|
|
11
|
+
* @borwwos IElement
|
|
12
|
+
* @borrows Arrayable
|
|
13
|
+
*/
|
|
14
|
+
export type ElementOptions = Partial<
|
|
15
|
+
Events & Record<string, any> & {
|
|
16
|
+
id: string,
|
|
17
|
+
class: Arrayable<string>;
|
|
18
|
+
children: Arrayable<IElement>;
|
|
19
|
+
dataset: Record<string, string>
|
|
20
|
+
}>
|
|
21
|
+
export default ElementOptions;
|
package/src/Interfaces/index.ts
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Construct a type with the properties of Type except for those in type Properties.
|
|
3
|
+
*/
|
|
4
|
+
export type BetterOmit<Type, Properties extends keyof Type> = Omit<Type, Properties>;
|
|
5
|
+
/**
|
|
6
|
+
* Extract from From those types that are assignable to Properties
|
|
7
|
+
*/
|
|
8
|
+
export type BetterExtract<From, Properties extends From> = Extract<From, Properties>;
|
|
9
|
+
|
|
10
|
+
export type PartialExcept<From, Properties extends keyof From> = Partial<From> & Required<Pick<From, Properties>>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type LongMonth = 'Janurary' | 'February' | 'March' | 'April' | 'May' | 'June' | 'July' | 'August' | 'September' | 'October' | 'November' | 'December';
|
|
2
|
+
export type ShortMonth = 'Jan' | 'Feb' | 'Mar' | 'Apr' | 'May' | 'Jun' | 'Jul' | 'Aug' | 'Sep' | 'Oct' | 'Nov' | 'Dec';
|
|
3
|
+
export type Month = LongMonth | ShortMonth;
|
|
4
|
+
|
|
5
|
+
export type ShortDay = 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun';
|
|
6
|
+
export type LongDay = `${'Mon' | 'Tues' | 'Wednes' | 'Thurs' | 'Fri' | 'Satur' | 'Sun'}day`;
|
|
7
|
+
export type Day = ShortDay | LongDay;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default eventhandler mapper. Object with properties that are arrays
|
|
3
|
+
*/
|
|
4
|
+
export type BaseEvent<Keys extends string, Types extends Array<any>> = Record<Keys, Types>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Eventhandler type for:
|
|
8
|
+
* @see EventCollection
|
|
9
|
+
* @borrows BaseEvent
|
|
10
|
+
*/
|
|
11
|
+
export type EventHandler<
|
|
12
|
+
Events extends BaseEvent<string, Array<any>> = BaseEvent<string, Array<any>>,
|
|
13
|
+
Event extends keyof Events = keyof Events,
|
|
14
|
+
ReturnType = any
|
|
15
|
+
> = (...args: Events[Event]) => ReturnType;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters all properties from From that has the return type of Type
|
|
3
|
+
*/
|
|
4
|
+
export type PropertiesWith<Type, From> = {
|
|
5
|
+
[Key in keyof From as From[Key] extends Type ? Key : never]: From[Key]
|
|
6
|
+
}
|
|
7
|
+
export default PropertiesWith;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Fitlers all properties from From that don't have the return type of Type
|
|
11
|
+
*/
|
|
12
|
+
export type PropertiesWithout<Type, From> = {
|
|
13
|
+
[Key in keyof From as From[Key] extends Type ? never : Key]: From[Key]
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts Start types to Switch types in From type
|
|
3
|
+
*/
|
|
4
|
+
export type TransformType<From, Start, Switch> = {
|
|
5
|
+
[Key in keyof From]: From[Key] extends Start ? Switch : From[Key]
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns object with properties matching BaseType with types of NewType
|
|
10
|
+
*/
|
|
11
|
+
export type TransformTypes<From, BaseType, NewType> = Record<keyof {
|
|
12
|
+
[Key in keyof From as From[Key] extends BaseType ? Key : never]: Key
|
|
13
|
+
}, NewType>
|
|
14
|
+
|
|
15
|
+
//Returns From with properties switched from BaseType to NewType
|
|
16
|
+
// export type TransformTypes<From, BaseType, NewType> = TransformType<From, BaseType[keyof BaseType], NewType>;
|
|
17
|
+
|
|
18
|
+
export default TransformTypes;
|
package/src/Types/index.ts
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
1
|
+
export * from './BetterTypes';
|
|
2
|
+
export * from './Date';
|
|
3
|
+
export * from './Events';
|
|
4
|
+
export * from './TransformTypes';
|
|
5
|
+
export * from './PropertiesWith';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Item is single or multiple
|
|
9
|
+
*/
|
|
10
|
+
export type Arrayable<T> = T | Array<T>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Used for HTMLElement.append in ElementOptions, Document.createProperElement.
|
|
14
|
+
* IElement accepts HTML Elements or HTMl-like strings.
|
|
15
|
+
*
|
|
16
|
+
* @see HTMLElement.append
|
|
17
|
+
* @see Document.createProperElement
|
|
18
|
+
*/
|
|
19
|
+
export type IElement = HTMLElement | string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Return types of T
|
|
23
|
+
*/
|
|
24
|
+
export type ValueOf<T> = T[keyof T];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Type's properties are ReturnType
|
|
28
|
+
*/
|
|
29
|
+
export type AllPropsAre<ReturnType> = {
|
|
30
|
+
[key: string]: ReturnType
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* string or RegExp.. pretty self-explanatory
|
|
35
|
+
*/
|
|
36
|
+
export type StringRegex = string | RegExp;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
export * as Extensions from './Extensions';
|
|
1
|
+
export * from './Classes';
|
|
2
|
+
export * from './Extensions';
|
|
3
|
+
export * from './Functions';
|
|
4
|
+
export * from './Interfaces';
|
|
5
|
+
export * from './Types';
|