x4js 1.4.2

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 (176) hide show
  1. package/lib/application.d.ts +95 -0
  2. package/lib/application.js +137 -0
  3. package/lib/base64.d.ts +31 -0
  4. package/lib/base64.js +135 -0
  5. package/lib/base_component.d.ts +64 -0
  6. package/lib/base_component.js +77 -0
  7. package/lib/button.d.ts +145 -0
  8. package/lib/button.js +235 -0
  9. package/lib/calendar.d.ts +77 -0
  10. package/lib/calendar.js +236 -0
  11. package/lib/canvas.d.ts +88 -0
  12. package/lib/canvas.js +354 -0
  13. package/lib/cardview.d.ts +83 -0
  14. package/lib/cardview.js +152 -0
  15. package/lib/checkbox.d.ts +72 -0
  16. package/lib/checkbox.js +126 -0
  17. package/lib/color.d.ts +144 -0
  18. package/lib/color.js +584 -0
  19. package/lib/colorpicker.d.ts +98 -0
  20. package/lib/colorpicker.js +1457 -0
  21. package/lib/combobox.d.ts +97 -0
  22. package/lib/combobox.js +246 -0
  23. package/lib/component.d.ts +572 -0
  24. package/lib/component.js +1712 -0
  25. package/lib/datastore.d.ts +392 -0
  26. package/lib/datastore.js +986 -0
  27. package/lib/dialog.d.ts +171 -0
  28. package/lib/dialog.js +468 -0
  29. package/lib/dom_events.d.ts +284 -0
  30. package/lib/dom_events.js +13 -0
  31. package/lib/drag_manager.d.ts +26 -0
  32. package/lib/drag_manager.js +118 -0
  33. package/lib/drawtext.d.ts +43 -0
  34. package/lib/drawtext.js +261 -0
  35. package/lib/fileupload.d.ts +60 -0
  36. package/lib/fileupload.js +158 -0
  37. package/lib/form.d.ts +122 -0
  38. package/lib/form.js +293 -0
  39. package/lib/formatters.d.ts +31 -0
  40. package/lib/formatters.js +75 -0
  41. package/lib/gridview.d.ts +171 -0
  42. package/lib/gridview.js +786 -0
  43. package/lib/hosts/host.d.ts +44 -0
  44. package/lib/hosts/host.js +69 -0
  45. package/lib/i18n.d.ts +67 -0
  46. package/lib/i18n.js +169 -0
  47. package/lib/icon.d.ts +56 -0
  48. package/lib/icon.js +173 -0
  49. package/lib/image.d.ts +51 -0
  50. package/lib/image.js +149 -0
  51. package/lib/index.js +1 -0
  52. package/lib/input.d.ts +86 -0
  53. package/lib/input.js +172 -0
  54. package/lib/label.d.ts +54 -0
  55. package/lib/label.js +86 -0
  56. package/lib/layout.d.ts +77 -0
  57. package/lib/layout.js +261 -0
  58. package/lib/link.d.ts +46 -0
  59. package/lib/link.js +55 -0
  60. package/lib/listview.d.ts +173 -0
  61. package/lib/listview.js +532 -0
  62. package/lib/md5.d.ts +56 -0
  63. package/lib/md5.js +397 -0
  64. package/lib/menu.d.ts +122 -0
  65. package/lib/menu.js +276 -0
  66. package/lib/messagebox.d.ts +64 -0
  67. package/lib/messagebox.js +141 -0
  68. package/lib/panel.d.ts +42 -0
  69. package/lib/panel.js +61 -0
  70. package/lib/popup.d.ts +71 -0
  71. package/lib/popup.js +373 -0
  72. package/lib/property_editor.d.ts +67 -0
  73. package/lib/property_editor.js +247 -0
  74. package/lib/radiobtn.d.ts +68 -0
  75. package/lib/radiobtn.js +131 -0
  76. package/lib/rating.d.ts +49 -0
  77. package/lib/rating.js +93 -0
  78. package/lib/request.d.ts +48 -0
  79. package/lib/request.js +220 -0
  80. package/lib/router.d.ts +13 -0
  81. package/lib/router.js +27 -0
  82. package/lib/settings.d.ts +33 -0
  83. package/lib/settings.js +63 -0
  84. package/lib/sidebarview.d.ts +44 -0
  85. package/lib/sidebarview.js +73 -0
  86. package/lib/smartedit.d.ts +103 -0
  87. package/lib/smartedit.js +381 -0
  88. package/lib/spreadsheet.d.ts +214 -0
  89. package/lib/spreadsheet.js +1073 -0
  90. package/lib/styles.d.ts +81 -0
  91. package/lib/styles.js +262 -0
  92. package/lib/svgcomponent.d.ts +165 -0
  93. package/lib/svgcomponent.js +350 -0
  94. package/lib/tabbar.d.ts +41 -0
  95. package/lib/tabbar.js +66 -0
  96. package/lib/tabview.d.ts +45 -0
  97. package/lib/tabview.js +79 -0
  98. package/lib/textarea.d.ts +59 -0
  99. package/lib/textarea.js +119 -0
  100. package/lib/textedit.d.ts +118 -0
  101. package/lib/textedit.js +406 -0
  102. package/lib/texthiliter.d.ts +56 -0
  103. package/lib/texthiliter.js +219 -0
  104. package/lib/toaster.d.ts +38 -0
  105. package/lib/toaster.js +58 -0
  106. package/lib/tools.d.ts +382 -0
  107. package/lib/tools.js +1096 -0
  108. package/lib/tooltips.d.ts +42 -0
  109. package/lib/tooltips.js +148 -0
  110. package/lib/treeview.d.ts +128 -0
  111. package/lib/treeview.js +490 -0
  112. package/lib/x4_events.d.ts +253 -0
  113. package/lib/x4_events.js +363 -0
  114. package/package.json +21 -0
  115. package/src/README.md +2 -0
  116. package/src/application.ts +191 -0
  117. package/src/base64.ts +162 -0
  118. package/src/base_component.ts +118 -0
  119. package/src/button.ts +327 -0
  120. package/src/calendar.ts +312 -0
  121. package/src/canvas.ts +501 -0
  122. package/src/cardview.ts +220 -0
  123. package/src/checkbox.ts +178 -0
  124. package/src/color.ts +748 -0
  125. package/src/colorpicker.ts +1618 -0
  126. package/src/combobox.ts +348 -0
  127. package/src/component.ts +2330 -0
  128. package/src/datastore.ts +1318 -0
  129. package/src/dialog.ts +631 -0
  130. package/src/dom_events.ts +297 -0
  131. package/src/drag_manager.ts +168 -0
  132. package/src/drawtext.ts +342 -0
  133. package/src/fileupload.ts +208 -0
  134. package/src/form.ts +362 -0
  135. package/src/formatters.ts +96 -0
  136. package/src/gridview.ts +1051 -0
  137. package/src/hosts/electron.ts +161 -0
  138. package/src/hosts/host.ts +100 -0
  139. package/src/hosts/nwjs.ts +141 -0
  140. package/src/hosts/nwjs_types.ts +339 -0
  141. package/src/i18n.ts +205 -0
  142. package/src/icon.ts +237 -0
  143. package/src/image.ts +198 -0
  144. package/src/input.ts +236 -0
  145. package/src/label.ts +124 -0
  146. package/src/layout.ts +366 -0
  147. package/src/link.ts +82 -0
  148. package/src/listview.ts +749 -0
  149. package/src/md5.ts +432 -0
  150. package/src/menu.ts +394 -0
  151. package/src/messagebox.ts +199 -0
  152. package/src/panel.ts +81 -0
  153. package/src/popup.ts +488 -0
  154. package/src/property_editor.ts +333 -0
  155. package/src/radiobtn.ts +190 -0
  156. package/src/rating.ts +131 -0
  157. package/src/request.ts +296 -0
  158. package/src/router.ts +43 -0
  159. package/src/settings.ts +75 -0
  160. package/src/sidebarview.ts +97 -0
  161. package/src/smartedit.ts +532 -0
  162. package/src/spreadsheet.ts +1423 -0
  163. package/src/styles.ts +332 -0
  164. package/src/svgcomponent.ts +440 -0
  165. package/src/tabbar.ts +105 -0
  166. package/src/tabview.ts +106 -0
  167. package/src/textarea.ts +183 -0
  168. package/src/textedit.ts +535 -0
  169. package/src/texthiliter.ts +284 -0
  170. package/src/toaster.ts +76 -0
  171. package/src/tools.ts +1391 -0
  172. package/src/tooltips.ts +185 -0
  173. package/src/treeview.ts +670 -0
  174. package/src/x4.less +1940 -0
  175. package/src/x4_events.ts +558 -0
  176. package/tsconfig.json +14 -0
