danholibraryjs 1.11.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 (210) 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/Classes/DanhoLogger.d.ts +23 -0
  5. package/dist/Classes/DanhoLogger.js +65 -0
  6. package/dist/Classes/Events/Event.d.ts +66 -66
  7. package/dist/Classes/Events/Event.js +114 -114
  8. package/dist/Classes/Events/EventCollection.d.ts +57 -57
  9. package/dist/Classes/Events/EventCollection.js +109 -109
  10. package/dist/Classes/Events/EventEmitter.d.ts +74 -74
  11. package/dist/Classes/Events/EventEmitter.js +97 -97
  12. package/dist/Classes/Events/index.d.ts +3 -3
  13. package/dist/Classes/Events/index.js +19 -19
  14. package/dist/Classes/Time/Date.d.ts +148 -147
  15. package/dist/Classes/Time/Date.js +241 -238
  16. package/dist/Classes/Time/Time.d.ts +66 -65
  17. package/dist/Classes/Time/Time.js +120 -117
  18. package/dist/Classes/Time/TimeProperties.d.ts +3 -3
  19. package/dist/Classes/Time/TimeProperties.js +2 -2
  20. package/dist/Classes/Time/TimeSpan.d.ts +123 -123
  21. package/dist/Classes/Time/TimeSpan.js +179 -179
  22. package/dist/Classes/Time/index.d.ts +4 -4
  23. package/dist/Classes/Time/index.js +20 -20
  24. package/dist/Classes/index.d.ts +4 -3
  25. package/dist/Classes/index.js +20 -19
  26. package/dist/Classes/store.d.ts +75 -75
  27. package/dist/Classes/store.js +84 -84
  28. package/dist/Extensions/Array/array.extension.d.ts +42 -0
  29. package/dist/Extensions/Array/array.extension.js +57 -0
  30. package/dist/Extensions/Array/crud.extension.d.ts +24 -0
  31. package/dist/Extensions/Array/crud.extension.js +28 -0
  32. package/dist/Extensions/Array/index.d.ts +6 -0
  33. package/dist/Extensions/Array/index.js +22 -0
  34. package/dist/Extensions/Array/loop.extension.d.ts +18 -0
  35. package/dist/Extensions/Array/loop.extension.js +23 -0
  36. package/dist/Extensions/Array/random.extension.d.ts +23 -0
  37. package/dist/Extensions/Array/random.extension.js +35 -0
  38. package/dist/Extensions/Array/sort.extension.d.ts +27 -0
  39. package/dist/Extensions/Array/sort.extension.js +31 -0
  40. package/dist/Extensions/Array/string.extension.d.ts +13 -0
  41. package/dist/Extensions/Array/string.extension.js +14 -0
  42. package/dist/Extensions/Function.d.ts +29 -14
  43. package/dist/Extensions/Function.js +23 -10
  44. package/dist/Extensions/Map.d.ts +54 -54
  45. package/dist/Extensions/Map.js +42 -42
  46. package/dist/Extensions/Number.d.ts +13 -0
  47. package/dist/Extensions/Number.js +40 -0
  48. package/dist/Extensions/Object/arrays.extension.d.ts +17 -0
  49. package/dist/Extensions/Object/arrays.extension.js +13 -0
  50. package/dist/Extensions/Object/booleans.extension.d.ts +18 -0
  51. package/dist/Extensions/Object/booleans.extension.js +37 -0
  52. package/dist/Extensions/Object/extracts.extension.d.ts +38 -0
  53. package/dist/Extensions/Object/extracts.extension.js +72 -0
  54. package/dist/Extensions/Object/index.d.ts +4 -49
  55. package/dist/Extensions/Object/index.js +20 -38
  56. package/dist/Extensions/Object/properties.d.ts +28 -28
  57. package/dist/Extensions/Object/properties.extension.d.ts +33 -0
  58. package/dist/Extensions/Object/properties.extension.js +21 -0
  59. package/dist/Extensions/Object/properties.js +20 -20
  60. package/dist/Extensions/String/case.extension.d.ts +12 -0
  61. package/dist/Extensions/String/case.extension.js +55 -0
  62. package/dist/Extensions/String/index.d.ts +1 -0
  63. package/dist/{Functions → Extensions/String}/index.js +17 -21
  64. package/dist/Extensions/index.d.ts +6 -17
  65. package/dist/Extensions/index.js +22 -30
  66. package/dist/Interfaces/ElementOptions.d.ts +15 -15
  67. package/dist/Interfaces/ElementOptions.js +2 -2
  68. package/dist/Interfaces/IReplacement.d.ts +12 -12
  69. package/dist/Interfaces/IReplacement.js +2 -2
  70. package/dist/Interfaces/index.d.ts +2 -2
  71. package/dist/Interfaces/index.js +18 -18
  72. package/dist/Types/Able.d.ts +16 -0
  73. package/dist/Types/Able.js +2 -0
  74. package/dist/Types/Array.d.ts +6 -0
  75. package/dist/Types/Array.js +2 -0
  76. package/dist/Types/BetterTypes.d.ts +9 -9
  77. package/dist/Types/BetterTypes.js +2 -2
  78. package/dist/Types/C#.d.ts +8 -0
  79. package/dist/Types/C#.js +2 -0
  80. package/dist/Types/Date.d.ts +6 -6
  81. package/dist/Types/Date.js +2 -2
  82. package/dist/Types/Events.d.ts +10 -10
  83. package/dist/Types/Events.js +2 -2
  84. package/dist/Types/Function.d.ts +5 -0
  85. package/dist/Types/Function.js +2 -0
  86. package/dist/Types/Object.d.ts +4 -0
  87. package/dist/Types/Object.js +2 -0
  88. package/dist/Types/PropertiesWith.d.ts +34 -13
  89. package/dist/Types/PropertiesWith.js +2 -2
  90. package/dist/Types/String.d.ts +1 -0
  91. package/dist/Types/String.js +2 -0
  92. package/dist/Types/TransformTypes.d.ts +22 -16
  93. package/dist/Types/TransformTypes.js +2 -2
  94. package/dist/Types/index.d.ts +24 -65
  95. package/dist/Types/index.js +27 -21
  96. package/dist/Utils/{ApiUtil → ApiUtils}/ApiTypes.d.ts +15 -15
  97. package/dist/Utils/{ApiUtil → ApiUtils}/ApiTypes.js +15 -15
  98. package/dist/Utils/{ApiUtil → ApiUtils}/RequestUtil.d.ts +19 -19
  99. package/dist/Utils/{ApiUtil → ApiUtils}/RequestUtil.js +73 -73
  100. package/dist/Utils/{ApiUtil → ApiUtils}/index.d.ts +20 -20
  101. package/dist/Utils/{ApiUtil → ApiUtils}/index.js +33 -33
  102. package/dist/Utils/ColorUtils.d.ts +11 -0
  103. package/dist/Utils/ColorUtils.js +93 -0
  104. package/dist/Utils/{FormUtil.d.ts → FormUtils.d.ts} +6 -6
  105. package/dist/Utils/{FormUtil.js → FormUtils.js} +35 -35
  106. package/dist/Utils/NumberUtils.d.ts +1 -0
  107. package/dist/Utils/NumberUtils.js +7 -0
  108. package/dist/Utils/PatcherUtils.d.ts +6 -0
  109. package/dist/Utils/PatcherUtils.js +80 -0
  110. package/dist/Utils/StringUtils.d.ts +3 -0
  111. package/dist/Utils/StringUtils.js +47 -0
  112. package/dist/Utils/TimeUtils/debounce.util.d.ts +22 -0
  113. package/dist/Utils/TimeUtils/debounce.util.js +78 -0
  114. package/dist/Utils/TimeUtils/functions.util.d.ts +4 -0
  115. package/dist/Utils/TimeUtils/functions.util.js +21 -0
  116. package/dist/Utils/TimeUtils/index.d.ts +15 -0
  117. package/dist/Utils/TimeUtils/index.js +34 -0
  118. package/dist/Utils/TimeUtils/throttle.util.d.ts +15 -0
  119. package/dist/Utils/TimeUtils/throttle.util.js +43 -0
  120. package/dist/Utils/index.d.ts +7 -2
  121. package/dist/Utils/index.js +23 -18
  122. package/dist/index.d.ts +4 -5
  123. package/dist/index.js +20 -21
  124. package/docs/Classes.md +78 -3
  125. package/docs/Extensions.md +219 -78
  126. package/docs/Types.md +202 -58
  127. package/docs/index.md +0 -1
  128. package/package.json +4 -2
  129. package/src/Classes/DanhoLogger.ts +78 -0
  130. package/src/Classes/Events/Event.ts +96 -96
  131. package/src/Classes/Events/EventCollection.ts +90 -90
  132. package/src/Classes/Events/EventEmitter.ts +68 -68
  133. package/src/Classes/Time/Date.ts +219 -216
  134. package/src/Classes/Time/Time.ts +109 -104
  135. package/src/Classes/Time/TimeSpan.ts +171 -171
  136. package/src/Classes/index.ts +1 -0
  137. package/src/Classes/store.ts +22 -22
  138. package/src/Extensions/Array/array.extension.ts +103 -0
  139. package/src/Extensions/Array/crud.extension.ts +46 -0
  140. package/src/Extensions/Array/index.ts +6 -0
  141. package/src/Extensions/Array/loop.extension.ts +38 -0
  142. package/src/Extensions/Array/random.extension.ts +56 -0
  143. package/src/Extensions/Array/sort.extension.ts +52 -0
  144. package/src/Extensions/Array/string.extension.ts +22 -0
  145. package/src/Extensions/Function.ts +37 -10
  146. package/src/Extensions/Map.ts +56 -56
  147. package/src/Extensions/Number.ts +50 -0
  148. package/src/Extensions/Object/arrays.extension.ts +27 -0
  149. package/src/Extensions/Object/booleans.extension.ts +46 -0
  150. package/src/Extensions/Object/extracts.extension.ts +102 -0
  151. package/src/Extensions/Object/index.ts +4 -82
  152. package/src/Extensions/Object/properties.extension.ts +59 -0
  153. package/src/Extensions/Object/properties.ts +36 -36
  154. package/src/Extensions/String/case.extension.ts +95 -0
  155. package/src/Extensions/String/index.ts +1 -0
  156. package/src/Extensions/index.ts +3 -21
  157. package/src/Interfaces/ElementOptions.ts +7 -7
  158. package/src/Interfaces/IReplacement.ts +2 -2
  159. package/src/Types/Able.ts +22 -0
  160. package/src/Types/Array.ts +7 -0
  161. package/src/Types/C#.ts +9 -0
  162. package/src/Types/Date.ts +1 -1
  163. package/src/Types/Events.ts +12 -12
  164. package/src/Types/Function.ts +10 -0
  165. package/src/Types/Object.ts +4 -0
  166. package/src/Types/PropertiesWith.ts +35 -4
  167. package/src/Types/String.ts +1 -0
  168. package/src/Types/TransformTypes.ts +21 -13
  169. package/src/Types/index.ts +7 -69
  170. package/src/Utils/{ApiUtil → ApiUtils}/ApiTypes.ts +2 -1
  171. package/src/Utils/{ApiUtil → ApiUtils}/index.ts +1 -1
  172. package/src/Utils/ColorUtils.ts +102 -0
  173. package/src/Utils/{FormUtil.ts → FormUtils.ts} +2 -2
  174. package/src/Utils/NumberUtils.ts +3 -0
  175. package/src/Utils/PatcherUtils.ts +111 -0
  176. package/src/Utils/StringUtils.ts +44 -0
  177. package/src/Utils/TimeUtils/debounce.util.ts +85 -0
  178. package/src/Utils/TimeUtils/functions.util.ts +18 -0
  179. package/src/Utils/TimeUtils/index.ts +9 -0
  180. package/src/Utils/TimeUtils/throttle.util.ts +44 -0
  181. package/src/Utils/index.ts +8 -2
  182. package/src/index.ts +0 -1
  183. package/dist/Extensions/Array.d.ts +0 -52
  184. package/dist/Extensions/Array.js +0 -51
  185. package/dist/Extensions/Document.d.ts +0 -27
  186. package/dist/Extensions/Document.js +0 -32
  187. package/dist/Extensions/String.d.ts +0 -36
  188. package/dist/Extensions/String.js +0 -25
  189. package/dist/Functions/CopyToClipboard.d.ts +0 -7
  190. package/dist/Functions/CopyToClipboard.js +0 -15
  191. package/dist/Functions/GetCSSProperty.d.ts +0 -15
  192. package/dist/Functions/GetCSSProperty.js +0 -26
  193. package/dist/Functions/GetNestedProperty.d.ts +0 -9
  194. package/dist/Functions/GetNestedProperty.js +0 -23
  195. package/dist/Functions/HTMLEvent.d.ts +0 -11
  196. package/dist/Functions/HTMLEvent.js +0 -14
  197. package/dist/Functions/SetNavigationSelected.d.ts +0 -9
  198. package/dist/Functions/SetNavigationSelected.js +0 -25
  199. package/dist/Functions/index.d.ts +0 -5
  200. package/docs/Functions.md +0 -61
  201. package/src/Extensions/Array.ts +0 -95
  202. package/src/Extensions/Document.ts +0 -58
  203. package/src/Extensions/String.ts +0 -54
  204. package/src/Functions/CopyToClipboard.ts +0 -10
  205. package/src/Functions/GetCSSProperty.ts +0 -27
  206. package/src/Functions/GetNestedProperty.ts +0 -29
  207. package/src/Functions/HTMLEvent.ts +0 -13
  208. package/src/Functions/SetNavigationSelected.ts +0 -19
  209. package/src/Functions/index.ts +0 -5
  210. /package/src/Utils/{ApiUtil → ApiUtils}/RequestUtil.ts +0 -0
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
@@ -1,7 +1,9 @@
1
1
  {
2
-
3
2
  "name": "danholibraryjs",
4
- "version": "1.11.0",
3
+ "publishConfig": {
4
+ "registry": "https://registry.npmjs.org/"
5
+ },
6
+ "version": "2.0.1",
5
7
  "description": "Library for Javascript.",
6
8
  "main": "dist/index.js",
7
9
  "types": "dist/index.d.ts",
@@ -0,0 +1,78 @@
1
+ type LogOptions = {
2
+ name?: string;
3
+ color?: string;
4
+ preferCollapsed?: boolean;
5
+ }
6
+
7
+ export default class DanhoLogger {
8
+ public name: string;
9
+ public color?: string
10
+ private _preferCollapsed: boolean;
11
+
12
+ constructor(options: LogOptions = {}) {
13
+ this.name = options.name || 'DanhoLogger';
14
+ this.color = options.color;
15
+ this._preferCollapsed = options.preferCollapsed ?? false;
16
+ }
17
+
18
+ private _getPrefix() {
19
+ return {
20
+ prefix: `%c[${this.name}]`,
21
+ style: `color: ${this.color || 'inherit'}; font-weight: bold;`
22
+ };
23
+ }
24
+ private _log(
25
+ type: 'log' | 'warn' | 'error',
26
+ ...args: any[]
27
+ ) {
28
+ const { prefix, style } = this._getPrefix();
29
+ console[type](prefix, style, ...args);
30
+ return this;
31
+ }
32
+
33
+ public log(...args: any[]) {
34
+ return this._log('log', ...args);
35
+ }
36
+ public warn(...args: any[]) {
37
+ return this._log('warn', ...args);
38
+ }
39
+ public error(...args: any[]) {
40
+ return this._log('error', ...args);
41
+ }
42
+
43
+ public group(...args: any[]) {
44
+ const { prefix, style } = this._getPrefix();
45
+
46
+ const groupMethod = this._preferCollapsed ? console.groupCollapsed : console.group;
47
+ groupMethod(prefix, style, ...args);
48
+
49
+ return this;
50
+ }
51
+ public groupCollapsed(...args: any[]) {
52
+ const { prefix, style } = this._getPrefix();
53
+ console.groupCollapsed(prefix, style, ...args);
54
+ return this;
55
+ }
56
+ public groupEnd(...args: any[]) {
57
+ if (args.length) console.log(...args);
58
+ console.groupEnd();
59
+ return this;
60
+ }
61
+
62
+ time(label: string) {
63
+ const { prefix } = this._getPrefix();
64
+ console.time(`${prefix} ${label}`);
65
+ return this;
66
+ }
67
+ timeEnd(label: string) {
68
+ const { prefix } = this._getPrefix();
69
+ console.timeEnd(`${prefix} ${label}`);
70
+ return this;
71
+ }
72
+
73
+ table(data: any, columns?: string[]) {
74
+ this.log();
75
+ console.table(data, columns);
76
+ return this;
77
+ }
78
+ }
@@ -4,111 +4,111 @@ import { BaseEvent, EventHandler } from "../../Types/Events";
4
4
  * Base event for @see EventEmitter, @borrows EventHandler @borrows BaseEvent
5
5
  */
6
6
  export class Event<
7
- Events extends BaseEvent<string, Array<any>>,
8
- Name extends keyof Events = keyof Events,
7
+ Events extends BaseEvent<string, Array<any>>,
8
+ Name extends keyof Events = keyof Events,
9
9
  > {
10
- /**
11
- * Base event for @see EventEmitter, @borrows EventHandler
12
- * @param name Name of event
13
- * @param listeners Listeners/Handlers to execute when emitted
14
- */
15
- constructor(name: Name, ...listeners: Array<EventHandler<Events, Name>>) {
16
- this.name = name;
17
- this._listeners = listeners;
18
- this._lastEmitted = new Date();
19
- }
10
+ /**
11
+ * Base event for @see EventEmitter, @borrows EventHandler
12
+ * @param name Name of event
13
+ * @param listeners Listeners/Handlers to execute when emitted
14
+ */
15
+ constructor(name: Name, ...listeners: Array<EventHandler<Events, Name>>) {
16
+ this.name = name;
17
+ this._listeners = listeners;
18
+ this._lastEmitted = new Date();
19
+ }
20
20
 
21
- /**Name of event*/
22
- public name: Name;
23
- /**Listener limit - default: 0 */
24
- public limit = 0;
25
- /**Number of times event was emitted - default: 0*/
26
- public get runs() {
27
- return this._runs
28
- }
29
- /**Timestamp of last emit - default: null*/
30
- public get lastEmitted() {
31
- return this._lastEmitted;
32
- }
21
+ /**Name of event*/
22
+ public name: Name;
23
+ /**Listener limit - default: 0 */
24
+ public limit = 0;
25
+ /**Number of times event was emitted - default: 0*/
26
+ public get runs() {
27
+ return this._runs;
28
+ }
29
+ /**Timestamp of last emit - default: null*/
30
+ public get lastEmitted() {
31
+ return this._lastEmitted;
32
+ }
33
33
 
34
- /**@private Internal listeners array*/
35
- private _listeners: Array<EventHandler<Events, Name>>;
36
- /**@private Internal runs*/
37
- private _runs = 0;
38
- /**@private Internal lastEmitted*/
39
- private _lastEmitted: Date;
34
+ /**@private Internal listeners array*/
35
+ private _listeners: Array<EventHandler<Events, Name>>;
36
+ /**@private Internal runs*/
37
+ private _runs = 0;
38
+ /**@private Internal lastEmitted*/
39
+ private _lastEmitted: Date;
40
40
 
41
- /**
42
- * Emits event and returns array of responses
43
- * @param args Arguments required for event listeners
44
- * @returns Return values of listeners' returns
45
- */
46
- public emit(...args: Events[Name]) {
47
- this._runs++;
48
- this._lastEmitted = new Date();
49
- return this._listeners.map(listener => (listener as any)(...args))
41
+ /**
42
+ * Emits event and returns array of responses
43
+ * @param args Arguments required for event listeners
44
+ * @returns Return values of listeners' returns
45
+ */
46
+ public emit(...args: Events[Name]) {
47
+ this._runs++;
48
+ this._lastEmitted = new Date();
49
+ return this._listeners.map(listener => (listener as any)(...args));
50
+ }
51
+ /**
52
+ * Adds listener to listeners array and returns self with new listener added
53
+ * @param listener Listener to add
54
+ * @param prepend Add first (true) or last (false) - default: false
55
+ * @returns this with listener added
56
+ *
57
+ * @throws Limit error, if limit was reached
58
+ */
59
+ public on(listener: EventHandler<Events, Name>, prepend = false) {
60
+ if (this.limit > 0 && this._listeners.length + 1 > this.limit) {
61
+ throw new Error(`Event limit, ${this.limit}, reached for event ${this.name.toString()}!`);
50
62
  }
51
- /**
52
- * Adds listener to listeners array and returns self with new listener added
53
- * @param listener Listener to add
54
- * @param prepend Add first (true) or last (false) - default: false
55
- * @returns this with listener added
56
- *
57
- * @throws Limit error, if limit was reached
58
- */
59
- public on(listener: EventHandler<Events, Name>, prepend = false) {
60
- if (this.limit > 0 && this._listeners.length + 1 > this.limit) {
61
- throw new Error(`Event limit, ${this.limit}, reached for event ${this.name.toString()}!`);
62
- }
63
63
 
64
- if (prepend) this._listeners = [listener, ...this._listeners]
65
- else this._listeners.add(listener);
64
+ if (prepend) this._listeners = [listener, ...this._listeners];
65
+ else this._listeners.add(listener);
66
66
 
67
- return this;
68
- }
69
- /**
70
- * Like Event#on, adds listener to listeners array and returns self with new listener added, however removes listener once emitted
71
- * @param listener Listener to add
72
- * @param prepend Add first (true) or last (false) - default: false
73
- * @returns this with listener added
74
- *
75
- * @throws Limit error, if limit was reached
76
- */
77
- public once(listener: EventHandler<Events, Name>, prepend = false) {
78
- const handler = (...params: Events[Name]) => {
79
- const result = (listener as any)(...params);
80
- this.off(handler as any);
81
- return result;
82
- }
83
- this.on(handler as any, prepend);
84
- return this;
85
- }
67
+ return this;
68
+ }
69
+ /**
70
+ * Like Event#on, adds listener to listeners array and returns self with new listener added, however removes listener once emitted
71
+ * @param listener Listener to add
72
+ * @param prepend Add first (true) or last (false) - default: false
73
+ * @returns this with listener added
74
+ *
75
+ * @throws Limit error, if limit was reached
76
+ */
77
+ public once(listener: EventHandler<Events, Name>, prepend = false) {
78
+ const handler = (...params: Events[Name]) => {
79
+ const result = (listener as any)(...params);
80
+ this.off(handler as any);
81
+ return result;
82
+ };
83
+ this.on(handler as any, prepend);
84
+ return this;
85
+ }
86
86
 
87
- /**
88
- * Returns true or false, depending if event includes listener
89
- * @param listener Listener to test
90
- * @returns True of false, depending if event includes listener
91
- */
92
- public includes(listener: EventHandler<Events, Name>) {
93
- return this._listeners.includes(listener);
94
- }
87
+ /**
88
+ * Returns true or false, depending if event includes listener
89
+ * @param listener Listener to test
90
+ * @returns True of false, depending if event includes listener
91
+ */
92
+ public includes(listener: EventHandler<Events, Name>) {
93
+ return this._listeners.includes(listener);
94
+ }
95
95
 
96
- /**
97
- * Removes listener from internal listeners array
98
- * @param listener Listener to remove. If none specified, all will be removed
99
- * @param throwNotFoundError Throw error if listener isn't in listeners array - default: false
100
- * @returns this, without listener
101
- *
102
- * @throws NotFound, if throwNotFoundError is true, and internal listeners array doesn't include listener provided
103
- */
104
- public off(listener?: EventHandler<Events, Name>, throwNotFoundError = false) {
105
- try {
106
- if (!listener) this._listeners = new Array<EventHandler<Events, Name>>()
107
- else if (!this._listeners.includes(listener)) return this;
108
- else this._listeners.remove(listener);
109
- }
110
- catch (err) { if (throwNotFoundError) throw err; }
111
- return this;
96
+ /**
97
+ * Removes listener from internal listeners array
98
+ * @param listener Listener to remove. If none specified, all will be removed
99
+ * @param throwNotFoundError Throw error if listener isn't in listeners array - default: false
100
+ * @returns this, without listener
101
+ *
102
+ * @throws NotFound, if throwNotFoundError is true, and internal listeners array doesn't include listener provided
103
+ */
104
+ public off(listener?: EventHandler<Events, Name>, throwNotFoundError = false) {
105
+ try {
106
+ if (!listener) this._listeners = new Array<EventHandler<Events, Name>>();
107
+ else if (!this._listeners.includes(listener)) return this;
108
+ else this._listeners.remove(listener);
112
109
  }
110
+ catch (err) { if (throwNotFoundError) throw err; }
111
+ return this;
112
+ }
113
113
  }
114
114
  export default Event;