danholibraryjs 2.0.0 → 2.0.1

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 (62) hide show
  1. package/README.md +0 -1
  2. package/_package.github-release.json +6 -0
  3. package/_package.npm-release.json +6 -0
  4. package/dist/Extensions/Array/index.d.ts +6 -20
  5. package/dist/Extensions/Array/index.js +8 -26
  6. package/dist/Extensions/Object/index.d.ts +4 -10
  7. package/dist/Extensions/Object/index.js +6 -22
  8. package/dist/Extensions/Object/properties.extension.d.ts +28 -1
  9. package/dist/Extensions/Object/properties.extension.js +19 -2
  10. package/dist/Extensions/Object/properties.js +2 -1
  11. package/dist/Extensions/String/index.d.ts +1 -4
  12. package/dist/Extensions/String/index.js +3 -16
  13. package/dist/Extensions/index.d.ts +2 -2
  14. package/dist/Extensions/index.js +2 -2
  15. package/dist/index.d.ts +0 -1
  16. package/dist/index.js +0 -1
  17. package/docs/Classes.md +78 -3
  18. package/docs/Extensions.md +219 -78
  19. package/docs/Types.md +202 -58
  20. package/docs/index.md +0 -1
  21. package/package.json +1 -1
  22. package/src/Extensions/Array/index.ts +6 -15
  23. package/src/Extensions/Object/index.ts +4 -11
  24. package/src/Extensions/Object/properties.extension.ts +50 -2
  25. package/src/Extensions/Object/properties.ts +2 -1
  26. package/src/Extensions/String/index.ts +1 -5
  27. package/src/Extensions/index.ts +2 -2
  28. package/src/index.ts +0 -1
  29. package/dist/Extensions/Array.d.ts +0 -52
  30. package/dist/Extensions/Array.js +0 -51
  31. package/dist/Extensions/Document.d.ts +0 -27
  32. package/dist/Extensions/Document.js +0 -32
  33. package/dist/Extensions/String.d.ts +0 -36
  34. package/dist/Extensions/String.js +0 -25
  35. package/dist/Functions/CopyToClipboard.d.ts +0 -7
  36. package/dist/Functions/CopyToClipboard.js +0 -15
  37. package/dist/Functions/GetCSSProperty.d.ts +0 -15
  38. package/dist/Functions/GetCSSProperty.js +0 -26
  39. package/dist/Functions/GetNestedProperty.d.ts +0 -9
  40. package/dist/Functions/GetNestedProperty.js +0 -23
  41. package/dist/Functions/HTMLEvent.d.ts +0 -11
  42. package/dist/Functions/HTMLEvent.js +0 -14
  43. package/dist/Functions/SetNavigationSelected.d.ts +0 -9
  44. package/dist/Functions/SetNavigationSelected.js +0 -25
  45. package/dist/Functions/index.d.ts +0 -5
  46. package/dist/Functions/index.js +0 -21
  47. package/dist/Utils/ApiUtil/ApiTypes.d.ts +0 -15
  48. package/dist/Utils/ApiUtil/ApiTypes.js +0 -15
  49. package/dist/Utils/ApiUtil/RequestUtil.d.ts +0 -19
  50. package/dist/Utils/ApiUtil/RequestUtil.js +0 -73
  51. package/dist/Utils/ApiUtil/index.d.ts +0 -20
  52. package/dist/Utils/ApiUtil/index.js +0 -33
  53. package/dist/Utils/FormUtil.d.ts +0 -6
  54. package/dist/Utils/FormUtil.js +0 -35
  55. package/docs/Functions.md +0 -61
  56. package/src/Extensions/Document.ts +0 -58
  57. package/src/Functions/CopyToClipboard.ts +0 -10
  58. package/src/Functions/GetCSSProperty.ts +0 -27
  59. package/src/Functions/GetNestedProperty.ts +0 -29
  60. package/src/Functions/HTMLEvent.ts +0 -13
  61. package/src/Functions/SetNavigationSelected.ts +0 -19
  62. package/src/Functions/index.ts +0 -5
package/docs/Types.md CHANGED
@@ -4,115 +4,244 @@
4
4
 
5
5
  ### General
6
6
 