package/src/tools.ts ADDED
@@ -0,0 +1,1391 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file tools.ts
9
+ * @author Etienne Cochard
10
+ * @license
11
+ * Copyright (c) 2019-2021 R-libre ingenierie
12
+ *
13
+ * This program is free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
24
+ **/
25
+
26
+ import { _tr } from './i18n'; // you MUST create a file named translation.js
27
+
28
+ // :: ENVIRONMENT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
29
+
30
+ /**
31
+ * @return true is the device has touch
32
+ */
33
+
34
+ export function isTouchDevice() {
35
+ return 'ontouchstart' in window;
36
+ }
37
+
38
+ // :: NUMBERS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39
+
40
+
41
+ /**
42
+ * round to a given number of decimals
43
+ * @param num numbre to round
44
+ * @param ndec number of decimals
45
+ * @returns number rounded
46
+ */
47
+
48
+ export function roundTo(num: number, ndec: number) {
49
+ let mul = Math.pow(10, ndec);
50
+ let res = Math.round(num * mul) / mul;
51
+ if (Object.is(res, -0)) { // res===-0.0
52
+ res = 0;
53
+ }
54
+ return res;
55
+ }
56
+
57
+ /**
58
+ * parse an intl formatted number
59
+ * understand grouping and ',' separator
60
+ * @review us format: grouping = ','
61
+ * @param num
62
+ */
63
+
64
+ export function parseIntlFloat(num: string) {
65
+ num = num.replace(/\s*/g, ''); // group separator
66
+ num = num.replace(',', '.'); // decimal separator
67
+
68
+ // accept empty & return 0.0
69
+ if (num.length == 0) {
70
+ return 0.0;
71
+ }
72
+
73
+ return parseFloat(num);
74
+ }
75
+
76
+ // :: STRING MANIP ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
77
+
78
+ const RE_PCASE = /([a-z][A-Z])/g;
79
+ const RE_PCASE2 = /[^-a-z0-9]+/g;
80
+
81
+ /**
82
+ * inverse of camel case
83
+ * theThingToCase -> the-thing-to-case
84
+ * @param {String} str
85
+ */
86
+
87
+ export function pascalCase(string: string): string {
88
+
89
+ let result = string;
90
+
91
+ result = result.replace(/([a-z])([A-Z])/g, "$1 $2");
92
+ result = result.toLowerCase();
93
+ result = result.replace(/[^- a-z0-9]+/g, ' ');
94
+
95
+ if (result.indexOf(' ') < 0) {
96
+ return result;
97
+ }
98
+
99
+ result = result.trim();
100
+ return result.replace(/ /g, '-');
101
+ }
102
+
103
+ // :: MISC CLASSES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
104
+
105
+ export type Constructor<T> = new (...args: any[]) => T;
106
+
107
+ export class Point {
108
+ x: number;
109
+ y: number;
110
+
111
+ constructor(x = 0, y = 0) {
112
+ this.x = x;
113
+ this.y = y;
114
+ }
115
+ }
116
+
117
+ export class Size {
118
+ width: number;
119
+ height: number;
120
+
121
+ constructor(w = 0, h = 0) {
122
+ this.width = w;
123
+ this.height = h;
124
+ }
125
+ }
126
+
127
+ export class Rect {
128
+
129
+ left: number;
130
+ top: number;
131
+ width: number;
132
+ height: number;
133
+
134
+ constructor();
135
+ constructor(rc: Rect);
136
+ constructor(rc: DOMRect);
137
+ constructor(left: number, top: number, width: number, height: number);
138
+ constructor(left?: number | Rect | DOMRect, top?: number, width?: number, height?: number) {
139
+
140
+ if (left === undefined) {
141
+ this.left = this.top = this.right = this.bottom = 0;
142
+ }
143
+ else if (left instanceof Rect || left instanceof DOMRect) {
144
+ let rc = left;
145
+ this.left = rc.left;
146
+ this.top = rc.top;
147
+ this.width = rc.width;
148
+ this.height = rc.height;
149
+ }
150
+ else {
151
+ this.left = left;
152
+ this.top = top;
153
+ this.width = width;
154
+ this.height = height;
155
+ }
156
+ }
157
+
158
+ set(left: number, top: number, width: number, height: number) {
159
+ this.left = left;
160
+ this.top = top;
161
+ this.width = width;
162
+ this.height = height;
163
+ }
164
+
165
+ get bottom(): number {
166
+ return this.top + this.height;
167
+ }
168
+
169
+ set bottom(bottom: number) {
170
+ this.height = bottom - this.top;
171
+ }
172
+
173
+ get right(): number {
174
+ return this.left + this.width;
175
+ }
176
+
177
+ set right(right: number) {
178
+ this.width = right - this.left;
179
+ }
180
+
181
+ get topLeft(): Point {
182
+ return new Point(this.left, this.top);
183
+ }
184
+
185
+ get bottomRight(): Point {
186
+ return new Point(this.right, this.bottom);
187
+ }
188
+
189
+ get size(): Size {
190
+ return new Size(this.width, this.height);
191
+ }
192
+
193
+ moveTo(left: number, top: number): this {
194
+ this.left = left;
195
+ this.top = top;
196
+ return this;
197
+ }
198
+
199
+ movedTo(left: number, top: number): Rect {
200
+ return new Rect(left, top, this.width, this.height);
201
+ }
202
+
203
+ moveBy(dx: number, dy: number): this {
204
+ this.left += dx;
205
+ this.top += dy;
206
+ return this;
207
+ }
208
+
209
+ movedBy(dx: number, dy: number): Rect {
210
+ return new Rect(this.left + dx, this.top + dy, this.width, this.height);
211
+ }
212
+
213
+ isEmpty() {
214
+ return this.width == 0 && this.height == 0;
215
+ }
216
+
217
+ normalize(): this {
218
+ let w = this.width,
219
+ h = this.height;
220
+
221
+ if (w < 0) {
222
+ this.left += w;
223
+ this.width = -w;
224
+ }
225
+
226
+ if (h < 0) {
227
+ this.top += h;
228
+ this.height = -h;
229
+ }
230
+
231
+ return this;
232
+ }
233
+
234
+ normalized(): Rect {
235
+
236
+ let rc = new Rect(this);
237
+ let w = rc.width,
238
+ h = rc.height;
239
+
240
+
241
+ if (w < 0) {
242
+ rc.left += w;
243
+ rc.width = -w;
244
+ }
245
+
246
+ if (h < 0) {
247
+ rc.top += h;
248
+ rc.height = -h;
249
+ }
250
+
251
+ return rc;
252
+ }
253
+
254
+ /**
255
+ * @deprecated
256
+ */
257
+ containsPt(x: number, y: number): boolean {
258
+ return x >= this.left && x <= this.right && y >= this.top && y <= this.bottom;
259
+ }
260
+
261
+ contains(pt: Point): boolean;
262
+ contains(rc: Rect): boolean;
263
+ contains(arg: any): boolean {
264
+ if (arg instanceof Rect) {
265
+ return arg.left >= this.left && arg.right <= this.right && arg.top >= this.top && arg.bottom <= this.bottom;
266
+ }
267
+ else {
268
+ return arg.x >= this.left && arg.x < this.right && arg.y >= this.top && arg.y < this.bottom;
269
+ }
270
+ }
271
+
272
+ touches(rc: Rect): boolean {
273
+ if (this.left > rc.right || this.right < rc.left || this.top > rc.bottom || this.bottom < rc.top) {
274
+ return false;
275
+ }
276
+
277
+ return true;
278
+ }
279
+
280
+ inflate(dx: number, dy?: number) {
281
+ if (dy === undefined) {
282
+ dy = dx;
283
+ }
284
+
285
+ this.left -= dx;
286
+ this.top -= dy;
287
+ this.width += dx + dx;
288
+ this.height += dy + dy;
289
+ }
290
+
291
+ inflatedBy(dx: number, dy?: number) {
292
+ if (dy === undefined) {
293
+ dy = dx;
294
+ }
295
+ return new Rect(this.left - dx, this.top - dy, this.width + dx + dx, this.height + dy + dy);
296
+ }
297
+
298
+ combine(rc: Rect) {
299
+ let left = Math.min(this.left, rc.left);
300
+ let top = Math.min(this.top, rc.top);
301
+ let right = Math.max(this.right, rc.right);
302
+ let bottom = Math.max(this.bottom, rc.bottom);
303
+
304
+ this.left = left;
305
+ this.top = top;
306
+ this.right = right;
307
+ this.bottom = bottom;
308
+ }
309
+ }
310
+
311
+ /**
312
+ * replace {0..9} by given arguments
313
+ * @param format string
314
+ * @param args
315
+ *
316
+ * @example ```ts
317
+ *
318
+ * console.log( sprintf( 'here is arg 1 {1} and arg 0 {0}', 'argument 0', 'argument 1' ) )
319
+ */
320
+
321
+ export function sprintf(format: string, ...args) {
322
+ return format.replace(/{(\d+)}/g, function (match, index) {
323
+ return typeof args[index] != 'undefined' ? args[index] : match;
324
+ });
325
+ }
326
+
327
+ /**
328
+ * replace special characters for display
329
+ * @param unsafe
330
+ *
331
+ * console.log( escapeHtml('<div style="width:50px; height: 50px; background-color:red"></div>') );
332
+ */
333
+ export function escapeHtml(unsafe: string, nl_br = false): string {
334
+ if (!unsafe || unsafe.length == 0) {
335
+ return unsafe;
336
+ }
337
+
338
+ let result = unsafe.replace(/[<>\&\"\']/g, function (c) {
339
+ return '&#' + c.charCodeAt(0) + ';';
340
+ });
341
+
342
+ if (nl_br) {
343
+ result = result.replace(/(\r\n|\n\r|\r|\n)/g, '<br/>');
344
+ }
345
+
346
+ return result;
347
+ }
348
+
349
+ /**
350
+ * replace special characters for display
351
+ * @author Steven Levithan <http://slevithan.com/>
352
+ * @param unsafe
353
+ *
354
+ * console.log( removeHtmlTags('<h1>sss</h1>') );
355
+ */
356
+
357
+ export function removeHtmlTags(unsafe: string, nl_br = false): string {
358
+ if (!unsafe || unsafe.length == 0) {
359
+ return unsafe;
360
+ }
361
+ debugger;
362
+ let ret_val = '';
363
+ for (let i = 0; i < unsafe.length; i++) {
364
+ const ch = unsafe.codePointAt(i);
365
+ if (ch > 127) {
366
+ ret_val += '&#' + ch + ';';
367
+ }
368
+ else if (ch == 60/*<*/) {
369
+ ret_val += '&lt;'
370
+ }
371
+ else {
372
+ ret_val += unsafe.charAt(i);
373
+ }
374
+ }
375
+ return ret_val;
376
+ }
377
+
378
+
379
+ // :: DATES ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
380
+
381
+ let cur_locale: string = 'fr-FR';
382
+
383
+ /**
384
+ * change the current locale for misc translations (date...)
385
+ * @param locale
386
+ */
387
+
388
+ export function _date_set_locale(locale: string) {
389
+ cur_locale = locale;
390
+ }
391
+
392
+ /**
393
+ *
394
+ * @param date
395
+ * @param options
396
+ * @example
397
+ * let date = new Date( );
398
+ * let options = { day: 'numeric', month: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric' };
399
+ * let text = date_format( date, options );
400
+ */
401
+
402
+ export function date_format(date: Date, options?: any): string {
403
+ //return new Intl.DateTimeFormat(cur_locale, options).format( date );
404
+ return formatIntlDate(date);
405
+ }
406
+
407
+ /**
408
+ *
409
+ * @param date
410
+ * @param options
411
+ */
412
+
413
+ export function date_diff(date1: Date, date2: Date, options?: any): string {
414
+
415
+ var dt = (date1.getTime() - date2.getTime()) / 1000;
416
+
417
+ // seconds
418
+ let sec = dt;
419
+ if (sec < 60) {
420
+ return sprintf(_tr.global.diff_date_seconds, Math.round(sec));
421
+ }
422
+
423
+ // minutes
424
+ let min = Math.floor(sec / 60);
425
+ if (min < 60) {
426
+ return sprintf(_tr.global.diff_date_minutes, Math.round(min));
427
+ }
428
+
429
+ // hours
430
+ let hrs = Math.floor(min / 60);
431
+ return sprintf(_tr.global.diff_date_hours, hrs, min % 60);
432
+ }
433
+
434
+ export function date_to_sql(date: Date, withHours: boolean) {
435
+
436
+ if (withHours) {
437
+ return formatIntlDate(date, 'Y-M-D H:I:S');
438
+ }
439
+ else {
440
+ return formatIntlDate(date, 'Y-M-D');
441
+ }
442
+ }
443
+
444
+ /**
445
+ * construct a date from an utc date time (sql format)
446
+ * YYYY-MM-DD HH:MM:SS
447
+ */
448
+
449
+ export function date_sql_utc(date: string): Date {
450
+ let result = new Date(date + ' GMT');
451
+ return result;
452
+ }
453
+
454
+
455
+
456
+ /**
457
+ * return a number that is a representation of the date
458
+ * this number can be compared with another hash
459
+ */
460
+
461
+ export function date_hash(date: Date): number {
462
+ return date.getFullYear() << 16 | date.getMonth() << 8 | date.getDate();
463
+ }
464
+
465
+ (<any>Date.prototype).hash = function (this: Date) {
466
+ return date_hash(this);
467
+ }
468
+
469
+ /**
470
+ * return a copy of a date
471
+ */
472
+
473
+ export function date_clone(date: Date): Date {
474
+ return new Date(date.getTime());
475
+ }
476
+
477
+ /**
478
+ * return the week number of a date
479
+ */
480
+
481
+ export function date_calc_weeknum(date: Date): number {
482
+ const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
483
+ const pastDaysOfYear = (date.valueOf() - firstDayOfYear.valueOf()) / 86400000;
484
+ return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
485
+ }
486
+
487
+
488
+
489
+ /**
490
+ * parse a date according to the given format
491
+ * @param value - string date to parse
492
+ * @param fmts - format list - i18 tranlation by default
493
+ * allowed format specifiers:
494
+ * d or D: date (1 or 2 digits)
495
+ * m or M: month (1 or 2 digits)
496
+ * y or Y: year (2 or 4 digits)
497
+ * h or H: hours (1 or 2 digits)
498
+ * i or I: minutes (1 or 2 digits)
499
+ * s or S: seconds (1 or 2 digits)
500
+ * <space>: 1 or more spaces
501
+ * any other char: <0 or more spaces><the char><0 or more spaces>
502
+ * each specifiers is separated from other by a pipe (|)
503
+ * more specific at first
504
+ * @example
505
+ * 'd/m/y|d m Y|dmy|y-m-d h:i:s|y-m-d'
506
+ */
507
+
508
+ export function parseIntlDate(value: string, fmts: string = _tr.global.date_input_formats): Date {
509
+
510
+ let formats = fmts.split('|');
511
+ for (let fmatch of formats) {
512
+
513
+ //review: could do that only once & keep result
514
+ //review: add hours, minutes, seconds
515
+
516
+ let smatch = '';
517
+ for (let c of fmatch) {
518
+
519
+ if (c == 'd' || c == 'D') {
520
+ smatch += '(?<day>\\d{1,2})';
521
+ }
522
+ else if (c == 'm' || c == 'M') {
523
+ smatch += '(?<month>\\d{1,2})';
524
+ }
525
+ else if (c == 'y' || c == 'Y') {
526
+ smatch += '(?<year>\\d{1,4})';
527
+ }
528
+ else if (c == 'h' || c == 'H') {
529
+ smatch += '(?<hour>\\d{1,2})';
530
+ }
531
+ else if (c == 'i' || c == 'I') {
532
+ smatch += '(?<min>\\d{1,2})';
533
+ }
534
+ else if (c == 's' || c == 'S') {
535
+ smatch += '(?<sec>\\d{1,2})';
536
+ }
537
+ else if (c == ' ') {
538
+ smatch += '\\s+';
539
+ }
540
+ else {
541
+ smatch += '\\s*\\' + c + '\\s*';
542
+ }
543
+ }
544
+
545
+ let rematch = new RegExp('^' + smatch + '$', 'm');
546
+
547
+ let match = rematch.exec(value);
548
+
549
+ if (match) {
550
+ let d = parseInt(match.groups.day ?? '1');
551
+ let m = parseInt(match.groups.month ?? '1');
552
+ let y = parseInt(match.groups.year ?? '1970');
553
+ let h = parseInt(match.groups.hour ?? '0');
554
+ let i = parseInt(match.groups.min ?? '0');
555
+ let s = parseInt(match.groups.sec ?? '0');
556
+
557
+ if (y > 0 && y < 100) {
558
+ y += 2000;
559
+ }
560
+
561
+ let result = new Date(y, m - 1, d, h, i, s, 0);
562
+
563
+ // we test the vdate validity (without adjustments)
564
+ // without this test, date ( 0, 0, 0) is accepted and transformed to 1969/11/31 (not fun)
565
+ let ty = result.getFullYear(),
566
+ tm = result.getMonth() + 1,
567
+ td = result.getDate();
568
+
569
+ if (ty != y || tm != m || td != d) {
570
+ //debugger;
571
+ return null;
572
+ }
573
+
574
+ return result;
575
+ }
576
+ }
577
+
578
+ return null;
579
+ }
580
+
581
+ /**
582
+ * format a date as string
583
+ * @param date - date to format
584
+ * @param fmt - format
585
+ * format specifiers:
586
+ * d: date
587
+ * D: 2 digits date padded with 0
588
+ * j: day of week short mode 'mon'
589
+ * J: day of week long mode 'monday'
590
+ * w: week number
591
+ * m: month
592
+ * M: 2 digits month padded with 0
593
+ * o: month short mode 'jan'
594
+ * O: month long mode 'january'
595
+ * y or Y: year
596
+ * h: hour (24 format)
597
+ * H: 2 digits hour (24 format) padded with 0
598
+ * i: minutes
599
+ * I: 2 digits minutes padded with 0
600
+ * s: seconds
601
+ * S: 2 digits seconds padded with 0
602
+ * a: am or pm
603
+ * anything else is inserted
604
+ * if you need to insert some text, put it between {}
605
+ *
606
+ * @example
607
+ *
608
+ * 01/01/1970 11:25:00 with '{this is my demo date formatter: }H-i*M'
609
+ * "this is my demo date formatter: 11-25*january"
610
+ */
611
+
612
+ export function formatIntlDate(date: Date, fmt: string = _tr.global.date_format) {
613
+
614
+ if (!date) {
615
+ return '';
616
+ }
617
+
618
+ let now = {
619
+ year: date.getFullYear(),
620
+ month: date.getMonth() + 1,
621
+ day: date.getDate(),
622
+ wday: date.getDay(),
623
+ hours: date.getHours(),
624
+ minutes: date.getMinutes(),
625
+ seconds: date.getSeconds(),
626
+ milli: date.getMilliseconds()
627
+ };
628
+
629
+
630
+ let result = '';
631
+ let esc = 0;
632
+
633
+ for (let c of fmt) {
634
+
635
+ if (c == '{') {
636
+ if (++esc == 1) {
637
+ continue;
638
+ }
639
+ }
640
+ else if (c == '}') {
641
+ if (--esc == 0) {
642
+ continue;
643
+ }
644
+ }
645
+
646
+ if (esc) {
647
+ result += c;
648
+ continue;
649
+ }
650
+
651
+ if (c == 'd') {
652
+ result += now.day;
653
+ }
654
+ else if (c == 'D') {
655
+ result += pad(now.day, -2);
656
+ }
657
+ else if (c == 'j') { // day short
658
+ result += _tr.global.day_short[now.wday];
659
+ }
660
+ else if (c == 'J') { // day long
661
+ result += _tr.global.day_long[now.wday];
662
+ }
663
+ else if (c == 'w') { // week
664
+ result += date_calc_weeknum(date);
665
+ }
666
+ else if (c == 'W') { // week
667
+ result += pad(date_calc_weeknum(date), -2);
668
+ }
669
+ else if (c == 'm') {
670
+ result += now.month;
671
+ }
672
+ else if (c == 'M') {
673
+ result += pad(now.month, -2);
674
+ }
675
+ else if (c == 'o') { // month short
676
+ result += _tr.global.month_short[now.month - 1];
677
+ }
678
+ else if (c == 'O') { // month long
679
+ result += _tr.global.month_long[now.month - 1];
680
+ }
681
+ else if (c == 'y' || c == 'Y') {
682
+ result += pad(now.year, -4);
683
+ }
684
+ else if (c == 'a' || c == 'A') {
685
+ result += now.hours < 12 ? 'am' : 'pm';
686
+ }
687
+ else if (c == 'h') {
688
+ result += now.hours;
689
+ }
690
+ else if (c == 'H') {
691
+ result += pad(now.hours, -2);
692
+ }
693
+ else if (c == 'i') {
694
+ result += now.minutes;
695
+ }
696
+ else if (c == 'I') {
697
+ result += pad(now.minutes, -2);
698
+ }
699
+ else if (c == 's') {
700
+ result += now.seconds;
701
+ }
702
+ else if (c == 'S') {
703
+ result += pad(now.seconds, -2);
704
+ }
705
+ else if (c == 'l') {
706
+ result += now.milli;
707
+ }
708
+ else if (c == 'L') {
709
+ result += pad(now.milli, -3);
710
+ }
711
+ else {
712
+ result += c;
713
+ }
714
+ }
715
+
716
+ return result;
717
+ }
718
+
719
+ export function calcAge(birth: Date, ref?: Date) {
720
+ if (ref === undefined) {
721
+ ref = new Date();
722
+ }
723
+
724
+ if (!birth) {
725
+ return 0;
726
+ }
727
+
728
+ let age = ref.getFullYear() - birth.getFullYear();
729
+ if (ref.getMonth() < birth.getMonth() || (ref.getMonth() == birth.getMonth() && ref.getDate() < birth.getDate())) {
730
+ age--;
731
+ }
732
+
733
+ return age;
734
+ }
735
+
736
+
737
+ /**
738
+ * date object patch
739
+ */
740
+
741
+ declare global {
742
+ interface Date {
743
+ hash(): number;
744
+ clone(): Date;
745
+ weekNum(): number;
746
+ format(format: string): string;
747
+ isSameDay(date: Date): boolean;
748
+ addDays( days: number );
749
+ }
750
+ }
751
+
752
+ Date.prototype.isSameDay = function (date: Date) {
753
+ return this.getDate() == date.getDate() && this.getMonth() == date.getMonth() && this.getFullYear() == date.getFullYear();
754
+ }
755
+
756
+ Date.prototype.hash = function () {
757
+ return date_hash(this);
758
+ }
759
+
760
+ Date.prototype.clone = function () {
761
+ return date_clone(this);
762
+ }
763
+
764
+ Date.prototype.weekNum = function () {
765
+ return date_calc_weeknum(this);
766
+ }
767
+
768
+ Date.prototype.format = function (fmt: string) {
769
+ return formatIntlDate(this, fmt);
770
+ }
771
+
772
+ Date.prototype.addDays = function ( days: number ): Date {
773
+ this.setDate( this.getDate() + days );
774
+ return this;
775
+ }
776
+
777
+
778
+
779
+ // :: FILE CREATION ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
780
+
781
+
782
+ /**
783
+ *
784
+ * @param data data to export
785
+ * @param mimetype - 'text/plain'
786
+ */
787
+
788
+ export function downloadData(data, mimetype: string, filename: string) {
789
+
790
+ //if (data !== null && navigator.msSaveBlob) {
791
+ // return navigator.msSaveBlob(new Blob([data], { type: mimetype }), filename);
792
+ //}
793
+
794
+ let blob = new Blob([data], { type: mimetype });
795
+ let url = window.URL.createObjectURL(blob);
796
+
797
+ let a = document.createElement("a");
798
+ a.style['display'] = "none";
799
+ a.href = url;
800
+ a.download = filename;
801
+ document.body.appendChild(a);
802
+
803
+ a.click();
804
+ window.URL.revokeObjectURL(url);
805
+ }
806
+
807
+ // :: MISC ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
808
+
809
+
810
+ /**
811
+ * check if a value is a string
812
+ * @param val
813
+ */
814
+ export function isString(val: any): val is string {
815
+ return typeof val === 'string';
816
+ }
817
+
818
+ /**
819
+ * check is a value is an array
820
+ * @param val
821
+ */
822
+ export function isArray(val: any): val is any[] {
823
+ return val instanceof Array;
824
+ }
825
+
826
+ /**
827
+ *
828
+ */
829
+
830
+ export function isFunction(val: any): val is Function {
831
+ return val instanceof Function;
832
+ }
833
+
834
+ /**
835
+ *
836
+ */
837
+
838
+ export function isLiteralObject(val: any) {
839
+ return (!!val) && (val.constructor === Object);
840
+ };
841
+
842
+ /**
843
+ * prepend 0 to a value to a given length
844
+ * @param value
845
+ * @param length
846
+ */
847
+
848
+ export function pad(what: unknown, size: number, ch: string = '0') {
849
+
850
+ let value: string;
851
+
852
+ if (!isString(what)) {
853
+ value = '' + what;
854
+ }
855
+ else {
856
+ value = what;
857
+ }
858
+
859
+ if (size > 0) {
860
+ return value.padEnd(size, ch);
861
+ }
862
+ else {
863
+ return value.padStart(-size, ch);
864
+ }
865
+ }
866
+
867
+
868
+ /**
869
+ * return true if val is a finite number
870
+ */
871
+
872
+ export function isNumber(val: any): val is number {
873
+ return typeof val === 'number' && isFinite(val);
874
+ }
875
+
876
+ /**
877
+ *
878
+ * @param name
879
+ */
880
+
881
+ export function waitFontLoading(name: string) {
882
+
883
+ // tip for waiting font loading:
884
+ // by default, body is created invisible ((opacity = 0)
885
+ // we create a div inside with the font we need to wait the loading
886
+ // as soon as the font is loaded, it's size will change, the browser end font loading
887
+ // we can remove the div.
888
+ // pitfall: if the font is already loaded, ne never end.
889
+ // @review that
890
+
891
+ let ct = document.createElement('div');
892
+
893
+ ct.style.position = 'absolute';
894
+ ct.style.fontFamily = name;
895
+ ct.style.fontSize = '44px';
896
+ ct.style.opacity = '0.001';
897
+ ct.innerText = 'X';
898
+
899
+ document.body.appendChild(ct);
900
+
901
+ return new Promise<void>((resolve) => {
902
+
903
+ let irc = ct.getBoundingClientRect();
904
+ let tm = setInterval(() => {
905
+
906
+ let nrc = ct.getBoundingClientRect();
907
+ if (nrc.height != irc.height) {
908
+ clearInterval(tm);
909
+ document.body.removeChild(ct);
910
+
911
+ resolve();
912
+ }
913
+ }, 0);
914
+ });
915
+ }
916
+
917
+ /**
918
+ *
919
+ * @param fn
920
+ * @param tm
921
+ *
922
+ * @example:
923
+ *
924
+ * defer( ( ) => {
925
+ * console.log( x );
926
+ * } )( );
927
+ */
928
+
929
+ export function deferCall(fn: Function, tm = 0, ...args) {
930
+ setTimeout(fn, tm, ...args);
931
+ }
932
+
933
+ /**
934
+ *
935
+ */
936
+
937
+ export function asap(cb: (time: number) => void) {
938
+ requestAnimationFrame(cb);
939
+ }
940
+
941
+
942
+
943
+ /**
944
+ * micro md to html
945
+ *
946
+ * understand:
947
+ * **bold**
948
+ * *italic*
949
+ *
950
+ * > quote
951
+ * - list
952
+ * # title lvl 1
953
+ * ## title lvl 2
954
+ * ### title lvl 3 ...
955
+ *
956
+ */
957
+
958
+ export function markdownToHtml(text: string): string {
959
+
960
+ if (!text) {
961
+ return '';
962
+ }
963
+
964
+ let lines = text.split('\n');
965
+
966
+ let state = 'para';
967
+ let div = 'p';
968
+ let result: string[] = [];
969
+
970
+ lines.forEach((l) => {
971
+
972
+ let txt = l.trim();
973
+
974
+ if (state == 'para') {
975
+ // entree en mode list
976
+ if (txt[0] == '-') {
977
+ txt = txt.substr(1);
978
+ result.push('<ul>')
979
+ state = 'list';
980
+ div = 'li';
981
+ }
982
+ else if (txt[0] == '>') {
983
+ txt = txt.substr(1);
984
+ result.push('<blockquote>')
985
+ state = 'quote';
986
+ div = 'p';
987
+ }
988
+ else if (txt[0] == '#') {
989
+ let lvl = 0;
990
+ do {
991
+ txt = txt.substr(1);
992
+ lvl++;
993
+ } while (txt[0] == '#' && lvl < 5);
994
+
995
+ div = 'h' + lvl;
996
+ state = 'title';
997
+ }
998
+ }
999
+ else if (state == 'list') {
1000
+ // sortie du mode list
1001
+ if (txt[0] != '-') {
1002
+ result.push('</ul>')
1003
+ state = 'para';
1004
+ div = 'p';
1005
+ }
1006
+ else {
1007
+ txt = txt.substr(1);
1008
+ }
1009
+ }
1010
+ else if (state == 'quote') {
1011
+
1012
+ // sortie du mode blockquote
1013
+ if (txt[0] != '>') {
1014
+ result.push('</blockquote>')
1015
+ state = 'para';
1016
+ div = 'p';
1017
+ }
1018
+ else {
1019
+ txt = txt.substr(1);
1020
+ }
1021
+ }
1022
+
1023
+ let reBold = /\*\*([^*]+)\*\*/gi;
1024
+
1025
+ txt = escapeHtml(txt, false);
1026
+ txt = txt.replace(reBold, (sub: string, ...a): string => {
1027
+ return '<b>' + sub.substr(2, sub.length - 4) + '</b>';
1028
+ });
1029
+
1030
+ let reItalic = /\*([^*]+)\*/gi;
1031
+ txt = txt.replace(reItalic, (sub: string, ...a): string => {
1032
+ return '<i>' + sub.substr(1, sub.length - 2) + '</i>';
1033
+ });
1034
+
1035
+ // keep empty lines
1036
+ if (txt == '') {
1037
+ txt = '&nbsp;';
1038
+ }
1039
+
1040
+ result.push(`<${div}>` + txt + `</${div}>`);
1041
+
1042
+ if (state == 'title') {
1043
+ state = 'para';
1044
+ div = 'p';
1045
+ }
1046
+ });
1047
+
1048
+ if (state == 'list') {
1049
+ result.push('</ul>')
1050
+ }
1051
+ else if (state == 'quote') {
1052
+ result.push('</blockquote>')
1053
+ }
1054
+
1055
+ return result.join('');
1056
+ }
1057
+
1058
+
1059
+ /**
1060
+ *
1061
+ */
1062
+
1063
+ export class NetworkError extends Error {
1064
+
1065
+ private m_code: number;
1066
+
1067
+ constructor(response: Response);
1068
+ constructor(code: number, text: string);
1069
+ constructor(a: number | Response, b?: string) {
1070
+
1071
+ if (a instanceof Response) {
1072
+ super(a.statusText);
1073
+ this.m_code = a.status;
1074
+ }
1075
+ else {
1076
+ super(b);
1077
+ this.m_code = a;
1078
+ }
1079
+ }
1080
+
1081
+ get code(): number {
1082
+ return this.m_code;
1083
+ }
1084
+ }
1085
+
1086
+ /**
1087
+ * return the mouse pos in client coordinates
1088
+ * handle correctly touch & mouse
1089
+ */
1090
+
1091
+ export function getMousePos(ev: UIEvent, fromDoc: boolean): Point {
1092
+
1093
+ let x_name = 'offsetX',
1094
+ y_name = 'offsetY';
1095
+
1096
+ if (fromDoc) {
1097
+ x_name = 'clientX';
1098
+ y_name = 'clientY';
1099
+ }
1100
+
1101
+ if (ev.type == 'mousemove' || ev.type == 'mousedown' || ev.type == 'mouseup') {
1102
+ let em = ev as MouseEvent;
1103
+ return new Point(em[x_name], em[y_name]);
1104
+ }
1105
+ else if (ev.type == 'pointermove' || ev.type == 'pointerdown' || ev.type == 'pointerup') {
1106
+ let em = ev as MouseEvent;
1107
+ return new Point(em[x_name], em[y_name]);
1108
+ }
1109
+ else if (ev.type == 'touchmove' || ev.type == 'touchstart') {
1110
+ let et = ev as TouchEvent;
1111
+ return new Point(et.touches[0][x_name], et.touches[0][y_name]);
1112
+ }
1113
+ else if (ev.type == 'contextmenu') {
1114
+ let em = ev as MouseEvent;
1115
+ return new Point(em[x_name], em[y_name]);
1116
+ }
1117
+ else {
1118
+ return new Point(0, 0);
1119
+ }
1120
+ }
1121
+
1122
+ /**
1123
+ * clamp a value
1124
+ * @param v - value to clamp
1125
+ * @param min - min value
1126
+ * @param max - max value
1127
+ * @returns clamped value
1128
+ */
1129
+
1130
+ export function clamp(v: number, min: number, max: number) {
1131
+ return Math.min(Math.max(v, min), max);
1132
+ }
1133
+
1134
+
1135
+ /**
1136
+ *
1137
+ */
1138
+
1139
+ export interface IDisposable {
1140
+ dispose();
1141
+ }
1142
+
1143
+ // :: HTML strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1144
+
1145
+ export class HtmlString extends String {
1146
+ constructor(text: string) { super(text); }
1147
+
1148
+ static from(text: string) {
1149
+ return new HtmlString(text);
1150
+ }
1151
+ }
1152
+
1153
+ export function html(a, ...b): HtmlString {
1154
+ return HtmlString.from(String.raw(a, ...b));
1155
+ }
1156
+
1157
+ export function isHtmlString(val: any): val is HtmlString {
1158
+ return val instanceof HtmlString;
1159
+ }
1160
+
1161
+ // :: CLIPBOARD ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1162
+
1163
+ export class Clipboard {
1164
+ static copy(data: any) {
1165
+ if (navigator.clipboard) {
1166
+ navigator.clipboard.writeText(JSON.stringify(data));
1167
+ }
1168
+ }
1169
+
1170
+ static paste(cb: (data: string) => void) {
1171
+ if (navigator.clipboard) {
1172
+ navigator.clipboard.readText().then(v => cb(v));
1173
+ }
1174
+ else {
1175
+ console.error('no clipboard, are you in https ?');
1176
+ }
1177
+ }
1178
+ }
1179
+
1180
+
1181
+ // :: CRC32 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1182
+
1183
+ /**
1184
+ * Calculates the CRC32 checksum of a string.
1185
+ * taken from: https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
1186
+ *
1187
+ * @param {String} str
1188
+ * @param {Boolean} hex
1189
+ * @return {String} checksum
1190
+ * @api public
1191
+ */
1192
+
1193
+ export function crc32(str) {
1194
+
1195
+ let crc = ~0;
1196
+ for (let i = 0, l = str.length; i < l; i++) {
1197
+ crc = (crc >>> 8) ^ crc32tab[(crc ^ str.charCodeAt(i)) & 0xff];
1198
+ }
1199
+ return Math.abs(crc ^ -1);
1200
+ }
1201
+
1202
+ var crc32tab = [
1203
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
1204
+ 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
1205
+ 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
1206
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
1207
+ 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
1208
+ 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
1209
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
1210
+ 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
1211
+ 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
1212
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
1213
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
1214
+ 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
1215
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
1216
+ 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
1217
+ 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
1218
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
1219
+ 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
1220
+ 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
1221
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
1222
+ 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
1223
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
1224
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
1225
+ 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
1226
+ 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
1227
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
1228
+ 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
1229
+ 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
1230
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
1231
+ 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
1232
+ 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
1233
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
1234
+ 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
1235
+ 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
1236
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
1237
+ 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
1238
+ 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
1239
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
1240
+ 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
1241
+ 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
1242
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
1243
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
1244
+ 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
1245
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
1246
+ 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
1247
+ 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
1248
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
1249
+ 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
1250
+ 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
1251
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
1252
+ 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
1253
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
1254
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
1255
+ 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
1256
+ 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
1257
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
1258
+ 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
1259
+ 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
1260
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
1261
+ 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
1262
+ 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
1263
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
1264
+ 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
1265
+ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
1266
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
1267
+ ];
1268
+
1269
+
1270
+ // :: MIXINS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1271
+
1272
+ /**
1273
+ * taken from this excellent article:
1274
+ * https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
1275
+ *
1276
+ * @example:
1277
+ * class MyClass extends mix(MyBaseClass).with(Mixin1, Mixin2) {
1278
+ * }
1279
+ **/
1280
+
1281
+ export const mix = (superclass) => new MixinBuilder(superclass);
1282
+
1283
+ class MixinBuilder {
1284
+
1285
+ private superclass: any;
1286
+
1287
+ constructor(superclass) {
1288
+ this.superclass = superclass;
1289
+ }
1290
+
1291
+ with(...mixins) {
1292
+ return mixins.reduce((c, mixin) => mixin(c), this.superclass);
1293
+ }
1294
+ }
1295
+
1296
+ /**
1297
+ * @example
1298
+ *
1299
+ * ```
1300
+ * const cls = classNames( 'class1 class2', {
1301
+ * 'class3': false,
1302
+ * 'class4': true,
1303
+ * });
1304
+ *
1305
+ * // even shorter
1306
+ * const class1 = true, class2 = false;
1307
+ * const cls = classNames( { class1, class2 } ); // cls = "class1"
1308
+ *
1309
+ * ```
1310
+ *
1311
+ * @returns
1312
+ */
1313
+
1314
+ export function classNames(...args: (string | any)[]) {
1315
+
1316
+ let result = '';
1317
+
1318
+ for (const cls of args) {
1319
+ if (typeof cls === 'string') {
1320
+ result += ' ' + cls;
1321
+ }
1322
+ else if (cls) {
1323
+ for (const c in cls) {
1324
+ if (cls[c])
1325
+ result += ' ' + c;
1326
+ }
1327
+ }
1328
+ }
1329
+
1330
+ return result;
1331
+ }
1332
+
1333
+ /**
1334
+ *
1335
+ */
1336
+
1337
+ interface PasswordRule {
1338
+ chars: string;
1339
+ min: number;
1340
+ }
1341
+
1342
+ export function generatePassword(length: number, rules?: PasswordRule[]) {
1343
+
1344
+ if (!length || length == undefined) {
1345
+ length = 8;
1346
+ }
1347
+
1348
+ if (!rules) {
1349
+ rules = [
1350
+ { chars: "abcdefghijklmnopqrstuvwxyz", min: 3 }, // As least 3 lowercase letters
1351
+ { chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", min: 2 }, // At least 2 uppercase letters
1352
+ { chars: "0123456789", min: 2 }, // At least 2 digits
1353
+ { chars: "!@#$*|%+-_.;", min: 2 } // At least 1 special char
1354
+ ];
1355
+ }
1356
+
1357
+ let allChars = "";
1358
+ let allMin = 0;
1359
+
1360
+ rules.forEach(function (rule) {
1361
+ allChars += rule.chars;
1362
+ allMin += rule.min;
1363
+ });
1364
+
1365
+ if (length < allMin) {
1366
+ length = allMin;
1367
+ }
1368
+
1369
+ rules.push({ chars: allChars, min: length - allMin });
1370
+
1371
+ let pswd = "";
1372
+ rules.forEach(function (rule) {
1373
+ if (rule.min > 0) {
1374
+ pswd += shuffle(rule.chars, rule.min);
1375
+ }
1376
+ });
1377
+
1378
+ return shuffle(pswd);
1379
+ }
1380
+
1381
+ function shuffle(str: string, maxlength?: number) {
1382
+ let shuffled = str.split('').sort(() => {
1383
+ return 0.5 - Math.random()
1384
+ }).join('');
1385
+
1386
+ if (maxlength > 0) {
1387
+ shuffled = shuffled.substr(0, maxlength);
1388
+ }
1389
+
1390
+ return shuffled;
1391
+ }