nmce-func 0.0.4 → 0.0.8

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