nmce-func 0.0.3 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/_func/addressFunc.d.ts +31 -0
  2. package/_func/authentication.service.d.ts +24 -0
  3. package/_func/currencyFunc.d.ts +20 -0
  4. package/_func/dateFunc.d.ts +121 -0
  5. package/_func/htmlPrintFunc.d.ts +18 -0
  6. package/_func/index.d.ts +10 -0
  7. package/_func/javascriptFunc.d.ts +9 -0
  8. package/_func/jsonFunc.d.ts +31 -0
  9. package/_func/stringAusFunc.d.ts +40 -0
  10. package/_func/stringFunc.d.ts +41 -0
  11. package/_func/uuidFunc.d.ts +7 -0
  12. package/bundles/nmce-func.umd.js +1294 -0
  13. package/bundles/nmce-func.umd.js.map +1 -0
  14. package/bundles/nmce-func.umd.min.js +2 -0
  15. package/bundles/nmce-func.umd.min.js.map +1 -0
  16. package/esm2015/_func/addressFunc.js +49 -0
  17. package/esm2015/_func/authentication.service.js +41 -0
  18. package/esm2015/_func/currencyFunc.js +72 -0
  19. package/esm2015/_func/dateFunc.js +316 -0
  20. package/esm2015/_func/htmlPrintFunc.js +45 -0
  21. package/esm2015/_func/index.js +11 -0
  22. package/esm2015/_func/javascriptFunc.js +17 -0
  23. package/esm2015/_func/jsonFunc.js +67 -0
  24. package/esm2015/_func/stringAusFunc.js +200 -0
  25. package/esm2015/_func/stringFunc.js +93 -0
  26. package/esm2015/_func/uuidFunc.js +17 -0
  27. package/esm2015/nmce-func.js +5 -0
  28. package/esm2015/public-api.js +5 -0
  29. package/fesm2015/nmce-func.js +927 -0
  30. package/fesm2015/nmce-func.js.map +1 -0
  31. package/nmce-func.d.ts +4 -0
  32. package/nmce-func.metadata.json +1 -0
  33. package/package.json +18 -4
  34. package/public-api.d.ts +1 -0
  35. package/karma.conf.js +0 -44
  36. package/ng-package.json +0 -11
  37. package/src/_func/addressFunc.spec.ts +0 -12
  38. package/src/_func/addressFunc.ts +0 -54
  39. package/src/_func/authentication.service.ts +0 -47
  40. package/src/_func/currencyFunc.ts +0 -71
  41. package/src/_func/dateFunc.spec.ts +0 -191
  42. package/src/_func/dateFunc.ts +0 -385
  43. package/src/_func/helperFunc.ts +0 -27
  44. package/src/_func/htmlFunc.ts +0 -46
  45. package/src/_func/index.ts +0 -9
  46. package/src/_func/jsFunc.ts +0 -48
  47. package/src/_func/stringFunc.spec.ts +0 -142
  48. package/src/_func/stringFunc.ts +0 -337
  49. package/src/public-api.ts +0 -5
  50. package/src/test.ts +0 -26
  51. package/tsconfig.lib.json +0 -25
  52. package/tsconfig.lib.prod.json +0 -11
  53. package/tsconfig.spec.json +0 -17
  54. package/tslint.json +0 -17