7
- ``` ts
7
+ ```ts
8
8
  /**
9
- * Type's properties are ReturnType
9
+ * Used for HTMLElement.append in ElementOptions, Document.createElement.
10
+ * IElement accepts HTML Elements or HTML-like strings.
10
11
  */
11
- type AllPropsAre<ReturnType> = {
12
- [key: string]: ReturnType
13
- }
12
+ type IElement = HTMLElement | string;
14
13
 
15
14
  /**
16
- * Item is single or multiple
15
+ * string or RegExp
17
16
  */
18
- type Arrayable<T> = T | Array<T>;
17
+ type StringRegex = string | RegExp;
19
18
 
20
19
  /**
21
- * If Condition is true, Then, Else... pretty self-explanatory
20
+ * Conditional type - If Boolean is true, returns True, otherwise False
22
21
  */
23
- type If<Condition, Then, Else> = Condition extends true ? Then : Else;
22
+ type If<Boolean extends boolean, True, False> = Boolean extends true ? True : False;
24
23
 
25
24
  /**
26
- * Used for HTMLElement.append in ElementOptions, Document.createProperElement.
27
- * IElement accepts HTML Elements or HTMl-like strings.
28
- *
29
- * @see HTMLElement.append
30
- * @see Document.createProperElement
25
+ * Return types of T
31
26
  */
32
- type IElement = HTMLElement | string;
27
+ type ValueOf<T> = T[keyof T];
28
+ ```
33
29
 
30
+ ### Able
31
+
32
+ ```ts
34
33
  /**
35
- * string or RegExp.. pretty self-explanatory
34
+ * Value can be either T or a function that returns T
36
35
  */
37
- type StringRegex = string | RegExp;
36
+ type Functionable<T, Args extends any[] = []> = T | ((...args: Args) => T);
38
37
 
39
38
  /**
40
- * Return types of T
39
+ * Value can be either T or a Promise that resolves to T
41
40
  */
42
- type ValueOf<T> = T[keyof T];
41
+ type Promisable<T> = T | Promise<T>;
42
+
43
+ /**
44
+ * Value can be T or null
45
+ */
46
+ type Nullable<T> = T | null;
47
+
48
+ /**
49
+ * Removes null and undefined from T
50
+ */
51
+ type NonNullable<T> = T extends null | undefined ? never : T;
52
+ ```
53
+
54
+ ### Array
55
+
56
+ ```ts
57
+ /**
58
+ * Item is single or multiple (T or Array<T>)
59
+ */
60
+ type Arrayable<T> = T | Array<T>;
61
+
62
+ /**
63
+ * Item is single or wrapped in single-element array
64
+ */
65
+ type SingleArrayable<T> = T | [T];
66
+
67
+ /**
68
+ * Extracts type from Array<T>
69
+ */
70
+ type TFromArray<T> = T extends Array<infer U> ? U : never;
43
71
  ```
44
72
 
45
73
  ### BetterTypes
46
74
 
47
75
  ```ts
48
76
  /**
49
- * Construct a type with the properties of Type except for those in type Properties.
77
+ * Construct a type with the properties of Type except for those in Properties
50
78
  */
51
79
  type BetterOmit<Type, Properties extends keyof Type> = Omit<Type, Properties>;
80
+
52
81
  /**
53
82
  * Extract from From those types that are assignable to Properties
54
83
  */
55
84
  type BetterExtract<From, Properties extends From> = Extract<From, Properties>;
