nmce-func 1.2.0 → 1.3.0

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