danholibraryjs 1.6.0 → 1.7.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 -345
- 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} +9 -10
- package/dist/Classes/{EventCollection.js → Events/EventCollection.js} +27 -29
- package/dist/Classes/{EventEmitter.d.ts → Events/EventEmitter.d.ts} +5 -6
- package/dist/Classes/{EventEmitter.js → Events/EventEmitter.js} +6 -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/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 +9 -14
- 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 +455 -0
- package/docs/Extensions.md +146 -0
- package/docs/Functions.md +53 -0
- package/docs/Interfaces.md +34 -0
- package/docs/Types.md +129 -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} +9 -10
- 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/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 +15 -12
- 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 -96
- package/dist/Extensions.js +0 -90
- package/dist/Interfaces/BaseEventInterface.d.ts +0 -7
- package/dist/Interfaces/IHTMLEvent.d.ts +0 -4
- package/dist/Types/EventHandler.d.ts +0 -8
- package/dist/Types/IElement.d.ts +0 -9
- package/dist/Types/StringRegex.d.ts +0 -2
- package/src/Classes/EventCollection.ts +0 -117
- package/src/Extensions.ts +0 -202
- package/src/Interfaces/BaseEventInterface.ts +0 -7
- package/src/Types/EventHandler.ts +0 -13
- package/src/Types/IElement.ts +0 -9
- package/src/Types/StringRegex.ts +0 -2
- package/test.js +0 -20
package/dist/Extensions.d.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import ElementOptions from "./Interfaces/ElementOptions";
|
|
2
|
-
import IReplacement from "./Interfaces/IReplacement";
|
|
3
|
-
declare global {
|
|
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
|
-
*/
|
|
10
|
-
createProperElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementOptions): HTMLElementTagNameMap[K];
|
|
11
|
-
}
|
|
12
|
-
interface HTMLCollection {
|
|
13
|
-
/**
|
|
14
|
-
* Converts HTMLCollection to Element[]
|
|
15
|
-
*/
|
|
16
|
-
array(): Element[];
|
|
17
|
-
}
|
|
18
|
-
interface Array<T> {
|
|
19
|
-
/**
|
|
20
|
-
* Pushes items to array and returns self with new items
|
|
21
|
-
* @param items Items to add to array
|
|
22
|
-
*/
|
|
23
|
-
add(...items: Array<T>): this;
|
|
24
|
-
/**
|
|
25
|
-
* Removes item from array and returns self without item
|
|
26
|
-
* @param item Item to remove
|
|
27
|
-
*/
|
|
28
|
-
remove(item: T): this;
|
|
29
|
-
/**
|
|
30
|
-
* Returns a random element from array
|
|
31
|
-
*/
|
|
32
|
-
random(): T;
|
|
33
|
-
/**
|
|
34
|
-
* Returns item matching index. If negative number, subtracts number from length
|
|
35
|
-
* @param i Index of item
|
|
36
|
-
*/
|
|
37
|
-
index(i: number): T;
|
|
38
|
-
}
|
|
39
|
-
interface Map<K, V> {
|
|
40
|
-
/**
|
|
41
|
-
* Converts into Array<[Key, Value]>
|
|
42
|
-
*/
|
|
43
|
-
array(): [K, V][];
|
|
44
|
-
/**
|
|
45
|
-
* Maps values into new types of generics
|
|
46
|
-
* @param callback Callbacking function to map values
|
|
47
|
-
*/
|
|
48
|
-
map<EK, EV>(callback: (value: V, key?: K, index?: number, map?: this) => [EK, EV]): Map<EK, EV>;
|
|
49
|
-
/**
|
|
50
|
-
* Returns array of "accepted" values. Criteria defined in callback param
|
|
51
|
-
* @param callback Callbacking function to filter away unwanted values
|
|
52
|
-
*/
|
|
53
|
-
filter(callback: (value: V, key?: K, index?: number, map?: Map<K, V>) => boolean): Map<K, V>;
|
|
54
|
-
/**
|
|
55
|
-
* Returns array of keys
|
|
56
|
-
*/
|
|
57
|
-
keyArr(): Array<K>;
|
|
58
|
-
/**
|
|
59
|
-
* Returns array of values
|
|
60
|
-
*/
|
|
61
|
-
valueArr(): Array<V>;
|
|
62
|
-
/**
|
|
63
|
-
* Returns first [key, value] match to callback param
|
|
64
|
-
* @param callback Callbacking function to find KeyValuePair
|
|
65
|
-
*/
|
|
66
|
-
find(callback: (value: V, key?: K, index?: number, map?: Map<K, V>) => boolean): [K, V];
|
|
67
|
-
/**
|
|
68
|
-
* Whether or not map includes a value. Returns true if it does, false if not ¯\_(ツ)_/¯
|
|
69
|
-
* @param value Value that may be includded in map
|
|
70
|
-
* @param fromIndex Start looking for value from specific index+. Default: 0
|
|
71
|
-
*/
|
|
72
|
-
includes(value: V, fromIndex?: number): boolean;
|
|
73
|
-
}
|
|
74
|
-
interface String {
|
|
75
|
-
/**
|
|
76
|
-
* Uppercases first letter of string
|
|
77
|
-
*/
|
|
78
|
-
toPascalCase(): string;
|
|
79
|
-
/**
|
|
80
|
-
* Replaces "replacer" (default: ' ') with "replacement" (default: '_')
|
|
81
|
-
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
82
|
-
*/
|
|
83
|
-
toSnakeCase(replaceOptions?: IReplacement): string;
|
|
84
|
-
/**
|
|
85
|
-
* Replaces "replacer" (default: ' ') with "replacement" (default: '-')
|
|
86
|
-
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
87
|
-
*/
|
|
88
|
-
toKebabCase(replaceOptions?: IReplacement): string;
|
|
89
|
-
/**
|
|
90
|
-
* String.substring but accepting negative numbers to cut from length
|
|
91
|
-
* @param start Start of string. 0 indexed
|
|
92
|
-
* @param end End of string. 0 indexed, if negative number, substracts number from length
|
|
93
|
-
*/
|
|
94
|
-
clip(start: number, end?: number): string;
|
|
95
|
-
}
|
|
96
|
-
}
|
package/dist/Extensions.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
Document.prototype.createProperElement = function (tagName, options) {
|
|
4
|
-
let baseElement = document.createElement(tagName);
|
|
5
|
-
if (!options)
|
|
6
|
-
return baseElement;
|
|
7
|
-
if (options.classes) {
|
|
8
|
-
baseElement.classList.add(...options.classes);
|
|
9
|
-
}
|
|
10
|
-
if (options.attributes) {
|
|
11
|
-
options.attributes.forEach(([attribute, value]) => baseElement.setAttribute(attribute, value));
|
|
12
|
-
}
|
|
13
|
-
if (options.children) {
|
|
14
|
-
baseElement.append(...[].concat(options.children));
|
|
15
|
-
}
|
|
16
|
-
if (options.events) {
|
|
17
|
-
options.events.forEach(({ name, handler }) => (baseElement.addEventListener(name, handler)));
|
|
18
|
-
}
|
|
19
|
-
return baseElement;
|
|
20
|
-
};
|
|
21
|
-
HTMLCollection.prototype.array = function () {
|
|
22
|
-
let result = new Array();
|
|
23
|
-
for (let i = 0; i < this.length; i++) {
|
|
24
|
-
result.push(this.item(i));
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
27
|
-
};
|
|
28
|
-
Array.prototype.add = function (...items) {
|
|
29
|
-
this.push(...items);
|
|
30
|
-
return this;
|
|
31
|
-
};
|
|
32
|
-
Array.prototype.remove = function (item) {
|
|
33
|
-
const itemInArray = this.includes(item) ? item : this.find(i => i == item);
|
|
34
|
-
if (!itemInArray)
|
|
35
|
-
throw new Error(`item is not in array!`);
|
|
36
|
-
const itemIndex = this.indexOf(itemInArray);
|
|
37
|
-
this.splice(itemIndex, 1);
|
|
38
|
-
return this;
|
|
39
|
-
};
|
|
40
|
-
Array.prototype.random = function () {
|
|
41
|
-
const randomIndex = Math.round(Math.random() * this.length);
|
|
42
|
-
return this[randomIndex];
|
|
43
|
-
};
|
|
44
|
-
Array.prototype.index = function (i) {
|
|
45
|
-
return this[i < 0 ? this.length + i : i];
|
|
46
|
-
};
|
|
47
|
-
Map.prototype.array = function () {
|
|
48
|
-
let result = new Array();
|
|
49
|
-
for (const kvp of this) {
|
|
50
|
-
result.push(kvp);
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
};
|
|
54
|
-
Map.prototype.map = function (callback) {
|
|
55
|
-
return this.array()
|
|
56
|
-
.map(([k, v], i) => callback(v, k, i, this))
|
|
57
|
-
.reduce((map, [key, value]) => map.set(key, value), new Map());
|
|
58
|
-
};
|
|
59
|
-
Map.prototype.filter = function (callback) {
|
|
60
|
-
return this.array()
|
|
61
|
-
.filter(([k, v], i) => callback(v, k, i, this))
|
|
62
|
-
.reduce((map, [key, value]) => map.set(key, value), new Map());
|
|
63
|
-
};
|
|
64
|
-
Map.prototype.keyArr = function () {
|
|
65
|
-
return this.array().map(([k]) => k);
|
|
66
|
-
};
|
|
67
|
-
Map.prototype.valueArr = function () {
|
|
68
|
-
return this.array().map(([_, v]) => v);
|
|
69
|
-
};
|
|
70
|
-
Map.prototype.find = function (callback) {
|
|
71
|
-
return this.array().find(([k, v], i) => callback(v, k, i, this));
|
|
72
|
-
};
|
|
73
|
-
Map.prototype.includes = function (item, fromIndex) {
|
|
74
|
-
return this.valueArr().includes(item, fromIndex);
|
|
75
|
-
};
|
|
76
|
-
String.prototype.toPascalCase = function () {
|
|
77
|
-
return this.substring(0, 1).toUpperCase() + this.substring(1);
|
|
78
|
-
};
|
|
79
|
-
function spaceReplacer(self, replacer, replacement) {
|
|
80
|
-
return self.replace(new RegExp(`${typeof replacer == 'string' ? replacer : replacer.source}+`), replacement);
|
|
81
|
-
}
|
|
82
|
-
String.prototype.toSnakeCase = function (replaceOptions = { replacer: ' ', replacement: '_' }) {
|
|
83
|
-
return spaceReplacer(this, replaceOptions.replacer, replaceOptions.replacement);
|
|
84
|
-
};
|
|
85
|
-
String.prototype.toKebabCase = function (replaceOptions = { replacer: ' ', replacement: '-' }) {
|
|
86
|
-
return spaceReplacer(this, replaceOptions.replacer, replaceOptions.replacement);
|
|
87
|
-
};
|
|
88
|
-
String.prototype.clip = function (start, end) {
|
|
89
|
-
return this.substring(start, end < 0 ? this.length + end : end);
|
|
90
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import BaseEvent from "../Interfaces/BaseEventInterface";
|
|
2
|
-
/**
|
|
3
|
-
* Eventhandler type for:
|
|
4
|
-
* @see EventCollection
|
|
5
|
-
* @borrows BaseEvent
|
|
6
|
-
*/
|
|
7
|
-
export declare type EventHandler<Events extends BaseEvent, Event extends keyof Events, ReturnType = any> = (args: Events[Event]) => ReturnType;
|
|
8
|
-
export default EventHandler;
|
package/dist/Types/IElement.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Used for HTMLElement.append in ElementOptions, Document.createProperElement.
|
|
3
|
-
* IElement accepts HTML Elements or HTMl-like strings.
|
|
4
|
-
*
|
|
5
|
-
* @see HTMLElement.append
|
|
6
|
-
* @see Document.createProperElement
|
|
7
|
-
*/
|
|
8
|
-
export declare type IElement = HTMLElement | string;
|
|
9
|
-
export default IElement;
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import BaseEvent from "../Interfaces/BaseEventInterface";
|
|
2
|
-
import EventHandler from "../Types/EventHandler";
|
|
3
|
-
import Event from './Event';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Collection of Events from @see EventEmitter
|
|
7
|
-
* @borrows EventHandler
|
|
8
|
-
* @borrows Event
|
|
9
|
-
* @borrows BaseEvent
|
|
10
|
-
*/
|
|
11
|
-
export class EventCollection<Events extends BaseEvent> {
|
|
12
|
-
constructor(events?: Map<keyof Events, EventHandler<Events, keyof Events>[]>) {
|
|
13
|
-
this._events = !events ?
|
|
14
|
-
new Map<keyof Events, Event<Events>>() :
|
|
15
|
-
events.array().reduce((result, [event, handlers]) =>
|
|
16
|
-
result.set(event, new Event<Events>(event, ...handlers)),
|
|
17
|
-
new Map<keyof Events, Event<Events>>());
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**Amount of events stored*/
|
|
21
|
-
public get size() {
|
|
22
|
-
return this._events.size;
|
|
23
|
-
}
|
|
24
|
-
/**@private Internal event collection*/
|
|
25
|
-
private _events = new Map<keyof Events, Event<Events>>();
|
|
26
|
-
/**@private limit of events*/
|
|
27
|
-
private _limit = 0;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Returns true if event is in collection
|
|
31
|
-
* @param event Event name
|
|
32
|
-
* @returns true if event is in collection
|
|
33
|
-
*/
|
|
34
|
-
public has(event: keyof Events): boolean {
|
|
35
|
-
return this._events.has(event);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Returns event matching event parameter
|
|
39
|
-
* @param event Event name
|
|
40
|
-
* @returns Event
|
|
41
|
-
*/
|
|
42
|
-
public get(event: keyof Events): Event<Events> {
|
|
43
|
-
return this._events.get(event);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Adds handler to event collection with name as key
|
|
47
|
-
* @param name Event name
|
|
48
|
-
* @param handler Handler for event
|
|
49
|
-
* @returns this
|
|
50
|
-
*/
|
|
51
|
-
public add(name: keyof Events, handler: EventHandler<Events, keyof Events>, once = false): this {
|
|
52
|
-
if (this._limit > 0 && this._limit + 1 > this._events.size) {
|
|
53
|
-
throw new Error(`Listener limit, ${this._limit}, reached!`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const event = this.has(name) && this.get(name);
|
|
57
|
-
|
|
58
|
-
if (once) this._events.set(name, event ? event.once(handler) : new Event<Events>(name).once(handler))
|
|
59
|
-
else this._events.set(name, event ? event.on(handler) : new Event(name, handler));
|
|
60
|
-
return this;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* @summary clear(): Clears all events
|
|
64
|
-
* @summary clear("all", myEventHandler): Removes myEventHandler from all events that have it
|
|
65
|
-
* @summary clear("myEvent"): Clears all handlers tied to "myEvent"
|
|
66
|
-
* @summary clear("myEvent", myEventHandler): Removes myEventHandler from "myEvent"
|
|
67
|
-
*
|
|
68
|
-
* @param name Event name | "all"
|
|
69
|
-
* @param handler Specific handler to remove. If left blank, all handlers in name will be removed
|
|
70
|
-
* @returns this
|
|
71
|
-
*/
|
|
72
|
-
public clear(name: keyof Events | "all" = 'all', handler?: EventHandler<Events, keyof Events>): this {
|
|
73
|
-
//clear(): Clears all events
|
|
74
|
-
if ((name as string).toLowerCase() == 'all' && handler == null) this._events.clear();
|
|
75
|
-
|
|
76
|
-
//clear("all", myEventHandler): Removes the "myEventHandler" handler from all events
|
|
77
|
-
else if ((name as string).toLowerCase() == 'all' && handler) this._events = (() => {
|
|
78
|
-
const eventNames = this._events.array().map(([name, event]) => event.includes(handler) && name);
|
|
79
|
-
this._events.forEach((event, name) =>
|
|
80
|
-
eventNames.includes(name) &&
|
|
81
|
-
this._events.set(name, event.off(handler)
|
|
82
|
-
));
|
|
83
|
-
|
|
84
|
-
return this._events;
|
|
85
|
-
})();
|
|
86
|
-
|
|
87
|
-
//clear("myEvent"): Deletes myEvent from this._events
|
|
88
|
-
else if ((name as string).toLowerCase() != "all" && handler == null) this._events.delete(name as keyof Events);
|
|
89
|
-
|
|
90
|
-
//clear("myEvent", myEventHandler): Removes the "myEventsHandler" handler from "myEvent"
|
|
91
|
-
else if ((name as string).toLowerCase() != 'all' && handler) this._events.set(name as keyof Events, this.get(name as keyof Events).off(handler));
|
|
92
|
-
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public emit<Event extends keyof Events>(name: Event, args: Events[Event]) {
|
|
97
|
-
return this.get(name).emit(args);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Limits how many events to accept using EventEmitter#on or EventEmitter#once
|
|
102
|
-
* @param limit Limit of events to keep
|
|
103
|
-
* @returns this with the new limit
|
|
104
|
-
*
|
|
105
|
-
* @throws Unknown event, if event name isn't recognized
|
|
106
|
-
*/
|
|
107
|
-
public limit<Event extends keyof Events>(event: 'all' | Event, limit: number) {
|
|
108
|
-
if (limit <= 0) return;
|
|
109
|
-
|
|
110
|
-
if (event == 'all') this._limit = limit;
|
|
111
|
-
else if (this.has(event)) this.get(event).limit = limit;
|
|
112
|
-
else throw new Error(`Unknown event, ${event}!`);
|
|
113
|
-
|
|
114
|
-
return this;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
export default EventCollection;
|
package/src/Extensions.ts
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import ElementOptions from "./Interfaces/ElementOptions";
|
|
2
|
-
import IReplacement from "./Interfaces/IReplacement";
|
|
3
|
-
|
|
4
|
-
declare global {
|
|
5
|
-
interface Document {
|
|
6
|
-
/**
|
|
7
|
-
* Creates an element like Document#createElement, however with construction options to assign values in construction instead of after construction.
|
|
8
|
-
* @param tagName HTMLElement tag name
|
|
9
|
-
* @param options Construction options, instead of assigning values after construction
|
|
10
|
-
*/
|
|
11
|
-
createProperElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementOptions): HTMLElementTagNameMap[K]
|
|
12
|
-
}
|
|
13
|
-
interface HTMLCollection {
|
|
14
|
-
/**
|
|
15
|
-
* Converts HTMLCollection to Element[]
|
|
16
|
-
*/
|
|
17
|
-
array(): Element[]
|
|
18
|
-
}
|
|
19
|
-
interface Array<T> {
|
|
20
|
-
/**
|
|
21
|
-
* Pushes items to array and returns self with new items
|
|
22
|
-
* @param items Items to add to array
|
|
23
|
-
*/
|
|
24
|
-
add(...items: Array<T>): this
|
|
25
|
-
/**
|
|
26
|
-
* Removes item from array and returns self without item
|
|
27
|
-
* @param item Item to remove
|
|
28
|
-
*/
|
|
29
|
-
remove(item: T): this
|
|
30
|
-
/**
|
|
31
|
-
* Returns a random element from array
|
|
32
|
-
*/
|
|
33
|
-
random(): T
|
|
34
|
-
/**
|
|
35
|
-
* Returns item matching index. If negative number, subtracts number from length
|
|
36
|
-
* @param i Index of item
|
|
37
|
-
*/
|
|
38
|
-
index(i: number): T
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
interface Map<K, V> {
|
|
42
|
-
/**
|
|
43
|
-
* Converts into Array<[Key, Value]>
|
|
44
|
-
*/
|
|
45
|
-
array(): [K, V][]
|
|
46
|
-
/**
|
|
47
|
-
* Maps values into new types of generics
|
|
48
|
-
* @param callback Callbacking function to map values
|
|
49
|
-
*/
|
|
50
|
-
map<EK, EV>(callback: (value: V, key?: K, index?: number, map?: this) => [EK, EV]): Map<EK, EV>
|
|
51
|
-
/**
|
|
52
|
-
* Returns array of "accepted" values. Criteria defined in callback param
|
|
53
|
-
* @param callback Callbacking function to filter away unwanted values
|
|
54
|
-
*/
|
|
55
|
-
filter(callback: (value: V, key?: K, index?: number, map?: Map<K, V>) => boolean): Map<K, V>
|
|
56
|
-
/**
|
|
57
|
-
* Returns array of keys
|
|
58
|
-
*/
|
|
59
|
-
keyArr(): Array<K>
|
|
60
|
-
/**
|
|
61
|
-
* Returns array of values
|
|
62
|
-
*/
|
|
63
|
-
valueArr(): Array<V>
|
|
64
|
-
/**
|
|
65
|
-
* Returns first [key, value] match to callback param
|
|
66
|
-
* @param callback Callbacking function to find KeyValuePair
|
|
67
|
-
*/
|
|
68
|
-
find(callback: (value: V, key?: K, index?: number, map?: Map<K, V>) => boolean): [K, V]
|
|
69
|
-
/**
|
|
70
|
-
* Whether or not map includes a value. Returns true if it does, false if not ¯\_(ツ)_/¯
|
|
71
|
-
* @param value Value that may be includded in map
|
|
72
|
-
* @param fromIndex Start looking for value from specific index+. Default: 0
|
|
73
|
-
*/
|
|
74
|
-
includes(value: V, fromIndex?: number): boolean
|
|
75
|
-
}
|
|
76
|
-
interface String {
|
|
77
|
-
/**
|
|
78
|
-
* Uppercases first letter of string
|
|
79
|
-
*/
|
|
80
|
-
toPascalCase(): string
|
|
81
|
-
/**
|
|
82
|
-
* Replaces "replacer" (default: ' ') with "replacement" (default: '_')
|
|
83
|
-
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
84
|
-
*/
|
|
85
|
-
toSnakeCase(replaceOptions?: IReplacement): string
|
|
86
|
-
/**
|
|
87
|
-
* Replaces "replacer" (default: ' ') with "replacement" (default: '-')
|
|
88
|
-
* @param replaceOptions This is practically your stereotypical String.replace, if you really want it to be
|
|
89
|
-
*/
|
|
90
|
-
toKebabCase(replaceOptions?: IReplacement): string,
|
|
91
|
-
/**
|
|
92
|
-
* String.substring but accepting negative numbers to cut from length
|
|
93
|
-
* @param start Start of string. 0 indexed
|
|
94
|
-
* @param end End of string. 0 indexed, if negative number, substracts number from length
|
|
95
|
-
*/
|
|
96
|
-
clip(start: number, end?: number): string
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
Document.prototype.createProperElement = function<K extends keyof HTMLElementTagNameMap>(this: Document, tagName: K, options?: ElementOptions) {
|
|
101
|
-
let baseElement = document.createElement(tagName);
|
|
102
|
-
if (!options) return baseElement;
|
|
103
|
-
|
|
104
|
-
if (options.classes) {
|
|
105
|
-
baseElement.classList.add(...options.classes);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (options.attributes) {
|
|
109
|
-
options.attributes.forEach(([attribute, value]) => baseElement.setAttribute(attribute, value));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (options.children) {
|
|
113
|
-
baseElement.append(...[].concat(options.children));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (options.events) {
|
|
117
|
-
options.events.forEach(({ name, handler }) => (
|
|
118
|
-
baseElement.addEventListener(name, handler)
|
|
119
|
-
))
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return baseElement;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
HTMLCollection.prototype.array = function(this: HTMLCollection) {
|
|
126
|
-
let result = new Array<Element>();
|
|
127
|
-
|
|
128
|
-
for (let i = 0; i < this.length; i++) {
|
|
129
|
-
result.push(this.item(i));
|
|
130
|
-
}
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
Array.prototype.add = function<T>(this: Array<T>, ...items: Array<T>) {
|
|
135
|
-
this.push(...items);
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
Array.prototype.remove = function<T>(this: Array<T>, item: T): Array<T> {
|
|
139
|
-
const itemInArray = this.includes(item) ? item : this.find(i => i == item);
|
|
140
|
-
if (!itemInArray) throw new Error(`item is not in array!`);
|
|
141
|
-
|
|
142
|
-
const itemIndex = this.indexOf(itemInArray);
|
|
143
|
-
this.splice(itemIndex, 1);
|
|
144
|
-
return this;
|
|
145
|
-
}
|
|
146
|
-
Array.prototype.random = function<T>(this: Array<T>): T {
|
|
147
|
-
const randomIndex = Math.round(Math.random() * this.length);
|
|
148
|
-
return this[randomIndex];
|
|
149
|
-
}
|
|
150
|
-
Array.prototype.index = function<T>(this: Array<T>, i: number): T {
|
|
151
|
-
return this[i < 0 ? this.length + i : i];
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
Map.prototype.array = function<K, V>(this: Map<K, V>): [K, V][] {
|
|
155
|
-
let result = new Array<[K, V]>();
|
|
156
|
-
for (const kvp of this) {
|
|
157
|
-
result.push(kvp);
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
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> {
|
|
162
|
-
return this.array()
|
|
163
|
-
.map(([k, v], i) => callback(v, k, i, this))
|
|
164
|
-
.reduce((map, [key, value]) =>
|
|
165
|
-
map.set(key, value),
|
|
166
|
-
new Map<EK, EV>())
|
|
167
|
-
}
|
|
168
|
-
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> {
|
|
169
|
-
return this.array()
|
|
170
|
-
.filter(([k, v], i) => callback(v, k, i, this))
|
|
171
|
-
.reduce((map, [key, value]) =>
|
|
172
|
-
map.set(key, value),
|
|
173
|
-
new Map<K, V>())
|
|
174
|
-
}
|
|
175
|
-
Map.prototype.keyArr = function<K, V>(this: Map<K, V>): K[] {
|
|
176
|
-
return this.array().map(([k]) => k);
|
|
177
|
-
}
|
|
178
|
-
Map.prototype.valueArr = function<K, V>(this: Map<K, V>): V[] {
|
|
179
|
-
return this.array().map(([_, v]) => v);
|
|
180
|
-
}
|
|
181
|
-
Map.prototype.find = function<K, V>(this: Map<K, V>, callback: (value: V, key?: K, index?: number, map?: Map<K, V>) => boolean) {
|
|
182
|
-
return this.array().find(([k, v], i) => callback(v, k, i, this));
|
|
183
|
-
}
|
|
184
|
-
Map.prototype.includes = function<K, V>(this: Map<K, V>, item: V, fromIndex?: number) {
|
|
185
|
-
return this.valueArr().includes(item, fromIndex);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
String.prototype.toPascalCase = function(this: string) {
|
|
189
|
-
return this.substring(0, 1).toUpperCase() + this.substring(1);
|
|
190
|
-
}
|
|
191
|
-
function spaceReplacer(self: string, replacer: string | RegExp, replacement: string) {
|
|
192
|
-
return self.replace(new RegExp(`${typeof replacer == 'string' ? replacer : replacer.source}+`), replacement);
|
|
193
|
-
}
|
|
194
|
-
String.prototype.toSnakeCase = function(this: string, replaceOptions: IReplacement = { replacer: ' ', replacement: '_' }) {
|
|
195
|
-
return spaceReplacer(this, replaceOptions.replacer, replaceOptions.replacement)
|
|
196
|
-
}
|
|
197
|
-
String.prototype.toKebabCase = function(this: string, replaceOptions: IReplacement = { replacer: ' ', replacement: '-' }) {
|
|
198
|
-
return spaceReplacer(this, replaceOptions.replacer, replaceOptions.replacement);
|
|
199
|
-
}
|
|
200
|
-
String.prototype.clip = function(this: string, start: number, end?: number) {
|
|
201
|
-
return this.substring(start, end < 0 ? this.length + end : end);
|
|
202
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import BaseEvent from "../Interfaces/BaseEventInterface";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Eventhandler type for:
|
|
5
|
-
* @see EventCollection
|
|
6
|
-
* @borrows BaseEvent
|
|
7
|
-
*/
|
|
8
|
-
export type EventHandler<
|
|
9
|
-
Events extends BaseEvent,
|
|
10
|
-
Event extends keyof Events,
|
|
11
|
-
ReturnType = any
|
|
12
|
-
> = (args: Events[Event]) => ReturnType;
|
|
13
|
-
export default EventHandler;
|
package/src/Types/IElement.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Used for HTMLElement.append in ElementOptions, Document.createProperElement.
|
|
3
|
-
* IElement accepts HTML Elements or HTMl-like strings.
|
|
4
|
-
*
|
|
5
|
-
* @see HTMLElement.append
|
|
6
|
-
* @see Document.createProperElement
|
|
7
|
-
*/
|
|
8
|
-
export type IElement = HTMLElement | string;
|
|
9
|
-
export default IElement
|
package/src/Types/StringRegex.ts
DELETED
package/test.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const library = require('./dist');
|
|
2
|
-
|
|
3
|
-
const testElement = document.createProperElement('div', {
|
|
4
|
-
classes: ['test-elements'],
|
|
5
|
-
attributes: [],
|
|
6
|
-
children: [
|
|
7
|
-
document.createProperElement('h1', { children: 'Hello there' }),
|
|
8
|
-
document.createProperElement('label', {
|
|
9
|
-
children: [
|
|
10
|
-
'Test Element',
|
|
11
|
-
document.createProperElement('input', {
|
|
12
|
-
attributes: [['type', 'test'], ['placeholder', 'Test me here maybe? uwu']]
|
|
13
|
-
})
|
|
14
|
-
]
|
|
15
|
-
}),
|
|
16
|
-
document.createProperElement('button', {
|
|
17
|
-
events: [library.HTMLEvent('click', e => console.log('clicked!'))]
|
|
18
|
-
})
|
|
19
|
-
]
|
|
20
|
-
})
|