85
+
86
+ /**
87
+ * Partial type but with required properties
88
+ */
89
+ type PartialExcept<From, Properties extends keyof From> = Partial<From> & Required<Pick<From, Properties>>;
90
+ ```
91
+
92
+ ### C# Types
93
+
94
+ ```ts
95
+ /**
96
+ * GUID string
97
+ */
98
+ type Guid = string;
99
+
100
+ /**
101
+ * TimeSpan string format
102
+ */
103
+ type TimeSpanType = string;
104
+ ```
105
+
106
+ ### Date
107
+
108
+ ```ts
109
+ /**
110
+ * Long month names
111
+ */
112
+ type LongMonth = 'January' | 'February' | 'March' | 'April' | 'May' | 'June' | 'July' | 'August' | 'September' | 'October' | 'November' | 'December';
113
+
114
+ /**
115
+ * Short month names
116
+ */
117
+ type ShortMonth = 'Jan' | 'Feb' | 'Mar' | 'Apr' | 'May' | 'Jun' | 'Jul' | 'Aug' | 'Sep' | 'Oct' | 'Nov' | 'Dec';
118
+
119
+ /**
120
+ * Month name (long or short)
121
+ */
122
+ type Month = LongMonth | ShortMonth;
123
+
124
+ /**
125
+ * Short day names
126
+ */
127
+ type ShortDay = 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun';
128
+
129
+ /**
130
+ * Long day names
131
+ */
132
+ type LongDay = `${'Mon' | 'Tues' | 'Wednes' | 'Thurs' | 'Fri' | 'Satur' | 'Sun'}day`;
133
+
134
+ /**
135
+ * Day name (long or short)
136
+ */
137
+ type Day = ShortDay | LongDay;
138
+
139
+ /**
140
+ * Constructor type for DanhoDate
141
+ */
142
+ type DanhoDateConstructor = TimeProperties | string | number | Date;
143
+
144
+ /**
145
+ * Time properties object
146
+ */
147
+ type TimeProperties<Plural extends boolean = false> = Record<Plural extends true ? `${TimeKeys}s` : TimeKeys, number>;
148
+
149
+ /**
150
+ * What properties to include when using TimeSpan.toString()
151
+ */
152
+ type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>;
56
153
  ```
57
154
 
58
155
  ### Events
59
156
 
60
157
  ```ts
61
158
  /**
62
- * Default eventhandler mapper. Object with properties that are arrays
159
+ * Default event handler mapper. Object with properties that are arrays
63
160
  */
64
- type BaseEvent<Keys extends string, Types extends Array<any>> = Record<Keys, Types>;
161
+ type BaseEvent<Keys extends string, Types extends Array<any>> = Record<Keys, Types>;
65
162
 
66
- /**
67
- * Eventhandler type for:
68
- * @see EventCollection
69
- * @borrows BaseEvent
70
- */
71
- type EventHandler<
72
- Events extends BaseEvent<string, Array<any>> = BaseEvent<string, Array<any>>,
73
- Event extends keyof Events = keyof Events,
74
- ReturnType = any
75
- > = (...args: Events[Event]) => ReturnType;
163
+ /**
164
+ * Event handler type
165
+ */
166
+ type EventHandler<
167
+ Events extends BaseEvent<string, Array<any>> = BaseEvent<string, Array<any>>,
168
+ Event extends keyof Events = keyof Events,
169
+ ReturnType = any
170
+ > = (...args: Events[Event]) => ReturnType;
76
171
  ```
77
172
 
78
- ### PropertiesWith
173
+ ### Function
79
174
 
80
175
  ```ts
81
176
  /**
82
- * Filters all properties from From that has the return type of Type
177
+ * Changes return type of a function
83
178
  */
84
- type PropertiesWith<Type, From> = {
85
- [Key in keyof From as From[Key] extends Type ? Key : never]: From[Key]
86
- }
87
- default PropertiesWith;
179
+ type NewReturnType<Func extends (...args: any[]) => any, NewReturn> =
180
+ Func extends (...args: infer Args) => any ? (...args: Args) => NewReturn : never;
88
181
 
89
182
  /**
90
- * Fitlers all properties from From that don't have the return type of Type
183
+ * Wraps function return type in Promise
91
184
  */
92
- type PropertiesWithout<Type, From> = {
93
- [Key in keyof From as From[Key] extends Type ? never : Key]: From[Key]
94
- }
185
+ type PromisedReturn<Func extends (...args: any[]) => any> =
186
+ Func extends (...args: infer Args) => infer Return ? (...args: Args) => Promise<Return> : never;
187
+
188
+ /**
189
+ * Removes all function properties from type
190
+ */
191
+ type NoFunctions<T> = { [K in keyof T]: T[K] extends Function ? never : T[K] };
95
192
  ```
96
193
 
97
- ### Time
194
+ ### Object
98
195
 
99
196
  ```ts
