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.
Files changed (68) hide show
  1. package/.gitattributes +2 -2
  2. package/dist/Classes/Events/Event.js +1 -1
  3. package/dist/Classes/Events/EventCollection.js +1 -1
  4. package/dist/Classes/Events/EventEmitter.d.ts +28 -4
  5. package/dist/Classes/Events/EventEmitter.js +24 -0
  6. package/dist/Classes/Time/Date.d.ts +5 -5
  7. package/dist/Classes/Time/Time.d.ts +3 -3
  8. package/dist/Classes/Time/TimeProperties.d.ts +2 -2
  9. package/dist/Classes/Time/TimeSpan.d.ts +1 -1
  10. package/dist/Classes/index.d.ts +1 -0
  11. package/dist/Classes/index.js +1 -0
  12. package/dist/Classes/store.d.ts +79 -0
  13. package/dist/Classes/store.js +84 -0
  14. package/dist/Extensions/Array.d.ts +8 -1
  15. package/dist/Extensions/Array.js +47 -10
  16. package/dist/Extensions/Document.d.ts +27 -0
  17. package/dist/Extensions/Document.js +32 -0
  18. package/dist/Extensions/Function.d.ts +14 -0
  19. package/dist/Extensions/Function.js +10 -0
  20. package/dist/Extensions/Map.d.ts +17 -1
  21. package/dist/Extensions/Map.js +24 -13
  22. package/dist/Extensions/Object/index.d.ts +49 -0
  23. package/dist/Extensions/Object/index.js +38 -0
  24. package/dist/Extensions/Object/properties.d.ts +28 -0
  25. package/dist/Extensions/Object/properties.js +20 -0
  26. package/dist/Extensions/String.d.ts +11 -1
  27. package/dist/Extensions/String.js +15 -7
  28. package/dist/Extensions/index.d.ts +5 -17
  29. package/dist/Extensions/index.js +8 -49
  30. package/dist/Functions/GetCSSProperty.d.ts +1 -1
  31. package/dist/Functions/GetNestedProperty.d.ts +9 -0
  32. package/dist/Functions/GetNestedProperty.js +23 -0
  33. package/dist/Functions/index.d.ts +1 -0
  34. package/dist/Functions/index.js +1 -0
  35. package/dist/Interfaces/ElementOptions.d.ts +4 -4
  36. package/dist/Types/BetterTypes.d.ts +3 -3
  37. package/dist/Types/Date.d.ts +6 -6
  38. package/dist/Types/Events.d.ts +2 -2
  39. package/dist/Types/PropertiesWith.d.ts +2 -2
  40. package/dist/Types/TransformTypes.d.ts +2 -2
  41. package/dist/Types/index.d.ts +21 -6
  42. package/docs/Classes.md +33 -0
  43. package/docs/Extensions.md +7 -0
  44. package/docs/Functions.md +8 -0
  45. package/docs/Types.md +30 -17
  46. package/package.json +6 -3
  47. package/src/Classes/Events/Event.ts +1 -1
  48. package/src/Classes/Events/EventCollection.ts +1 -1
  49. package/src/Classes/Events/EventEmitter.ts +40 -5
  50. package/src/Classes/index.ts +2 -1
  51. package/src/Classes/store.ts +98 -0
  52. package/src/Extensions/Array.ts +49 -11
  53. package/src/Extensions/Document.ts +58 -0
  54. package/src/Extensions/Function.ts +18 -0
  55. package/src/Extensions/Map.ts +24 -9
  56. package/src/Extensions/Object/index.ts +82 -0
  57. package/src/Extensions/Object/properties.ts +51 -0
  58. package/src/Extensions/String.ts +17 -6
  59. package/src/Extensions/index.ts +7 -66
  60. package/src/Functions/GetNestedProperty.ts +29 -0
  61. package/src/Functions/index.ts +1 -0
  62. package/src/Interfaces/ElementOptions.ts +2 -2
  63. package/src/Types/index.ts +27 -2
  64. package/tsconfig.json +2 -2
  65. package/Time.xlsx +0 -0
  66. package/dist/Extensions/Object.d.ts +0 -16
  67. package/dist/Extensions/Object.js +0 -8
  68. package/src/Extensions/Object.ts +0 -25
@@ -1,6 +1,6 @@
1
- import ElementOptions from "../Interfaces/ElementOptions";
2
- import { EventHandler, IElement } from "../Types";
1
+
3
2
  export * from './Array';
3
+ export * from './Document';
4
4
  export * from './Map';
5
5
  export * from './Object';
6
6
  export * from './String';
@@ -12,72 +12,13 @@ declare global {
12
12
  */
13
13
  parseBoolean(value: string): boolean
14
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
15
  }
32
16
 
