nmce-func 0.0.3 → 0.0.7

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 (54) hide show
  1. package/_func/addressFunc.d.ts +31 -0
  2. package/_func/authentication.service.d.ts +24 -0
  3. package/_func/currencyFunc.d.ts +20 -0
  4. package/_func/dateFunc.d.ts +121 -0
  5. package/_func/htmlPrintFunc.d.ts +18 -0
  6. package/_func/index.d.ts +10 -0
  7. package/_func/javascriptFunc.d.ts +9 -0
  8. package/_func/jsonFunc.d.ts +31 -0
  9. package/_func/stringAusFunc.d.ts +40 -0
  10. package/_func/stringFunc.d.ts +41 -0
  11. package/_func/uuidFunc.d.ts +7 -0
  12. package/bundles/nmce-func.umd.js +1294 -0
  13. package/bundles/nmce-func.umd.js.map +1 -0
  14. package/bundles/nmce-func.umd.min.js +2 -0
  15. package/bundles/nmce-func.umd.min.js.map +1 -0
  16. package/esm2015/_func/addressFunc.js +49 -0
  17. package/esm2015/_func/authentication.service.js +41 -0
  18. package/esm2015/_func/currencyFunc.js +72 -0
  19. package/esm2015/_func/dateFunc.js +316 -0
  20. package/esm2015/_func/htmlPrintFunc.js +45 -0
  21. package/esm2015/_func/index.js +11 -0
  22. package/esm2015/_func/javascriptFunc.js +17 -0
  23. package/esm2015/_func/jsonFunc.js +67 -0
  24. package/esm2015/_func/stringAusFunc.js +200 -0
  25. package/esm2015/_func/stringFunc.js +93 -0
  26. package/esm2015/_func/uuidFunc.js +17 -0
  27. package/esm2015/nmce-func.js +5 -0
  28. package/esm2015/public-api.js +5 -0
  29. package/fesm2015/nmce-func.js +927 -0
  30. package/fesm2015/nmce-func.js.map +1 -0
  31. package/nmce-func.d.ts +4 -0
  32. package/nmce-func.metadata.json +1 -0
  33. package/package.json +18 -4
  34. package/public-api.d.ts +1 -0
  35. package/karma.conf.js +0 -44
  36. package/ng-package.json +0 -11
  37. package/src/_func/addressFunc.spec.ts +0 -12
  38. package/src/_func/addressFunc.ts +0 -54
  39. package/src/_func/authentication.service.ts +0 -47
  40. package/src/_func/currencyFunc.ts +0 -71
  41. package/src/_func/dateFunc.spec.ts +0 -191
  42. package/src/_func/dateFunc.ts +0 -385
  43. package/src/_func/helperFunc.ts +0 -27
  44. package/src/_func/htmlFunc.ts +0 -46
  45. package/src/_func/index.ts +0 -9
  46. package/src/_func/jsFunc.ts +0 -48
  47. package/src/_func/stringFunc.spec.ts +0 -142
  48. package/src/_func/stringFunc.ts +0 -337
  49. package/src/public-api.ts +0 -5
  50. package/src/test.ts +0 -26
  51. package/tsconfig.lib.json +0 -25
  52. package/tsconfig.lib.prod.json +0 -11
  53. package/tsconfig.spec.json +0 -17
  54. package/tslint.json +0 -17