100
197
  /**
101
- * Type used to construct DanhoDate.
102
- * @Data Partial TimeProperties except years & months
103
- * @DateFormat string as dd/MM/yyyy
198
+ * Type's properties are all ReturnType
199
+ */
200
+ type AllPropsAre<ReturnType> = {
201
+ [key: string]: ReturnType;
202
+ };
203
+ ```
204
+
205
+ ### PropertiesWith
206
+
207
+ ```ts
208
+ /**
209
+ * Filters all properties from From that have the return type of Type
210
+ */
211
+ type PropertiesWith<Type, From> = {
212
+ [Key in keyof From as From[Key] extends Type ? Key : never]: From[Key];
213
+ };
214
+
215
+ /**
216
+ * Filters all properties from From that don't have the return type of Type
104
217
  */
105
- type DanhoDateConstructor = Data | DateFormat | number | Date;
218
+ type PropertiesWithout<Type, From> = {
219
+ [Key in keyof From as From[Key] extends Type ? never : Key]: From[Key];
220
+ };
106
221
 
107
222
  /**
108
- * Object interface with keys above to number values. If Plural is true, all properties ends with 's'
223
+ * Gets keys that appear in all types in the array
109
224
  */
110
- type TimeProperties<Plural extends boolean = false> = Record<Plural extends true ? `${TimeKeys}s` : TimeKeys, number>
225
+ type GetRepeatedKeys<Types extends Array<any>> =
226
+ Types extends [infer First, ...infer Rest]
227
+ ? Rest extends Array<any>
228
+ ? keyof First & GetRepeatedKeys<Rest>
229
+ : keyof First
230
+ : never;
111
231
 
112
232
  /**
113
- * What properties to include when using TimeSpan.toString(format: TimeSpanFormat): string
233
+ * Filters types that have specific properties
114
234
  */
115
- type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>
235
+ type ModelWithProps<T, TProps extends (keyof T | string)> = T extends Record<TProps, any> ? T : never;
236
+ ```
237
+
238
+ ### String
239
+
240
+ ```ts
241
+ /**
242
+ * Autocomplete type - allows string literal autocomplete while still accepting any string
243
+ */
244
+ type Autocomplete<T> = T | (string & {});
116
245
  ```
117
246
 
118
247
  ### TransformTypes
@@ -122,21 +251,36 @@ type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolea
122
251
  * Converts Start types to Switch types in From type
123
252
  */
124
253
  type TransformType<From, Start, Switch> = {
125
- [Key in keyof From]: From[Key] extends Start ? Switch : From[Key]
126
- }
254
+ [Key in keyof From]: From[Key] extends Start ? Switch : From[Key];
255
+ };
127
256
 
128
257
  /**
129
258
  * Returns object with properties matching BaseType with types of NewType
130
259
  */
131
- type TransformTypes<From, BaseType, NewType> = Record<keyof {
132
- [Key in keyof From as From[Key] extends BaseType ? Key : never]: Key
133
- }, NewType>
260
+ type TransformTypes<From, BaseType, NewType> = Record<keyof {
261
+ [Key in keyof From as From[Key] extends BaseType ? Key : never]: Key;
262
+ }, NewType>;
263
+
264
+ /**
265
+ * Converts object to JSON-serializable type (removes functions, Date -> string, etc.)
266
+ */
267
+ type Json<T> = {
268
+ [K in keyof T]: T[K] extends Function
269
+ ? never
270
+ : T[K] extends Date
271
+ ? string
272
+ : T[K] extends object
273
+ ? Json<T[K]>
274
+ : T[K];
275
+ };
134
276
  ```
135
277
 
136
278
  ### Store
279
+
137
280
  ```ts
138
281
  /**
139
- * Reducer function to map wanted parameters when using @see Store.on(action, reducer);
282
+ * Reducer function for Store state updates
140
283
  */
141
- type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State
284
+ type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> =
285
+ (state: State, ...args: Types[Action]) => State;
142
286
  ```
package/docs/index.md CHANGED
@@ -24,6 +24,5 @@ import { ... } from 'DanhoLibraryJS';
24
24
 
25
25
  * [Classes](./Classes.md)
26
26
  * [Extensions](./Extensions.md)
27
- * [Functions](./Functions.md)
28
27
  * [Interfaces](./Interfaces.md)
29
28
  * [Types](./Types.md)
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "registry": "https://registry.npmjs.org/"
5
5
  },
6
- "version": "2.0.0",
6
+ "version": "2.0.1",
7
7
  "description": "Library for Javascript.",