33
- Boolean.parseBoolean = function(value: string) {
17
+ function parseBoolean(value: string) {
34
18
  return value === "true";
35
19
  };
20
+ Boolean.parseBoolean = parseBoolean;
36
21
 
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
- }
22
+ export const BooleanExtensions = {
23
+ parseBoolean
24
+ };
@@ -0,0 +1,29 @@
1
+ export type GetNestedProperty<Parent, Key extends string> =
2
+ Key extends keyof Parent
3
+ ? Parent[Key]
4
+ : Key extends `${infer K}.${infer Rest}`
5
+ ? K extends keyof Parent
6
+ ? GetNestedProperty<Parent[K], Rest>
7
+ : never
8
+ : never;
9
+
10
+ /**
11
+ * Gets a nested property from an object
12
+ * @param parent Parent object to search
13
+ * @param key Key to search for. Can be nested with dot notation
14
+ * @returns Value of key or null if not found
15
+ */
16
+ export function GetNestedProperty<Parent extends object, Key extends string>(parent: Parent, key: Key): GetNestedProperty<Parent, Key> | null {
17
+ if (key in parent) return parent[key as any as keyof Parent] as any;
18
+
19
+ for (const prop in parent) {
20
+ if (typeof parent[prop] === 'object') {
21
+ const result = GetNestedProperty((parent as any)[prop], key);
22
+ if (result) return result as any;
23
+ }
24
+ }
25
+
26
+ return null;
27
+ }
28
+
29
+ export default GetNestedProperty;
@@ -1,4 +1,5 @@
1
1
  export * from './CopyToClipboard';
2
2
  export * from './GetCSSProperty';
3
+ export * from './GetNestedProperty';
3
4
  export * from './SetNavigationSelected';
4
5
  export * from './HTMLEvent';
@@ -7,14 +7,14 @@ type Events = Record<
7
7
 
8
8
  /**
9
9
  * Construction options when creating an HTML element using:
10
- * @see Document.createProperElement
10
+ * @see Document.createElement
11
11
  * @borwwos IElement
12
12
  * @borrows Arrayable
13
13
  */
14
14
  export type ElementOptions = Partial<
15
15
  Events & Record<string, any> & {
16
16
  id: string,
17
- class: Arrayable<string>;
17
+ className: Arrayable<string>;
18
18
  children: Arrayable<IElement>;
19
19
  dataset: Record<string, string>
20
20
  }>
@@ -9,12 +9,17 @@ export * from './PropertiesWith';
9
9
  */
10
10
  export type Arrayable<T> = T | Array<T>;
11
11
 
12
+ /**
13
+ * Item is function or T
14
+ */
15
+ export type Functionable<T> = T | (() => T);
16
+
12
17
  /**
13
18
  * Used for HTMLElement.append in ElementOptions, Document.createProperElement.
14
19
  * IElement accepts HTML Elements or HTMl-like strings.
15
20
  *
16
21
  * @see HTMLElement.append
17
- * @see Document.createProperElement
22
+ * @see Document.createElement
18
23
  */
19
24
  export type IElement = HTMLElement | string;
20
25
 
@@ -33,4 +38,24 @@ export type AllPropsAre<ReturnType> = {
33
38
  /**
34
39
  * string or RegExp.. pretty self-explanatory
35
40
  */
36
- export type StringRegex = string | RegExp;
41
+ export type StringRegex = string | RegExp;
42
+
43
+ export type If<Boolean extends boolean, True, False> = Boolean extends true ? True : False;
44
+
45
+ /**
46
+ * GetRepeatedKeys<[
47
+ * { username: string, password: string },
48
+ * { username: number, email: string },
49
+ * ]> // { username: string | number }
50
+ */
51
+ export type GetRepeatedKeys<Types extends Array<any>> = (
52
+ Types extends [infer First, infer Second, ...infer Rest]
53
+ ? First extends object
54
+ ? Second extends object
55
+ ? {
56
+ [Key in Extract<keyof First, keyof Second>]: First[Key] | Second[Key]
57
+ } & GetRepeatedKeys<Rest>
58
+ : {}
59
+ : {}
60
+ : {}
61
+ );
package/tsconfig.json CHANGED
@@ -89,7 +89,7 @@
89
89
  // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
90
90
  // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
91
91
  // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
92
- // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
92
+ "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
93
93
  // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
94
94
  // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
95
95
  // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
@@ -97,6 +97,6 @@
97
97
  /* Completeness */
98
98
  "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
99
99
  "skipLibCheck": true, /* Skip type checking all .d.ts files. */
100
- "watch": true,
100
+ "keyofStringsOnly": false, /* Make keyof only return strings instead of string, numbers or symbols. */
101
101
  },
102
102
  }
package/Time.xlsx DELETED
Binary file
@@ -1,16 +0,0 @@
1
- import { ValueOf } from "../Types";
2
- export {};
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 = {}>(from: From): Array<[keyof From, ValueOf<From>]>;
10
- /**
11
- * Destructures object into array of property keys
12
- * @param from Object to destruct
13
- */
14
- keysOf<From = {}>(from: From): Array<keyof From>;
15
- }
16
- }
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- Object.keysOf = function (from) {
4
- return Object.keys(from);
5
- };
6
- Object.array = function (from) {
7
- return Object.keysOf(from).map(prop => [prop, from[prop]]);
8
- };
@@ -1,25 +0,0 @@
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
- }