@@ -1,191 +0,0 @@
1
- import * as moment from 'moment';
2
- import { DateFunc } from './dateFunc';
3
-
4
-
5
- describe('DateFunc', () => {
6
- it('dateTimeUtcToLocalDateNumber', () => {
7
- const dtUtc = new Date('2018-01-23T22:00:00Z');
8
- expect(dtUtc.getDate()).toBe(24);
9
- expect(dtUtc.getUTCDate()).toBe(23);
10
- expect(dtUtc.getUTCHours()).toBe(22);
11
- expect(dtUtc.getHours()).toBe(8);
12
-
13
- const n = DateFunc.dateTimeUtcToLocalDateNumber(dtUtc);
14
- const dt = new Date(n);
15
- expect(dt.getDate()).toBe(24);
16
- expect(dt.getUTCDate()).toBe(23);
17
- expect(dt.getHours()).toBe(0);
18
- expect(dt.getUTCHours()).toBe(14);
19
- expect(dt.getMinutes()).toBe(0);
20
- expect(JSON.stringify(dt)).toBe('"2018-01-23T14:00:00.000Z"');
21
- });
22
-
23
- it('dateTimeUtcToLocalDate', () => {
24
- const dtUtc = new Date('2018-01-23T22:00:00Z');
25
- expect(dtUtc.getDate()).toBe(24);
26
- expect(dtUtc.getUTCDate()).toBe(23);
27
- expect(dtUtc.getUTCHours()).toBe(22);
28
- expect(dtUtc.getHours()).toBe(8);
29
-
30
- const localDt = DateFunc.dateTimeUtcToLocalDate(dtUtc);
31
- expect(localDt.getDate()).toBe(24);
32
- expect(localDt.getUTCDate()).toBe(23);
33
- expect(localDt.getUTCHours()).toBe(14);
34
- expect(localDt.getHours()).toBe(0);
35
- expect(JSON.stringify(localDt)).toBe('"2018-01-23T14:00:00.000Z"');
36
- });
37
-
38
- it('localISODateString', () => {
39
- const dtUtc = new Date('2018-01-23T22:00:00Z');
40
-
41
- const localDt = DateFunc.localISODateString(dtUtc);
42
- expect(localDt).toBe('2018-01-24');
43
- });
44
-
45
- it('localDateToUtc', () => {
46
- const dt = new Date('2018-02-23');
47
- console.debug('dt: ' + dt.toString());
48
- //It might be intepreted as UTC date, that will become 2010-02-23 10:00:00 in +10 timezone so is 2010-02-23T0:0:0Z
49
- const m = moment(dt);
50
- expect(m.isUTC()).toBeFalsy();
51
- expect(m.hours()).toBe(10);
52
- expect(dt.getDate()).toBe(23);
53
- expect(dt.getHours()).toBe(10); //somehow the Date function give it 10 hours in Brisbane
54
-
55
- const dtUtc = DateFunc.localDateToUtc('2018-02-23');
56
- expect(dtUtc).toBeDefined();
57
- console.debug('dtUtc: ' + dtUtc.toString());
58
- expect(dtUtc.getHours()).toBe(14);
59
- expect(dtUtc.getDate()).toBe(22);
60
- });
61
-
62
- it('mod to near 5', () => {
63
- function near5(mt: number) {
64
- return (mt % 5) ? (Math.floor(mt / 5) * 5 + 5) : mt;
65
- }
66
-
67
- expect(near5(40)).toBe(40);
68
- expect(near5(37)).toBe(40);
69
- });
70
-
71
- it('older than 33 hours with 33 hours', () => {
72
- const m = moment().add(-33, 'hour');
73
- expect(DateFunc.olderThanHours(m.toDate(), 33)).toBeTruthy();
74
- });
75
-
76
- it('older than 33 hours with 333 hours', () => {
77
- const m = moment().add(-333, 'hour');
78
- expect(DateFunc.olderThanHours(m.toDate(), 33)).toBeTruthy();
79
- });
80
-
81
- it('not older than 33 hours with 2 hours', () => {
82
- const m = moment().add(-2, 'hour');
83
- expect(DateFunc.olderThanHours(m.toDate(), 33)).toBeFalsy();
84
- });
85
-
86
- it('older than 1 day 24 hours', () => {
87
- const m = moment().add(-24, 'hour');
88
- expect(DateFunc.olderThan24Hours(m.toDate())).toBeTruthy();
89
- });
90
-
91
- it('older than 1 day with 333 hours', () => {
92
- const m = moment().add(-333, 'hour');
93
- expect(DateFunc.olderThan24Hours(m.toDate())).toBeTruthy();
94
- });
95
-
96
- it('not older than 1 day with 23 hours', () => {
97
- const m = moment().add(-23, 'hour');
98
- expect(DateFunc.olderThan24Hours(m.toDate())).toBeFalsy();
99
- });
100
-
101
- it('not older than 1 day with undefined', () => {
102
- expect(DateFunc.olderThan24Hours(undefined)).toBeFalsy();
103
- });
104
-
105
-
106
- //UTC counter parts
107
- it('older than 1 day 24 hours utc', () => {
108
- const m = moment.utc().add(-24, 'hour');
109
- expect(DateFunc.olderThan24HoursUtc(m.toDate())).toBeTruthy();
110
- });
111
-
112
- it('older than 1 day with 333 hours utc', () => {
113
- const m = moment.utc().add(-333, 'hour');
114
- expect(DateFunc.olderThan24HoursUtc(m.toDate())).toBeTruthy();
115
- });
116
-
117
- it('not older than 1 day with 23 hours utc', () => {
118
- const m = moment.utc().add(-23, 'hour');
119
- expect(DateFunc.olderThan24HoursUtc(m.toDate())).toBeFalsy();
120
- });
121
-
122
-
123
- it('older than 33 hours with 33 hours Utc', () => {
124
- const m = moment.utc().add(-33, 'hour');
125
- expect(DateFunc.olderThanHoursUtc(m.toDate(), 33)).toBeTruthy();
126
- });
127
-
128
- it('older than 33 hours with 333 hours Utc', () => {
129
- const m = moment.utc().add(-333, 'hour');
130
- expect(DateFunc.olderThanHoursUtc(m.toDate(), 33)).toBeTruthy();
131
- });
132
-
133
- it('not older than 33 hours with 2 hours Utc', () => {
134
- const m = moment.utc().add(-2, 'hour');
135
- expect(DateFunc.olderThanHoursUtc(m.toDate(), 33)).toBeFalsy();
136
- });
137
-
138
- it('olderThan1Day', () => {
139
- const hour = moment().hour();
140
- const yesterday = moment().add(-hour - 1, 'hours').toDate();
141
- const yesterdayUtc = DateFunc.localDateToUtc(yesterday);
142
- console.debug(`olderThan1Day: hour: ${hour}; yesterday: ${yesterday}, yesterDayUtc: ${yesterdayUtc}`);
143
- expect(DateFunc.olderThan1Day(yesterdayUtc)).toBeTruthy();
144
-
145
-
146
- });
147
-
148
- it('notOlderThan1DayEnough', () => {
149
- const hour = moment(Date.now()).hour();
150
- const yesterday = moment().add(-hour - 1, 'hours').toDate();
151
- const yesterdayUtc = DateFunc.localDateToUtc(yesterday);
152
- console.debug(`notOlderThan1DayEnough: hour: ${hour}; yesterday: ${yesterday}, yesterDayUtc: ${yesterdayUtc}`);
153
- expect(DateFunc.olderThan1Day(new Date(yesterdayUtc))).toBeTruthy();
154
-
155
-
156
- });
157
-
158
- it('getTimeFromMins', () => {
159
- const v = DateFunc.getHMFromMins(120);
160
- expect(v).toBe('02:00');
161
-
162
- const v2 = DateFunc.getHMFromMins(780);
163
- expect(v2).toBe('13:00');
164
- });
165
-
166
- it('getMinutesSinceMidnight', () => {
167
- const m = moment('20180102T130000');
168
- const d = m.toDate();
169
- console.debug('getMinutesSinceMidnight: ' + d.toString());
170
- const v = DateFunc.getMinutesSinceMidnight(d);
171
- expect(v).toBe(780);
172
- });
173
-
174
- it('firstDayOfWeek en-AU', () => {
175
- const d = moment.localeData('en-AU');
176
- expect(d.firstDayOfWeek()).toBe(0); // the lib previously return 1, but now 0. https://www.abc.net.au/news/2019-08-18/which-day-do-you-consider-the-start-of-the-week/11346348
177
- //https://thetylt.com/culture/sunday-first-or-last-day-of-the-week, apparently the balance of the debate inside the lib dev team had been changing.
178
- });
179
-
180
- it('firstDayOfWeek en-GB', () => {
181
- const d = moment.localeData('en-GB');
182
- expect(d.firstDayOfWeek()).toBe(1);
183
- });
184
-
185
- it('firstDayOfWeek en-US', () => {
186
- const d = moment.localeData('en-US');
187
- expect(d.firstDayOfWeek()).toBe(0);
188
- });
189
-
190
-
191
- });
@@ -1,385 +0,0 @@
1
- import moment from 'moment';
2
-
3
- export class DateFunc {
4
-
5
- /**
6
- * 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.
7
- * @param dtUtc
8
- * @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
9
- */
10
- static dateTimeUtcToLocalDateNumber(dtUtc: Date): number {
11
- if (!dtUtc) {
12
- return 0; //0 is better for calculation by the clients.
13
- }
14
-
15
- const localDt = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
16
- const localDNum = localDt.setHours(0, 0, 0, 0);
17
-
18
- return localDNum;
19
- }
20
-
21
- /**
22
- * Date only. However, the date may still be in UTC.
23
- * @param dtUtc
24
- */
25
- static dateTimeUtcToLocalDate(dtUtc: Date): Date {
26
- if (!dtUtc) {
27
- return dtUtc;
28
- }
29
-
30
- const localDt = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
31
- const localD = localDt.setHours(0, 0, 0, 0);
32
- return new Date(localD);
33
- }
34
-
35
- static localISODateString(dtUtc: Date): string {
36
- const dt = moment(dtUtc).local();
37
- return dt.format('YYYY-MM-DD');
38
- }
39
-
40
- /**
41
- * locate date ONLY (no time) to UTC date.
42
- * @param dt if dt contain time info, it will become dt.setHours(0, 0, 0, 0)
43
- */
44
- static localDateToUtc(d: Date | number | string): Date | undefined {
45
- if (!d) {
46
- return undefined;
47
- }
48
-
49
- const dt = moment(d).toDate();
50
- const n = dt.setHours(0, 0, 0, 0);
51
- const offset = dt.getTimezoneOffset() * 60000;
52
- return new Date(n + offset);
53
- }
54
-
55
- static getTimezoneOffset(): number {
56
- const dt = this.today;
57
- return dt.getTimezoneOffset();
58
- }
59
-
60
- /**
61
- * Transform UTC DateTime to local dateTime.
62
- * @param dtUtc
63
- * @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
64
- */
65
- static dateTimeUtcToLocalDateTime(dtUtc: Date): Date {
66
- if (!dtUtc) {
67
- return dtUtc;
68
- }
69
-
70
- const stillUtc = moment.utc(dtUtc).toDate();
71
- return moment(stillUtc).local().toDate();
72
- }
73
-
74
- static dateTimeUtcToLocaMoment(dtUtc: Date): moment.Moment {
75
- if (!dtUtc) {
76
- return dtUtc;
77
- }
78
-
79
- const stillUtc = moment.utc(dtUtc);
80
- return stillUtc.local();
81
- }
82
-
83
- static getEndOfWeek(dt: Date | number) {
84
- // return new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 6 - dt.getDay(), 23, 59, 59, 999);
85
- return moment(dt).endOf('isoWeek').toDate();
86
- }
87
-
88
- static getStartOfWeek(dt: Date | number) {
89
- return moment(dt).startOf('isoWeek').toDate();
90
- }
91
-
92
- static getEndOfMonth(dt: Date | number) {
93
- // return new Date(dt.getFullYear(), dt.getMonth() + 1, 0, 23, 59, 59, 999);
94
- return moment(dt).endOf('month').toDate();
95
- }
96
-
97
- static getStartOfMonth(dt: Date | number) {
98
- return moment(dt).startOf('month').toDate();
99
- }
100
-
101
- static getDaysBetweenDates(dt1: Date, dt2: Date) {
102
- //const timeDiff = Math.abs(dt2.getTime() - dt1.getTime());
103
- //return Math.trunc(timeDiff / (1000 * 3600 * 24));
104
- return this.getDaysBetween(dt1, dt2);
105
- }
106
-
107
- static getEndOfDate(dt: Date): Date {
108
- return new Date(dt.setHours(23, 59, 59, 999));
109
- }
110
-
111
- static getStartOfDate(dt: Date): Date {
112
- return moment(dt).startOf('day').toDate();
113
- }
114
-
115
- static getEndOfToday(): Date {
116
- // return new Date((new Date(Date.now())).setHours(23, 59, 59, 999));
117
- return moment(Date.now()).endOf('day').toDate();
118
-
119
- }
120
-
121
- static getStartOfToday(): Date {
122
- // return new Date((new Date(Date.now())).setHours(0, 0, 0, 0));
123
- return moment(Date.now()).startOf('day').toDate();
124
- }
125
-
126
- //inspired https://stackoverflow.com/questions/563406/add-days-to-javascript-date
127
- static addDays(dt: Date, days: number) {
128
- const dat = moment(dt);
129
- dat.add(days, 'days');
130
- return dat.toDate();
131
- }
132
-
133
- static subtractDays(dt: Date, days: number) {
134
- const dat = moment(dt);
135
- dat.subtract(days, 'days');
136
- return dat.toDate();
137
- }
138
-
139
- /**
140
- * Start of today
141
- */
142
- static get today(): Date {
143
- return this.getStartOfToday();
144
- }
145
-
146
- static get now(): Date {
147
- return new Date(Date.now());
148
- }
149
-
150
- static getNext5MinuteMark(): Date {
151
- const m = moment().set('second', 0).set('millisecond', 0);
152
- const minute = m.minute();
153
- const mod = minute % 5;
154
- if (mod) {
155
- const delta = 5 - mod;
156
- return m.add(delta, 'm').toDate();
157
- }
158
-
159
- return m.toDate();
160
- }
161
-
162
- static getYMD(d: Date) {
163
- return moment(d).format('YYYYMMDD');
164
- }
165
-
166
- static getDMYWithSlash(d: Date) {
167
- return moment(d).format('DD/MM/YYYY');
168
- }
169
-
170
- static getDMYHmWithSlash(d: Date) {
171
- return moment(d).format('DD/MM/YYYY HH:mm');
172
- }
173
-
174
- static getMcpTime(dt: Date) {
175
- return moment(dt).format('HH:mm:ss.SSSZ');
176
- }
177
-
178
- /**
179
- * In 24 hour format
180
- * @param dtUtc
181
- */
182
- static getLocalDMYHmWithSlash(dtUtc: Date) {
183
- const d = DateFunc.dateTimeUtcToLocalDateTime(dtUtc);
184
- return moment(d).format('DD/MM/YYYY HH:mm');
185
- }
186
-
187
- /**
188
- * Offset minutes comparing with today
189
- */
190
- static getOffsetMinutes(dtUtc: Date | number): number {
191
- const dm1 = moment(dtUtc);
192
- const dm2 = moment(new Date().setHours(0, 0, 0, 0));
193
- return dm1.diff(dm2, 'minutes');
194
- }
195
-
196
- static getDaysBetween(d1: Date | number, d2: Date | number): number {
197
- const dm1 = moment(d1);
198
- const dm2 = moment(d2);
199
- return dm2.diff(dm1, 'days');
200
- }
201
-
202
- /**
203
- * Get hour of the date. If Date is not defined, the hour will be current hour.
204
- * @param dtUtc
205
- */
206
- static getHour(dtUtc: Date | number): number {
207
- const m = moment(dtUtc);
208
- return m.hours();
209
- }
210
-
211
- static getMinute(dtUtc: Date | number): number {
212
- const m = moment(dtUtc);
213
- return m.minutes();
214
- }
215
-
216
- static composeDateTime(dt: Date, h: number, minute?: number): Date {
217
- if (!dt) {
218
- return dt;
219
- }
220
-
221
- const mt = moment(dt);
222
- return new Date(mt.toDate().setHours(h, minute, 0, 0));
223
- }
224
-
225
- static olderThan24Hours(d: Date): boolean {
226
- const m = moment(d);
227
- return moment().diff(m, 'hours') >= 24;
228
- }
229
-
230
- static olderThan24HoursUtc(dtUtc: Date): boolean {
231
- return DateFunc.getHourAgeUtc(dtUtc) >= 24;
232
- }
233
-
234
- static olderThanHours(d: Date, hours: number) {
235
- const m = moment(d);
236
- return moment().diff(m, 'hours') >= hours;
237
- }
238
-
239
- static olderThanHoursUtc(dtUtc: Date, hours: number): boolean {
240
- return DateFunc.getHourAgeUtc(dtUtc) >= hours;
241
- }
242
-
243
- static olderThanMinutes(d: Date, minutes: number) {
244
- const m = moment(d);
245
- return moment().diff(m, 'minutes') >= minutes;
246
- }
247
-
248
- /**
249
- * It could be 11PM yesterday, and 1 AM today. Actually based on local today.
250
- */
251
- static olderThan1Day(dtUtc: Date): boolean {
252
- return DateFunc.getDayAgeUtc(dtUtc) > 0;
253
- }
254
-
255
- static getHourAge(d: Date) {
256
- const m = moment(d);
257
- return moment().diff(m, 'hours');
258
- }
259
-
260
- static getHourAgeUtc(dtUtc: Date) {
261
- const m = moment.utc(dtUtc);
262
- return moment.utc().diff(m, 'hours');
263
- }
264
-
265
- /**
266
- * Compare utc date with utc now.
267
- * @param dtUtc
268
- */
269
- static getDayAgeUtc(dtUtc: Date) {
270
- const m = moment.utc(dtUtc);
271
- return moment.utc().diff(m, 'days');
272
- }
273
-
274
- static getAge(d: Date) {
275
- const m = moment(d);
276
- return moment().diff(m, 'years');
277
- }
278
-
279
- static getYear(d: Date) {
280
- const m = moment(d);
281
- return m.year();
282
- }
283
-
284
- static getUtcNow(): Date {
285
- return moment.utc().toDate();
286
- }
287
-
288
- static addMinutes(d: Date, m: number): Date {
289
- return moment(d).add(m, 'm').toDate();
290
- }
291
-
292
- static addMonth(d: Date, m: number): Date {
293
- return moment(d).add(m, 'M').toDate();
294
- }
295
-
296
- static getDuration(d1: Date, d2: Date) {
297
- const md1 = moment(d1);
298
- const md2 = moment(d2);
299
- return moment.duration(md2.diff(md1));
300
- }
301
-
302
- /**
303
- * Convert minutes from midnight to HH:mm text
304
- * @param mins
305
- */
306
- static getHMFromMins(mins: number): string {
307
- // do not include the first validation check if you want, for example,
308
- // getTimeFromMins(1530) to equal getTimeFromMins(90) (i.e. mins rollover)
309
- if (mins >= 24 * 60 || mins < 0) {
310
- throw new RangeError('Valid input should be greater than or equal to 0 and less than 1440.');
311
- }
312
- const h = mins / 60 | 0,
313
- m = mins % 60 | 0;
314
- return moment.utc().hours(h).minutes(m).format('HH:mm');
315
- }
316
-
317
- static getMinutesSinceMidnight(d: Date) {
318
- const m = moment(d);
319
- 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
320
- return m.diff(midnight, 'minutes');
321
- }
322
-
323
- static getMinutesBetween(start: Date, end: Date) {
324
- const m = moment(start);
325
- const m2 = moment(end);
326
- return m2.diff(m, 'minutes');
327
- }
328
-
329
- /**
330
- * Parse json string with date serialized into string, and get proper date object back
331
- * @param s
332
- */
333
- static dateSafeJsonParse(s: string) {
334
- return JSON.parse(s, this.dateReviver);
335
- }
336
-
337
- private static dateReviver(key: string, value: any) {
338
- if (DateFunc.isSerializedDate(value)) {
339
- return (new Date(value));
340
- }
341
-
342
- // If it's not a date-string, we want to return the value as-is. If we fail to return
343
- // a value, it will be omitted from the resultant data structure.
344
- return (value);
345
-
346
- }
347
-
348
-
349
- // I determine if the given value is a string that matches the serialized-date pattern.
350
- private static isSerializedDate(value: any): boolean {
351
- // Dates are serialized in TZ format, example: '1981-12-20T04:00:14.000Z'.
352
- const datePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
353
- return (DateFunc.isString(value) && datePattern.test(value));
354
- }
355
-
356
-
357
- // I determine if the given value is a String.
358
- private static isString(value: any): boolean {
359
- return ({}.toString.call(value) === '[object String]');
360
- }
361
-
362
- static dateSafeParse(s: string | Date) {
363
- const m = moment(s);
364
- return m.toDate();
365
- }
366
-
367
- static composeDateWithMinutes(d: Date, minute: number): Date {
368
- const m = moment(d);
369
- 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
370
- midnight.add(minute, 'minutes');
371
- return midnight.toDate();
372
- }
373
-
374
- /**
375
- * Safe compare since date data may be considered as string rather than date.
376
- * @param d1
377
- * @param d2
378
- */
379
- static compare(d1: Date, d2: Date): number {
380
- const dd1 = (new Date(d1)).valueOf();
381
- const dd2 = (new Date(d2)).valueOf();
382
- return dd1 - dd2;
383
- }
384
- }
385
-
@@ -1,27 +0,0 @@
1
- import { v4 as uuid } from 'uuid'; //todo: somehow the system could not recognize v5.
2
- // 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'
3
- // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/28439
4
-
5
- export class HelperFunc {
6
- static loadExternalScript(scriptUrl: string) {
7
- return new Promise((resolve, reject) => {
8
- const scriptElement = document.createElement('script');
9
- scriptElement.src = scriptUrl;
10
- scriptElement.onload = resolve;
11
- document.body.appendChild(scriptElement);
12
- });
13
- }
14
-
15
- /**
16
- * 36 UUID string including 4 hyphens. MySql stores GUID as 36 bytes anyway rather than 16bytes.
17
- */
18
- static newUUID(): string {
19
- // return uuid('medilink.com.au', 'apscloud');
20
- return uuid();
21
- }
22
-
23
- static newUUIDStartWith0() {
24
- const s = uuid();
25
- return '0000' + s.slice(4);
26
- }
27
- }
@@ -1,46 +0,0 @@
1
- export class HtmlFunc {
2
- /**
3
- * Print with buillt-in CSS for internal reports
4
- * @param htmlTags
5
- * @param baseRef
6
- */
7
- static printWithCSS(htmlTags: string, baseRef: string) {
8
- if (window) {
9
- const htmlToPrint = `<html><head>
10
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
11
- <link rel="stylesheet" type="text/css" href="${baseRef}scripts/print190826.css" media="screen,print"/>
12
- </head><body onload="window.print()">${htmlTags}</body></html>`;
13
-
14
- const popup = window.open('', '_blank', 'width=1024,height=768');
15
- popup?.document.open();
16
- popup?.document.write(htmlToPrint);
17
-
18
- popup?.document.close();
19
- }
20
- return true;
21
- }
22
-
23
- /**
24
- * Print for external documents.
25
- * @param htmlTags
26
- */
27
- static print(htmlTags: string) {
28
- if (window) {
29
- const htmlToPrint = `<html><head>
30
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
31
- </head><body onload="window.print()">${htmlTags}</body></html>`;
32
-
33
- const popup = window.open('', '_blank', 'width=1024,height=768');
34
- popup?.document.open();
35
- popup?.document.write(htmlToPrint);
36
-
37
- popup?.document.close();
38
- }
39
- return true;
40
- }
41
-
42
- static printImage(url: string) {
43
- const imageTags = `<img src="${url}" alt="Image from URL"/>`;
44
- HtmlFunc.print(imageTags);
45
- }
46
- }
@@ -1,9 +0,0 @@
1
- export * from './addressFunc';
2
- export * from './authentication.service';
3
- export * from './currencyFunc';
4
- export * from './dateFunc';
5
- export * from './helperFunc';
6
- export * from './htmlFunc';
7
- export * from './jsFunc';
8
- export * from './stringFunc';
9
-
@@ -1,48 +0,0 @@
1
- import { DateFunc} from './dateFunc';
2
-
3
- export class JSFunc {
4
- static groupBy<T>(array: Array<T>, propertyName: string) {
5
- return array.reduce(function(acc: any, obj: any) {
6
- const key = obj[propertyName];
7
- if (!acc[key]) {
8
- acc[key] = [];
9
- }
10
- acc[key].push(obj);
11
- return acc;
12
- }, {});
13
- }
14
-
15
- /**
16
- * Group by a date property. The key is always of string type and representing milliseconds. The client should convert the string to number.
17
- * Angular date pipe could actually consume such string without explicitly converting to number.
18
- * @param array
19
- * @param propertyName
20
- */
21
- static groupByDate<T>(array: Array<T>, propertyName: string) {
22
- return array.reduce(function(acc: any, obj: any) {
23
- const key = DateFunc.dateTimeUtcToLocalDateNumber(obj[propertyName]);
24
- if (!acc[key]) {
25
- acc[key] = [];
26
- }
27
- acc[key].push(obj);
28
- return acc;
29
- }, {});
30
- }
31
-
32
- static removeNullOrEmptyFields(obj: any) {
33
- for (const f in obj) {
34
- if (obj[f] === null || obj[f]==='') {
35
- delete obj[f];
36
- }
37
- }
38
- }
39
-
40
- static removeNullFields(obj: any) {
41
- for (const f in obj) {
42
- if (obj[f] === null) {
43
- delete obj[f];
44
- }
45
- }
46
- }
47
-
48
- }