8
8
  "main": "dist/index.js",
9
9
  "types": "dist/index.d.ts",
@@ -1,15 +1,6 @@
1
- import * as Array from './array.extension';
2
- import * as CRUD from './crud.extension';
3
- import * as Loop from './loop.extension';
4
- import * as Random from './random.extension';
5
- import * as Sort from './sort.extension';
6
- import * as String from './string.extension';
7
-
8
- export const ArrayExtensions = {
9
- ...Array,
10
- ...CRUD,
11
- ...Loop,
12
- ...Random,
13
- ...Sort,
14
- ...String,
15
- };
1
+ export * from './array.extension';
2
+ export * from './crud.extension';
3
+ export * from './loop.extension';
4
+ export * from './random.extension';
5
+ export * from './sort.extension';
6
+ export * from './string.extension';
@@ -1,11 +1,4 @@
1
- import * as Array from './arrays.extension';
2
- import * as Booleans from './booleans.extension';
3
- import * as Extracts from './extracts.extension';
4
- import * as Properties from './properties.extension';
5
-
6
- export const ObjectExtensions = {
7
- ...Array,
8
- ...Booleans,
9
- ...Extracts,
10
- ...Properties
11
- };
1
+ export * from './arrays.extension';
2
+ export * from './booleans.extension';
3
+ export * from './extracts.extension';
4
+ export * from './properties.extension';
@@ -1,4 +1,37 @@
1
- import { Properties, properties } from "./properties";
1
+ import { PropertiesWith, If } from '../../Types';
2
+ import { convertCase } from '../String/case.extension';
3
+
4
+ type PrimitiveMap = {
5
+ string: string;
6
+ number: number;
7
+ boolean: boolean;
8
+ undefined: undefined;
9
+ null: null;
10
+ object: object;
11
+ function: Function;
12
+ any: any;
13
+ Date: Date;
14
+ RegExp: RegExp;
15
+ Promise: Promise<any>;
16
+ Array: Array<any>;
17
+ Map: Map<any, any>;
18
+ Set: Set<any>;
19
+ };
20
+
21
+ /**
22
+ * Object with getPrimitiveTypes<Source, AllowFunctions extends boolean>(
23
+ * source: Source,
24
+ * allowFunctions: AllowFunctions = false
25
+ * ): Object with properties from source that matches primitive type
26
+ */
27
+ export type Properties = {
28
+ [Key in keyof PrimitiveMap as `get${Capitalize<Key>}s`]:
29
+ <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions?: AllowFunctions) =>
30
+ If<AllowFunctions,
31
+ PropertiesWith<PrimitiveMap[Key] | ((...args: any[]) => PrimitiveMap[Key]), Source>,
32
+ PropertiesWith<PrimitiveMap[Key], Source>
33
+ >
34
+ };
2
35
 
3
36
  declare global {
4
37
  interface ObjectConstructor {
@@ -6,6 +39,21 @@ declare global {
6
39
  }
7
40
  }
8
41
 
9
-
42
+ export const properties: Properties = [
43
+ 'string', 'number', 'boolean', 'undefined', 'null',
44
+ 'object', 'function', 'any',
45
+ 'Date', 'RegExp', 'Promise', 'Array', 'Map', 'Set'
46
+ ].reduce((result, primitive) => {
47
+ result[`get${convertCase('camel', 'pascal')}s` as keyof Properties] = function <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions: AllowFunctions = false as AllowFunctions) {
48
+ return Object.keysOf<Source>(source).reduce((result, key) => {
49
+ if ((source[key] as any).constructor.name === primitive ||
50
+ (withFunctions && typeof source[key] === 'function' && source[key] as any).constructor.name === primitive) {
51
+ result[key] = source[key];
52
+ }
53
+ return result;
54
+ }, {} as any);
55
+ };
56
+ return result;
57
+ }, {} as Properties);
10
58
 
11
59
  Object.properties = properties;
@@ -1,4 +1,5 @@
1
1
  import { PropertiesWith, If } from '../../Types';
2
+ import { convertCase } from '../String/case.extension';
2
3
 
