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