@@ -0,0 +1,927 @@
1
+ import { HttpClient } from '@angular/common/http';
2
+ import { Injectable, Inject } from '@angular/core';
3
+ import { map } from 'rxjs/operators';
4
+ import moment from 'moment';
5
+ import { v4 } from 'uuid';
6
+
7
+ class AddressFunc {
8
+ /**
9
+ * Compose to one line separated by comma
10
+ * @param st1
11
+ * @param st2
12
+ * @param city
13
+ * @param state
14
+ * @param postcode
15
+ */
16
+ static composeOneLineAddress(st1, st2, city, state, postcode) {
17
+ return AddressFunc.composeAddress(st1, st2, city, state, postcode, ', ');
18
+ }
19
+ /**
20
+ * Compose to multiple separated by \n
21
+ * @param st1
22
+ * @param st2
23
+ * @param city
24
+ * @param state
25
+ * @param postcode
26
+ */
27
+ static composeMultiLineAddress(st1, st2, city, state, postcode) {
28
+ return AddressFunc.composeAddress(st1, st2, city, state, postcode, '\n');
29
+ }
30
+ /**
31
+ * Compose with separator
32
+ * @param st1
33
+ * @param st2
34
+ * @param city
35
+ * @param state
36
+ * @param postcode
37
+ * @param sep
38
+ */
39
+ static composeAddress(st1, st2, city, state, postcode, sep) {
40
+ const r = (st1 ? (st1 + sep) : '') + (st2 ? (st2 + sep) : '') + (city ? (city + sep) : '') + (state ? (state + ' ') : '') + (postcode ? postcode : '');
41
+ return r;
42
+ }
43
+ static composeGoogleMapsAuUrl(st1, st2, city, state, country) {
44
+ const googleBaseUrl = 'https://www.google.com.au/maps?hl=en&q=';
45
+ const fff = (s) => {
46
+ if (!s) {
47
+ return '';
48
+ }
49
+ return '+' + encodeURIComponent(s.replace(' ', '+')) + ',';
50
+ };
51
+ const ss = fff(st1) + fff(st2) + fff(city) + fff(state) + fff(country);
52
+ return googleBaseUrl + ss;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Login and saving tokens in sessionStorage.
58
+ * App needs to provide constant 'auth.tokenUrl'.
59
+ */
60
+ class AuthenticationService {
61
+ constructor(authUri, http) {
62
+ this.authUri = authUri;
63
+ this.http = http;
64
+ }
65
+ /**
66
+ * Login and save tokens to sessionStorage then return an observable.
67
+ * @param username
68
+ * @param password
69
+ */
70
+ login(username, password) {
71
+ const body = 'username=' + username + '&password=' + password + '&grant_type=password';
72
+ const options = { headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } };
73
+ return this.http.post(this.authUri, body, options)
74
+ .pipe(map(response => {
75
+ //sessionStorage.setItem('access_token', response.access_token); The client code is response to doing these.
76
+ //sessionStorage.setItem('expires_in', response.expires_in.toString());
77
+ //sessionStorage.setItem('token_type', response.token_type);
78
+ //sessionStorage.setItem('issued', response.issued);
79
+ //sessionStorage.setItem('expires', response.expires); // often up to 2 weeks by default in Asp.net identity 2.
80
+ this.userName = response.username;
81
+ //APP_STATUSES.userName = this.userName;
82
+ return response;
83
+ }));
84
+ }
85
+ }
86
+ AuthenticationService.decorators = [
87
+ { type: Injectable }
88
+ ];
89
+ AuthenticationService.ctorParameters = () => [
90
+ { type: String, decorators: [{ type: Inject, args: ['auth.tokenUrl',] }] },
91
+ { type: HttpClient }
92
+ ];
93
+
94
+ /**
95
+ * Currency calculations. Undefined input of number is considered zero, just like null.
96
+ */
97
+ class CurrencyFunc {
98
+ /**
99
+ *
100
+ * @param num
101
+ * @param decimalPlaces default 0
102
+ */
103
+ static bankerRound(num, decimalPlaces) {
104
+ if (!num) {
105
+ return 0;
106
+ }
107
+ const d = decimalPlaces || 0;
108
+ const m = Math.pow(10, d);
109
+ const n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
110
+ const i = Math.floor(n), f = n - i;
111
+ const e = 1e-8; // Allow for rounding errors in f
112
+ const r = (f > 0.5 - e && f < 0.5 + e) ?
113
+ ((i % 2 === 0) ? i : i + 1) : Math.round(n);
114
+ return d ? r / m : r;
115
+ }
116
+ static bankerRoundTo5cents(num) {
117
+ if (!num) {
118
+ return 0;
119
+ }
120
+ const r = this.bankerRound(Math.ceil(num * 20 - 0.5) / 20, 2);
121
+ return r;
122
+ }
123
+ static ceilTo5cents(num) {
124
+ if (!num) {
125
+ return 0;
126
+ }
127
+ const r = this.bankerRound(Math.ceil(num * 20) / 20, 4);
128
+ const roundup = Math.ceil(r * 10000) / 10000;
129
+ return roundup;
130
+ }
131
+ static transformCurrency(value, fractionSize = 2) {
132
+ let [integer, fraction = ''] = (value || '').toString()
133
+ .split(this.DECIMAL_SEPARATOR);
134
+ fraction = fractionSize > 0
135
+ ? this.DECIMAL_SEPARATOR + (fraction + this.PADDING).substring(0, fractionSize)
136
+ : '';
137
+ integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, this.THOUSANDS_SEPARATOR);
138
+ return integer + fraction;
139
+ }
140
+ static parseCurrency(value, fractionSize = 2) {
141
+ let [integer, fraction = ''] = (value || '').split(this.DECIMAL_SEPARATOR);
142
+ integer = integer.replace(new RegExp(this.THOUSANDS_SEPARATOR, 'g'), '');
143
+ fraction = parseInt(fraction, 10) > 0 && fractionSize > 0
144
+ ? this.DECIMAL_SEPARATOR + (fraction + this.PADDING).substring(0, fractionSize)
145
+ : '';
146
+ return integer + fraction;
147
+ }
148
+ //http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript
149
+ static pad(num, size) {
150
+ num = null;
151
+ let s = num + '';
152
+ while (s.length < size) {
153
+ s = '0' + s;
154
+ }
155
+ return s;
156
+ }
157
+ static sum(ns) {
158
+ const r = ns.reduce((a, b) => (a !== null && a !== void 0 ? a : 0) + (b !== null && b !== void 0 ? b : 0), 0);
159
+ return r;
160
+ }
161
+ }
162
+ CurrencyFunc.DECIMAL_SEPARATOR = '.';
163
+ CurrencyFunc.THOUSANDS_SEPARATOR = ',';
164
+ CurrencyFunc.PADDING = '000000';
165
+
166
+ class DateFunc {
167
+ /**
168
+ * Transform UTC DateTime to local date without H, M and S. For example, the month day of 2018-01-23T22:00:00Z is 24 in Australia.
169
+ * @param dtUtc
170
+ * @param offsetMinutes if not defined, it will be new Date().getTimezoneOffset(). //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
171
+ */
172
+ static dateTimeUtcToLocalDateNumber(dtUtc) {
173
+ if (!dtUtc) {
174
+ return 0; //0 is better for calculation by the clients.
175
+ }
176
+ const localDt = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
177
+ const localDNum = localDt.setHours(0, 0, 0, 0);
178
+ return localDNum;
179
+ }
180
+ /**
181
+ * Date only. However, the date may still be in UTC.
182
+ * @param dtUtc
183
+ */
184
+ static dateTimeUtcToLocalDate(dtUtc) {
185
+ const localDt = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
186
+ const localD = localDt.setHours(0, 0, 0, 0);
187
+ return new Date(localD);
188
+ }
189
+ static localISODateString(dtUtc) {
190
+ const dt = moment(dtUtc).local();
191
+ return dt.format('YYYY-MM-DD');
192
+ }
193
+ /**
194
+ * locate date ONLY (no time) to UTC date.
195
+ * @param dt if dt contain time info, it will become dt.setHours(0, 0, 0, 0)
196
+ */
197
+ static localDateToUtc(d) {
198
+ const dt = moment(d).toDate();
199
+ const n = dt.setHours(0, 0, 0, 0);
200
+ const offset = dt.getTimezoneOffset() * 60000;
201
+ return new Date(n + offset);
202
+ }
203
+ static getTimezoneOffset() {
204
+ const dt = this.today;
205
+ return dt.getTimezoneOffset();
206
+ }
207
+ /**
208
+ * Transform UTC DateTime to local dateTime.
209
+ * @param dtUtc
210
+ * @param offsetMinutes if not defined, it will be new Date().getTimezoneOffset(). //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
211
+ */
212
+ static dateTimeUtcToLocalDateTime(dtUtc) {
213
+ const stillUtc = moment.utc(dtUtc).toDate();
214
+ return moment(stillUtc).local().toDate();
215
+ }
216
+ static dateTimeUtcToLocaMoment(dtUtc) {
217
+ const stillUtc = moment.utc(dtUtc);
218
+ return stillUtc.local();
219
+ }
220
+ static getEndOfWeek(dt) {
221
+ return moment(dt).endOf('isoWeek').toDate();
222
+ }
223
+ static getStartOfWeek(dt) {
224
+ return moment(dt).startOf('isoWeek').toDate();
225
+ }
226
+ static getEndOfMonth(dt) {
227
+ // return new Date(dt.getFullYear(), dt.getMonth() + 1, 0, 23, 59, 59, 999);
228
+ return moment(dt).endOf('month').toDate();
229
+ }
230
+ static getStartOfMonth(dt) {
231
+ return moment(dt).startOf('month').toDate();
232
+ }
233
+ static getDaysBetweenDates(dt1, dt2) {
234
+ return this.getDaysBetween(dt1, dt2);
235
+ }
236
+ static getEndOfDate(dt) {
237
+ return dt ? new Date(dt.setHours(23, 59, 59, 999)) :
238
+ new Date(this.now.setHours(23, 59, 59, 999));
239
+ }
240
+ static getStartOfDate(dt) {
241
+ return moment(dt).startOf('day').toDate();
242
+ }
243
+ static getEndOfToday() {
244
+ // return new Date((new Date(Date.now())).setHours(23, 59, 59, 999));
245
+ return moment(Date.now()).endOf('day').toDate();
246
+ }
247
+ static getStartOfToday() {
248
+ // return new Date((new Date(Date.now())).setHours(0, 0, 0, 0));
249
+ return moment(Date.now()).startOf('day').toDate();
250
+ }
251
+ //inspired https://stackoverflow.com/questions/563406/add-days-to-javascript-date
252
+ static addDays(dt, days = 0) {
253
+ const dat = moment(dt);
254
+ dat.add(days, 'days');
255
+ return dat.toDate();
256
+ }
257
+ static subtractDays(dt, days = 0) {
258
+ const dat = moment(dt);
259
+ dat.subtract(days, 'days');
260
+ return dat.toDate();
261
+ }
262
+ /**
263
+ * Start of today
264
+ */
265
+ static get today() {
266
+ return this.getStartOfToday();
267
+ }
268
+ static get now() {
269
+ return new Date(Date.now());
270
+ }
271
+ static getNext5MinuteMark() {
272
+ const m = moment().set('second', 0).set('millisecond', 0);
273
+ const minute = m.minute();
274
+ const mod = minute % 5;
275
+ if (mod) {
276
+ const delta = 5 - mod;
277
+ return m.add(delta, 'm').toDate();
278
+ }
279
+ return m.toDate();
280
+ }
281
+ static getYMD(d) {
282
+ return moment(d).format('YYYYMMDD');
283
+ }
284
+ static getDMYWithSlash(d) {
285
+ return moment(d).format('DD/MM/YYYY');
286
+ }
287
+ static getDMYHmWithSlash(d) {
288
+ return moment(d).format('DD/MM/YYYY HH:mm');
289
+ }
290
+ static getMcpTime(dt) {
291
+ return moment(dt).format('HH:mm:ss.SSSZ');
292
+ }
293
+ /**
294
+ * In 24 hour format
295
+ * @param dtUtc
296
+ */
297
+ static getLocalDMYHmWithSlash(dtUtc) {
298
+ const d = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
299
+ return moment(d).format('DD/MM/YYYY HH:mm');
300
+ }
301
+ /**
302
+ * Offset minutes comparing with today
303
+ */
304
+ static getOffsetMinutes(dtUtc) {
305
+ const dm1 = moment(dtUtc);
306
+ const dm2 = moment(new Date().setHours(0, 0, 0, 0));
307
+ return dm1.diff(dm2, 'minutes');
308
+ }
309
+ static getDaysBetween(d1, d2) {
310
+ const dm1 = moment(d1);
311
+ const dm2 = moment(d2);
312
+ return dm2.diff(dm1, 'days');
313
+ }
314
+ /**
315
+ * Get hour of the date. If Date is not defined, the hour will be current hour.
316
+ * @param dtUtc
317
+ */
318
+ static getHour(dtUtc) {
319
+ const m = moment(dtUtc);
320
+ return m.hours();
321
+ }
322
+ static getMinute(dtUtc) {
323
+ const m = moment(dtUtc);
324
+ return m.minutes();
325
+ }
326
+ static composeDateTime(dt, h = 0, minute = 0) {
327
+ const mt = moment(dt);
328
+ return new Date(mt.toDate().setHours(h, minute, 0, 0));
329
+ }
330
+ static olderThan24Hours(d) {
331
+ const m = moment(d);
332
+ return moment().diff(m, 'hours') >= 24;
333
+ }
334
+ static olderThan24HoursUtc(dtUtc) {
335
+ return DateFunc.getHourAgeUtc(dtUtc) >= 24;
336
+ }
337
+ static olderThanHours(d, hours) {
338
+ const m = moment(d);
339
+ return moment().diff(m, 'hours') >= hours;
340
+ }
341
+ static olderThanHoursUtc(dtUtc, hours) {
342
+ return DateFunc.getHourAgeUtc(dtUtc) >= hours;
343
+ }
344
+ static olderThanMinutes(d, minutes) {
345
+ const m = moment(d);
346
+ return moment().diff(m, 'minutes') >= minutes;
347
+ }
348
+ /**
349
+ * It could be 11PM yesterday, and 1 AM today. Actually based on local today.
350
+ */
351
+ static olderThan1Day(dtUtc) {
352
+ return DateFunc.getDayAgeUtc(dtUtc) > 0;
353
+ }
354
+ static getHourAge(d) {
355
+ const m = moment(d);
356
+ return moment().diff(m, 'hours');
357
+ }
358
+ static getHourAgeUtc(dtUtc) {
359
+ const m = moment.utc(dtUtc);
360
+ return moment.utc().diff(m, 'hours');
361
+ }
362
+ /**
363
+ * Compare utc date with utc now.
364
+ * @param dtUtc
365
+ */
366
+ static getDayAgeUtc(dtUtc) {
367
+ const m = moment.utc(dtUtc);
368
+ return moment.utc().diff(m, 'days');
369
+ }
370
+ /**
371
+ * How many years from now.
372
+ * @param d
373
+ * @returns
374
+ */
375
+ static getAge(d) {
376
+ const m = moment(d);
377
+ return moment().diff(m, 'years');
378
+ }
379
+ /**
380
+ * Year of date.
381
+ * @param d
382
+ * @returns
383
+ */
384
+ static getYear(d) {
385
+ const m = moment(d);
386
+ return m.year();
387
+ }
388
+ static getUtcNow() {
389
+ return moment.utc().toDate();
390
+ }
391
+ static addMinutes(d, m) {
392
+ return moment(d).add(m, 'm').toDate();
393
+ }
394
+ static addMonth(d, m) {
395
+ return moment(d).add(m, 'M').toDate();
396
+ }
397
+ static getDuration(d1, d2) {
398
+ const md1 = moment(d1);
399
+ const md2 = moment(d2);
400
+ return moment.duration(md2.diff(md1));
401
+ }
402
+ /**
403
+ * Convert minutes from midnight to HH:mm text
404
+ * @param mins
405
+ */
406
+ static getHMFromMins(mins) {
407
+ // do not include the first validation check if you want, for example,
408
+ // getTimeFromMins(1530) to equal getTimeFromMins(90) (i.e. mins rollover)
409
+ if (mins >= 24 * 60 || mins < 0) {
410
+ throw new RangeError('Valid input should be greater than or equal to 0 and less than 1440.');
411
+ }
412
+ const h = mins / 60 | 0, m = mins % 60 | 0;
413
+ return moment.utc().hours(h).minutes(m).format('HH:mm');
414
+ }
415
+ static getMinutesSinceMidnight(d) {
416
+ const m = moment(d);
417
+ const midnight = moment(d).startOf('day'); //Mutates the original moment by setting it to the start of a unit of time. So I have better not to use m which wil be changed by calling this function
418
+ return m.diff(midnight, 'minutes');
419
+ }
420
+ static getMinutesBetween(start, end) {
421
+ const m = moment(start);
422
+ const m2 = moment(end);
423
+ return m2.diff(m, 'minutes');
424
+ }
425
+ /**
426
+ * Parse json string with date serialized into string, and get proper date object back
427
+ * @param s
428
+ */
429
+ static dateSafeJsonParse(s) {
430
+ return JSON.parse(s, this.dateReviver);
431
+ }
432
+ static dateReviver(key, value) {
433
+ if (DateFunc.isSerializedDate(value)) {
434
+ return (new Date(value));
435
+ }
436
+ // If it's not a date-string, we want to return the value as-is. If we fail to return
437
+ // a value, it will be omitted from the resultant data structure.
438
+ return (value);
439
+ }
440
+ // I determine if the given value is a string that matches the serialized-date pattern.
441
+ static isSerializedDate(value) {
442
+ // Dates are serialized in TZ format, example: '1981-12-20T04:00:14.000Z'.
443
+ const datePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
444
+ return (DateFunc.isString(value) && datePattern.test(value));
445
+ }
446
+ // I determine if the given value is a String.
447
+ static isString(value) {
448
+ return ({}.toString.call(value) === '[object String]');
449
+ }
450
+ static dateSafeParse(s) {
451
+ const m = moment(s);
452
+ return m.toDate();
453
+ }
454
+ static composeDateWithMinutes(d, minute) {
455
+ const m = moment(d);
456
+ const midnight = moment(d).startOf('day'); // Mutates the original moment by setting it to the start of a unit of time. So I have better not to use m which wil be changed by calling this function
457
+ midnight.add(minute, 'minutes');
458
+ return midnight.toDate();
459
+ }
460
+ /**
461
+ * Safe compare since date data may be considered as string rather than date.
462
+ * @param d1
463
+ * @param d2
464
+ */
465
+ static compare(d1, d2) {
466
+ if (!d1 && !d2) {
467
+ return 0;
468
+ }
469
+ if (!d1) {
470
+ return -NaN;
471
+ }
472
+ if (!d2) {
473
+ return NaN;
474
+ }
475
+ const dd1 = (new Date(d1)).valueOf();
476
+ const dd2 = (new Date(d2)).valueOf();
477
+ return dd1 - dd2;
478
+ }
479
+ }
480
+
481
+ class HtmlPrintFunc {
482
+ /**
483
+ * Print with CSS for internal reports
484
+ * @param htmlTags
485
+ * @param cssUrl
486
+ */
487
+ static printWithCSS(htmlTags, cssUrl) {
488
+ if (window) {
489
+ const htmlToPrint = `<html><head>
490
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
491
+ <link rel="stylesheet" type="text/css" href="${cssUrl}" media="screen,print"/>
492
+ </head><body onload="window.print()">${htmlTags}</body></html>`;
493
+ const popup = window.open('', '_blank', 'width=1024,height=768');
494
+ popup === null || popup === void 0 ? void 0 : popup.document.open();
495
+ popup === null || popup === void 0 ? void 0 : popup.document.write(htmlToPrint);
496
+ popup === null || popup === void 0 ? void 0 : popup.document.close();
497
+ }
498
+ return true;
499
+ }
500
+ /**
501
+ * Print for external documents.
502
+ * @param htmlTags
503
+ */
504
+ static print(htmlTags) {
505
+ if (window) {
506
+ const htmlToPrint = `<html><head>
507
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
508
+ </head><body onload="window.print()">${htmlTags}</body></html>`;
509
+ const popup = window.open('', '_blank', 'width=1024,height=768');
510
+ popup === null || popup === void 0 ? void 0 : popup.document.open();
511
+ popup === null || popup === void 0 ? void 0 : popup.document.write(htmlToPrint);
512
+ popup === null || popup === void 0 ? void 0 : popup.document.close();
513
+ }
514
+ return true;
515
+ }
516
+ /**
517
+ * Print image url through html img.
518
+ * @param url
519
+ */
520
+ static printImage(url) {
521
+ const imageTags = `<img src="${url}" alt="Image from URL"/>`;
522
+ HtmlPrintFunc.print(imageTags);
523
+ }
524
+ }
525
+
526
+ class JavaScriptFunc {
527
+ /**
528
+ * Some business functions depend on external JavaScript libraries. Lazy loading of respective business modules is good,
529
+ * and this function supports lazy loading of JS libraries.
530
+ * @param scriptUrl
531
+ * @returns Promise for subsequent JS function calls.
532
+ */
533
+ static loadExternalScript(scriptUrl) {
534
+ return new Promise((resolve, reject) => {
535
+ const scriptElement = document.createElement('script');
536
+ scriptElement.src = scriptUrl;
537
+ scriptElement.onload = resolve;
538
+ document.body.appendChild(scriptElement);
539
+ });
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Basic JSON functions
545
+ */
546
+ class JsonFunc {
547
+ /**
548
+ *
549
+ * @param array Group by a property of array element.
550
+ * @param propertyName
551
+ * @returns
552
+ */
553
+ static groupBy(array, propertyName) {
554
+ return array.reduce(function (acc, obj) {
555
+ const key = obj[propertyName];
556
+ if (!acc[key]) {
557
+ acc[key] = [];
558
+ }
559
+ acc[key].push(obj);
560
+ return acc;
561
+ }, {});
562
+ }
563
+ /**
564
+ * Group by a date property. The key is always of string type and representing milliseconds.
565
+ * The client should convert the string to number.
566
+ * Angular date pipe could actually consume such string without explicitly converting to number.
567
+ * @param array
568
+ * @param propertyName
569
+ */
570
+ static groupByDate(array, propertyName) {
571
+ return array.reduce(function (acc, obj) {
572
+ const key = DateFunc.dateTimeUtcToLocalDateNumber(obj[propertyName]);
573
+ if (!acc[key]) {
574
+ acc[key] = [];
575
+ }
576
+ acc[key].push(obj);
577
+ return acc;
578
+ }, {});
579
+ }
580
+ /**
581
+ * Remove null or empty fields including those in nested objects.
582
+ * This is useful for reducing payload of AJAX serialization.
583
+ * @param obj
584
+ */
585
+ static removeNullOrEmptyFields(obj) {
586
+ for (const f in obj) {
587
+ let p = obj[f];
588
+ if (p === null || p === '') {
589
+ delete obj[f];
590
+ }
591
+ else if (typeof p === 'object' && p !== null) {
592
+ this.removeNullOrEmptyFields(p);
593
+ }
594
+ }
595
+ }
596
+ /**
597
+ *
598
+ * @param obj Remove null fields of object at only the 1st level.
599
+ */
600
+ static removeNullFields(obj) {
601
+ for (const f in obj) {
602
+ if (obj[f] === null) {
603
+ delete obj[f];
604
+ }
605
+ }
606
+ }
607
+ }
608
+
609
+ /**
610
+ * String functions specific to Australia
611
+ */
612
+ class StringAusFunc {
613
+ /**
614
+ * Validate medicare number
615
+ * @param n
616
+ * @returns validation error message
617
+ */
618
+ static validateMedicare(n) {
619
+ if (!n) {
620
+ return null;
621
+ }
622
+ if (n && n.length === 10) {
623
+ const matches = n.match(/^(\d{8})(\d)/);
624
+ if (!matches) {
625
+ return {
626
+ code: 2, message: 'Medicare number should be all digit.'
627
+ };
628
+ }
629
+ const base = matches[1];
630
+ const checkDigit = matches[2];
631
+ const weights = [1, 3, 7, 9, 1, 3, 7, 9];
632
+ let sum = 0;
633
+ for (let i = 0; i < weights.length; i++) {
634
+ sum += parseInt(base[i], 10) * weights[i];
635
+ }
636
+ //console.debug(`sum: ${sum} checkDigits: ${checkDigit}`);
637
+ const isValid = sum % 10 === parseInt(checkDigit, 10);
638
+ if (!isValid) {
639
+ return {
640
+ code: 3, message: 'Checksum is incorrect.'
641
+ };
642
+ }
643
+ }
644
+ else {
645
+ return { code: 1, message: 'Length should be 10.' };
646
+ }
647
+ return null;
648
+ }
649
+ static validateMedicareProviderNumber(providerNumber) {
650
+ if (!providerNumber) {
651
+ return null;
652
+ }
653
+ if (!/^[0-9]{6}[0-9ABCDEFGHJKLMNPQRTUVWXY][ABFHJKLTWXY]/.test(providerNumber)) {
654
+ return { code: 1, message: 'Not matching provider number format.' };
655
+ }
656
+ const practiceLocationValues = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y'];
657
+ const remainderValues = ['Y', 'X', 'W', 'T', 'L', 'K', 'J', 'H', 'F', 'B', 'A'];
658
+ const stemWeights = [3, 5, 8, 4, 2, 1];
659
+ const stemNumbers = providerNumber.substr(0, 6).split('').map((char) => parseInt(char));
660
+ const practiceLoc = practiceLocationValues.findIndex((e) => e === providerNumber[6]);
661
+ const checksum = providerNumber[7];
662
+ const zipped = stemWeights.map((x, i) => [x, stemNumbers[i]]);
663
+ const sumWeights = zipped.map((y) => y[1] * y[0]).reduce((total, num) => total + num);
664
+ const remainder = (sumWeights + practiceLoc * 6) % 11;
665
+ const result = remainderValues[remainder];
666
+ const valid = result === checksum;
667
+ if (!valid) {
668
+ return { code: 2, message: 'Checksum is incorrect.' };
669
+ }
670
+ return null;
671
+ }
672
+ static validateDVAFileNumber(dva) {
673
+ if (!dva) {
674
+ return null;
675
+ }
676
+ const states = ['N', 'V', 'Q', 'S', 'W', 'T'];
677
+ const warCodes = [' ', 'X', 'KM', 'SR', 'SS', 'SM', 'SWP', 'AGX', 'BW', 'GW', 'CGW',
678
+ 'P', 'PX', 'PAD', 'PAM', 'PCA', 'PCR', 'PCV', 'PMS', 'PSW', 'PWO', 'HKX', 'MAL',
679
+ 'CN', 'CNX', 'IV', 'NF', 'NG', 'RD', 'RDX', 'SA', 'SAX', 'A',
680
+ 'N', 'NX', 'NSW', 'NSM',
681
+ 'BUR', 'CNK', 'CNS', 'FIJ', 'GHA', 'HKS', 'IND', 'KYA', 'MAU', 'MLS', 'MTX', 'MWI', 'NK', 'NGR', 'NRD', 'NSS', 'PK']; //British Commonwealth Countries - SP Eligibility
682
+ if (!states.includes(dva.charAt(0))) {
683
+ return { code: 1, message: 'State incorrect.' };
684
+ }
685
+ const ns = dva.match(/\d+/);
686
+ if (!ns) {
687
+ return { code: 2, message: 'No number.' };
688
+ }
689
+ const n = ns[0];
690
+ const idxOfN = dva.indexOf(n);
691
+ const warCode = dva.substring(1, idxOfN);
692
+ if (!warCodes.includes(warCode)) {
693
+ return { code: 3, message: 'War code invalid.' };
694
+ }
695
+ const lenOfN = n.length;
696
+ const lenOfWc = warCode.length;
697
+ if (lenOfN + lenOfWc > 7) {
698
+ return { code: 4, message: 'File number length should not be greater 7.' };
699
+ }
700
+ return null;
701
+ }
702
+ static validateTFN(n) {
703
+ if (!n) {
704
+ return null;
705
+ }
706
+ const tfn = n.replace(/\s+/g, '').replace(/[-]/g, '');
707
+ const isNumber = /^[0-9]+$/.test(tfn);
708
+ if (!isNumber) {
709
+ return { code: 1, message: 'Invalid TFN, only numbers are allowed.' };
710
+ }
711
+ const length = tfn.length;
712
+ if (length !== 9) {
713
+ return {
714
+ code: 2, message: 'Invalid TFN, must have 9 digits.'
715
+ };
716
+ }
717
+ const digits = tfn.split('').map(d => parseInt(d));
718
+ const sum = (digits[0] * 1)
719
+ + (digits[1] * 4)
720
+ + (digits[2] * 3)
721
+ + (digits[3] * 7)
722
+ + (digits[4] * 5)
723
+ + (digits[5] * 8)
724
+ + (digits[6] * 6)
725
+ + (digits[7] * 9)
726
+ + (digits[8] * 10);
727
+ const remainder = sum % 11;
728
+ const valid = remainder === 0;
729
+ if (!valid) {
730
+ return { code: 3, message: 'Checksum is incorrect.' };
731
+ }
732
+ return null;
733
+ }
734
+ static addWeighted(p, v, i) {
735
+ return p + v * StringAusFunc.weights[i];
736
+ }
737
+ static addAcnWeighted(p, v, i) {
738
+ return p + v * StringAusFunc.acnWeights[i];
739
+ }
740
+ static generateLookup() {
741
+ const ns = [];
742
+ for (let i = 0; i < 10; ++i) {
743
+ ns[i * 19 % 89] = i;
744
+ }
745
+ return ns;
746
+ }
747
+ static validateABN(abn) {
748
+ if (!abn) {
749
+ return null;
750
+ }
751
+ const digits = abn.replace(/[^\d]/g, '').split('').map(Number);
752
+ if (digits.length !== 11) {
753
+ return { code: 1, message: 'Expect 11-digit.' };
754
+ }
755
+ digits[0] -= 1;
756
+ const sum = digits.reduce(StringAusFunc.addWeighted, 0);
757
+ if (sum % 89 === 0) {
758
+ return null;
759
+ }
760
+ digits[0] += 1;
761
+ let sum1 = sum - digits[10] * StringAusFunc.weights[10];
762
+ let digit = StringAusFunc.suggestLookup[89 - sum1 % 89];
763
+ if (digit !== undefined) {
764
+ return {
765
+ code: 2,
766
+ message: 'Checksum1 is incorrect.'
767
+ };
768
+ }
769
+ else {
770
+ const sum2 = sum1 - digits[9] * StringAusFunc.weights[9];
771
+ for (let i = 0; i < 10; ++i) {
772
+ sum1 = sum2 + i * StringAusFunc.weights[9];
773
+ digit = StringAusFunc.suggestLookup[89 - sum1 % 89];
774
+ if (digit !== undefined) {
775
+ return {
776
+ code: 3,
777
+ message: 'Checksum2 is incorrect.'
778
+ };
779
+ }
780
+ }
781
+ }
782
+ return null;
783
+ }
784
+ static validateACN(acn) {
785
+ if (!acn) {
786
+ return null;
787
+ }
788
+ const digits = acn.replace(/[^\d]/g, '').split('').map(Number);
789
+ console.log(digits);
790
+ if (digits.length !== 9) {
791
+ return { code: 1, message: 'Expect 9-digit.' };
792
+ }
793
+ const sum = digits.slice(0, 8).reduce(StringAusFunc.addAcnWeighted, 0);
794
+ const lastDigit = 10 - sum % 10;
795
+ if (lastDigit === digits[8]) {
796
+ return null;
797
+ }
798
+ return {
799
+ code: 2,
800
+ message: 'Checksum is incorrect.'
801
+ };
802
+ }
803
+ }
804
+ //thanks to https://github.com/sidorares/australian-business-number/blob/0591475f5978fd122b472edcdc7efe6d96d56f26/index.js
805
+ StringAusFunc.acnWeights = [8, 7, 6, 5, 4, 3, 2, 1];
806
+ StringAusFunc.weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
807
+ StringAusFunc.suggestLookup = StringAusFunc.generateLookup();
808
+
809
+ class StringFunc {
810
+ /**
811
+ * Up to 2 letters. For John Smith, returns JS, for Huang, Zijian, returns ZH
812
+ * @param s
813
+ */
814
+ static getAbbr(s) {
815
+ if (!s) {
816
+ return '';
817
+ }
818
+ const sArray = s.split(/[\s,]+/);
819
+ const comma = s.indexOf(',') >= 0;
820
+ if (sArray.length === 1) {
821
+ return sArray[0][0];
822
+ }
823
+ return comma ? sArray[1][0] + sArray[0][0] : sArray[0][0] + sArray[1][0];
824
+ }
825
+ /**
826
+ * A substring with line breaks replaced by space.
827
+ * @param s
828
+ * @param length
829
+ * @returns result, or empty string if the input is empty, null or undefined
830
+ */
831
+ static getOneLineDigest(s, length) {
832
+ if (!s) {
833
+ return '';
834
+ }
835
+ const ss = s.substring(0, length);
836
+ const st = ss.replace(new RegExp('\n', 'g'), ' ') + ((s.length > length) ? '...' : '');
837
+ return st.trim();
838
+ }
839
+ /**
840
+ * Remove line breaks and econde with encodeURI() so the data could be saved in Azure as meta. If the string is truncated, the return will have ... suffix.
841
+ * @param s
842
+ * @param length
843
+ * @returns result, or empty string if the input is empty, null or undefined
844
+ */
845
+ static getOneLineDigestOfHtml(s, length) {
846
+ if (!s) {
847
+ return '';
848
+ }
849
+ const ss = s.substring(0, length);
850
+ const st = ss.replace(new RegExp('\n', 'g'), ' ') + ((s.length > length) ? '...' : '');
851
+ return encodeURI(st.trim()); //need to encode in order to save as meta in Azure.
852
+ }
853
+ /**
854
+ * Pad number with zero
855
+ * @param num
856
+ * @param size
857
+ * @returns
858
+ */
859
+ static pad(num, size) {
860
+ if (num == null) {
861
+ return '';
862
+ }
863
+ let s = num + '';
864
+ while (s.length < size) {
865
+ s = '0' + s;
866
+ }
867
+ return s;
868
+ }
869
+ /**
870
+ * get plain text of HTML content
871
+ * @param s
872
+ * @returns result. If input is empty, null, or undefined, return the same.
873
+ */
874
+ static getHtmlPlainText(s) {
875
+ if (!s) {
876
+ return null;
877
+ }
878
+ const parser = new DOMParser();
879
+ const html = parser.parseFromString(s, 'text/html');
880
+ return html.body.textContent;
881
+ }
882
+ /**
883
+ *
884
+ * @param s
885
+ * @returns result. If input is empty, null, or undefined, return the same.
886
+ */
887
+ static capitalizeWords(s) {
888
+ if (!s) {
889
+ return s;
890
+ }
891
+ return s.replace(/(?:^|\s)\S/g, (a) => a.toUpperCase());
892
+ }
893
+ static validateEmail(email) {
894
+ if (!email) {
895
+ return true;
896
+ }
897
+ const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
898
+ return re.test(email.toLowerCase());
899
+ }
900
+ }
901
+
902
+ // import { v5 as uuid } from 'uuid/v5 causes ERROR in src / app / _func / helperFunc.ts(1, 10): error TS2305: Module '"C:/VSProjects/ApsCloudTrunk/APS.WebPos.NGCli/NGSource/node_modules/@types/uuid/v5"' has no exported member 'v5'
903
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/28439
904
+ class UuidFunc {
905
+ /**
906
+ * 36 UUID string including 4 hyphens. MySql stores GUID as 36 bytes anyway rather than 16bytes.
907
+ */
908
+ static newUUID() {
909
+ // return uuid('medilink.com.au', 'apscloud');
910
+ return v4();
911
+ }
912
+ static newUUIDStartWith0() {
913
+ const s = v4();
914
+ return '0000' + s.slice(4);
915
+ }
916
+ }
917
+
918
+ /*
919
+ * Public API Surface of nmce-func
920
+ */
921
+
922
+ /**
923
+ * Generated bundle index. Do not edit.
924
+ */
925
+
926
+ export { AddressFunc, AuthenticationService, CurrencyFunc, DateFunc, HtmlPrintFunc, JavaScriptFunc, JsonFunc, StringAusFunc, StringFunc, UuidFunc };
927
+ //# sourceMappingURL=nmce-func.js.map