3
4
  type PrimitiveMap = {
4
5
  string: string;
@@ -37,7 +38,7 @@ export const properties: Properties = [
37
38
  'object', 'function', 'any',
38
39
  'Date', 'RegExp', 'Promise', 'Array', 'Map', 'Set'
39
40
  ].reduce((result, primitive) => {
40
- result[`get${primitive.convertCase('camel', 'pascal')}s` as keyof Properties] = function <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions: AllowFunctions = false as AllowFunctions) {
41
+ result[`get${convertCase('camel', 'pascal')}s` as keyof Properties] = function <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions: AllowFunctions = false as AllowFunctions) {
41
42
  return Object.keysOf<Source>(source).reduce((result, key) => {
42
43
  if ((source[key] as any).constructor.name === primitive ||
43
44
  (withFunctions && typeof source[key] === 'function' && source[key] as any).constructor.name === primitive) {
@@ -1,5 +1 @@
1
- import * as Case from './case.extension';
2
-
3
- export const StringUtils = {
4
- ...Case,
5
- }
1
+ export * from './case.extension';
@@ -1,6 +1,6 @@
1
1
  export * from './Array';
2
- export * from './Document';
2
+ export * from './Function';
3
3
  export * from './Map';
4
- export * from './Object';
5
4
  export * from './Number';
5
+ export * from './Object';
6
6
  export * from './String';
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './Classes';
2
2
  export * from './Extensions';
3
- export * from './Functions';
4
3
  export * from './Interfaces';
5
4
  export * from './Types';
@@ -1,52 +0,0 @@
1
- export type UpdateFinder<T> = (item: T, index: number, self: Array<T>) => boolean;
2
- declare global {
3
- interface Array<T> {
4
- /**
5
- * Pushes items to array and returns self with new items
6
- * @param items Items to add to array
7
- */
8
- add(...items: Array<T>): this;
9
- /**
10
- * Update an item in array
11
- * @param old The old value or index to update
12
- * @param updated Updated value
13
- */
14
- update(old: T | number | UpdateFinder<T>, updated: T): T;
15
- /**
16
- * Removes item from array and returns self without item
17
- * @param item Item or index to remove
18
- */
19
- remove(item: T | number): this;
20
- /**
21
- * Returns a random element from array
22
- */
23
- random(): T;
24
- /**
25
- * Returns item matching index. If negative number, subtracts number from length
26
- * @param i Index of item
27
- */
28
- index(i: number): T;
29
- /**
30
- * For every x in array, execute callback
31
- * @param every i.e every 2nd item in array
32
- * @param callback Function to execute
33
- * @returns Array of results
34
- */
35
- nth<U>(every: number, callback: (item: T, index: number, collection: Array<T>, self: this) => U): Array<U>;
36
- }
37
- }
38
- declare function add<T>(this: Array<T>, ...items: Array<T>): T[];
39
- declare function update<T>(this: Array<T>, old: T | number | UpdateFinder<T>, updated: T): T;
40
- declare function remove<T>(this: Array<T>, value: T | number): Array<T>;
41
- declare function random<T>(this: Array<T>): T;
42
- declare function index<T>(this: Array<T>, i: number): T;
43
- declare function nth<T, U>(this: Array<T>, every: number, callback: (item: T, index: number, collection: Array<T>, self: Array<T>) => U): Array<U>;
44
- export declare const ArrayExtensions: {
45
- add: typeof add;
46
- update: typeof update;
47
- remove: typeof remove;
48
- random: typeof random;
49
- index: typeof index;
50
- nth: typeof nth;
51
- };
52
- export {};
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArrayExtensions = void 0;
4
- function add(...items) {
5
- this.push(...items);
6
- return this;
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;
13
- if (!item)
14
- throw new Error('Old was not found in array!');
15
- const index = this.indexOf(item);
16
- return this[index] = updated;
17
- }
18
- Array.prototype.update = update;
19
- function remove(value) {
20
- const index = typeof value === 'number' ? value : this.indexOf(value);
21
- if (index > -1)
22
- this.splice(index, 1);
23
- return this;
24
- }
25
- Array.prototype.remove = remove;
26
- function random() {
27
- const randomIndex = Math.round(Math.random() * this.length);
28
- return this[randomIndex];
29
- }
30
- Array.prototype.random = random;
31
- function index(i) {
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
- };
@@ -1,27 +0,0 @@
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 {};
@@ -1,32 +0,0 @@
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
- };