@central-design-system/components 3.0.0-alpha.5 → 3.0.0-alpha.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 (59) hide show
  1. package/dist/cds.css +1 -1
  2. package/dist/cds.d.ts +51 -48
  3. package/dist/cds.es.js +3841 -3315
  4. package/dist/cds.umd.js +1 -1
  5. package/dist/components/CdsButton/CdsButton.vue.d.ts +6 -5
  6. package/dist/components/CdsCombobox/CdsCombobox.vue.d.ts +98 -80
  7. package/dist/components/CdsDropdown/CdsDropdown.vue.d.ts +134 -54
  8. package/dist/components/CdsGrid/composable/grid.d.ts +2 -1
  9. package/dist/components/CdsInput/CdsInput.vue.d.ts +14 -0
  10. package/dist/components/CdsMenu/CdsMenu.vue.d.ts +24 -18
  11. package/dist/components/CdsOverlay/CdsOverlay.vue.d.ts +9 -13
  12. package/dist/components/CdsOverlay/composable/locationStrategies.d.ts +3 -0
  13. package/dist/components/CdsPagination/CdsPagination.vue.d.ts +142 -0
  14. package/dist/components/CdsSelect/CdsSelect.vue.d.ts +40 -22
  15. package/dist/components/CdsTextArea/CdsTextArea.vue.d.ts +15 -3
  16. package/dist/components/CdsTextInput/CdsTextInput.vue.d.ts +64 -10
  17. package/dist/components/CdsTooltip/CdsTooltip.vue.d.ts +765 -0
  18. package/dist/components/index.d.ts +3 -1
  19. package/dist/composable/dimensions.d.ts +23 -5
  20. package/dist/composable/display.d.ts +56 -0
  21. package/dist/composable/index.d.ts +3 -0
  22. package/dist/composable/overlay.d.ts +1 -1
  23. package/dist/composable/refs.d.ts +6 -0
  24. package/dist/composable/resizeObserver.d.ts +7 -0
  25. package/dist/create.d.ts +21 -0
  26. package/dist/globals.d.ts +2 -0
  27. package/dist/utils/helpers.d.ts +2 -0
  28. package/package.json +1 -2
  29. package/src/scss/themes/index.ts +1 -1
  30. package/src/scss/utility/lists.scss +1 -0
  31. package/dist/types/CdsOptions.d.ts +0 -3
  32. package/src/utils/__test__/helper.test.ts +0 -59
  33. package/src/utils/__test__/mocks/helper.mock.ts +0 -362
  34. package/src/utils/__test__/mocks/propFactory.mock.ts +0 -88
  35. package/src/utils/__test__/propFactory.test.ts +0 -9
  36. package/src/utils/anchor.ts +0 -76
  37. package/src/utils/animation.ts +0 -62
  38. package/src/utils/box.ts +0 -39
  39. package/src/utils/cache.ts +0 -12
  40. package/src/utils/changeCase/__test__/lowerCase.test.ts +0 -12
  41. package/src/utils/changeCase/__test__/mocks/lowerCase.mock.ts +0 -80
  42. package/src/utils/changeCase/__test__/mocks/noCase.mock.ts +0 -131
  43. package/src/utils/changeCase/__test__/mocks/paramCase.mock.ts +0 -39
  44. package/src/utils/changeCase/__test__/mocks/pascalCase.mock.ts +0 -46
  45. package/src/utils/changeCase/__test__/noCase.test.ts +0 -8
  46. package/src/utils/changeCase/__test__/paramCase.test.ts +0 -8
  47. package/src/utils/changeCase/__test__/pascalCase.test.ts +0 -8
  48. package/src/utils/changeCase/lowerCase.ts +0 -56
  49. package/src/utils/changeCase/noCase.ts +0 -45
  50. package/src/utils/changeCase/paramCase.ts +0 -11
  51. package/src/utils/changeCase/pascalCase.ts +0 -24
  52. package/src/utils/date.ts +0 -1106
  53. package/src/utils/dom.ts +0 -24
  54. package/src/utils/getCurrentInstance.ts +0 -43
  55. package/src/utils/getScrollParent.ts +0 -29
  56. package/src/utils/globals.ts +0 -8
  57. package/src/utils/helpers.ts +0 -312
  58. package/src/utils/index.ts +0 -19
  59. package/src/utils/propsFactory.ts +0 -92
package/src/utils/date.ts DELETED
@@ -1,1106 +0,0 @@
1
- import { isDate, pad } from '@components/utils';
2
- import defaultLang from '@components/locale/ru';
3
-
4
- import type { ILocale } from '@components/locale';
5
-
6
- export interface IRawDate {
7
- year?: number;
8
- month?: number;
9
- day?: number;
10
- hour?: number;
11
- minute?: number;
12
- second?: number;
13
- millisecond?: number;
14
- timezoneOffset?: number;
15
- dateHash?: string;
16
- timeHash?: string;
17
- date?: number;
18
- changed?: boolean;
19
- }
20
-
21
- export type TDateLocale = ILocale['date'];
22
-
23
- const MILLISECONDS_IN_DAY: number = 86400000;
24
- const MILLISECONDS_IN_HOUR: number = 3600000;
25
- const MILLISECONDS_IN_MINUTE: number = 60000;
26
- const defaultMask: string = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
27
- const token: RegExp =
28
- /\[((?:[^\]\\]|\\]|\\)*)\]|d{1,4}|M{1,4}|m{1,2}|w{1,2}|Qo|Do|D{1,4}|YY(?:YY)?|H{1,2}|h{1,2}|s{1,2}|S{1,3}|Z{1,2}|a{1,2}|[AQExX]/g;
29
- const reverseToken: RegExp =
30
- /(\[[^\]]*\])|d{1,4}|M{1,4}|m{1,2}|w{1,2}|Qo|Do|D{1,4}|YY(?:YY)?|H{1,2}|h{1,2}|s{1,2}|S{1,3}|Z{1,2}|a{1,2}|[AQExX]|([.*+:?^,\s${}()|\\]+)/g;
31
- const regexStore: Record<string, any> = {};
32
-
33
- const formatter: Record<string, Function> = {
34
- // Year: 00, 01, ..., 99
35
- YY(date: Date, dateLocale: TDateLocale, forcedYear: number) {
36
- // workaround for < 1900 with new Date()
37
- const y = this.YYYY(date, dateLocale, forcedYear) % 100;
38
- return y >= 0 ? pad(y) : '-' + pad(Math.abs(y));
39
- },
40
-
41
- // Year: 1900, 1901, ..., 2099
42
- YYYY(date: Date, _dateLocale: TDateLocale, forcedYear: number) {
43
- // workaround for < 1900 with new Date()
44
- return forcedYear !== void 0 && forcedYear !== null ? forcedYear : date.getFullYear();
45
- },
46
-
47
- // Month: 1, 2, ..., 12
48
- M(date: Date) {
49
- return date.getMonth() + 1;
50
- },
51
-
52
- // Month: 01, 02, ..., 12
53
- MM(date: Date) {
54
- return pad(date.getMonth() + 1);
55
- },
56
-
57
- // Month Short Name: Jan, Feb, ...
58
- MMM(date: Date, dateLocale: TDateLocale) {
59
- return dateLocale.monthsShort[date.getMonth()];
60
- },
61
-
62
- // Month Name: January, February, ...
63
- MMMM(date: Date, dateLocale: TDateLocale) {
64
- return dateLocale.months[date.getMonth()];
65
- },
66
-
67
- // Quarter: 1, 2, 3, 4
68
- Q(date: Date) {
69
- return Math.ceil((date.getMonth() + 1) / 3);
70
- },
71
-
72
- // Quarter: 1st, 2nd, 3rd, 4th
73
- Qo(date: Date) {
74
- return getOrdinal(this.Q(date));
75
- },
76
-
77
- // Day of month: 1, 2, ..., 31
78
- D(date: Date) {
79
- return date.getDate();
80
- },
81
-
82
- // Day of month: 1st, 2nd, ..., 31st
83
- Do(date: Date) {
84
- return getOrdinal(date.getDate());
85
- },
86
-
87
- // Day of month: 01, 02, ..., 31
88
- DD(date: Date) {
89
- return pad(date.getDate());
90
- },
91
-
92
- // Day of year: 1, 2, ..., 366
93
- DDD(date: Date) {
94
- return getDayOfYear(date);
95
- },
96
-
97
- // Day of year: 001, 002, ..., 366
98
- DDDD(date: Date) {
99
- return pad(getDayOfYear(date) as number, 3);
100
- },
101
-
102
- // Day of week: 0, 1, ..., 6
103
- d(date: Date) {
104
- return date.getDay();
105
- },
106
-
107
- // Day of week: Su, Mo, ...
108
- dd(date: Date, dateLocale: TDateLocale) {
109
- return this.dddd(date, dateLocale).slice(0, 2);
110
- },
111
-
112
- // Day of week: Sun, Mon, ...
113
- ddd(date: Date, dateLocale: TDateLocale) {
114
- return dateLocale.daysShort[date.getDay()];
115
- },
116
-
117
- // Day of week: Sunday, Monday, ...
118
- dddd(date: Date, dateLocale: TDateLocale) {
119
- return dateLocale.days[date.getDay()];
120
- },
121
-
122
- // Day of ISO week: 1, 2, ..., 7
123
- E(date: Date) {
124
- return date.getDay() || 7;
125
- },
126
-
127
- // Week of Year: 1 2 ... 52 53
128
- w(date: Date) {
129
- return getWeekOfYear(date);
130
- },
131
-
132
- // Week of Year: 01 02 ... 52 53
133
- ww(date: Date) {
134
- return pad(getWeekOfYear(date));
135
- },
136
-
137
- // Hour: 0, 1, ... 23
138
- H(date: Date) {
139
- return date.getHours();
140
- },
141
-
142
- // Hour: 00, 01, ..., 23
143
- HH(date: Date) {
144
- return pad(date.getHours());
145
- },
146
-
147
- // Hour: 1, 2, ..., 12
148
- h(date: Date) {
149
- const hours = date.getHours();
150
- return hours === 0 ? 12 : hours > 12 ? hours % 12 : hours;
151
- },
152
-
153
- // Hour: 01, 02, ..., 12
154
- hh(date: Date) {
155
- return pad(this.h(date));
156
- },
157
-
158
- // Minute: 0, 1, ..., 59
159
- m(date: Date) {
160
- return date.getMinutes();
161
- },
162
-
163
- // Minute: 00, 01, ..., 59
164
- mm(date: Date) {
165
- return pad(date.getMinutes());
166
- },
167
-
168
- // Second: 0, 1, ..., 59
169
- s(date: Date) {
170
- return date.getSeconds();
171
- },
172
-
173
- // Second: 00, 01, ..., 59
174
- ss(date: Date) {
175
- return pad(date.getSeconds());
176
- },
177
-
178
- // 1/10 of second: 0, 1, ..., 9
179
- S(date: Date) {
180
- return Math.floor(date.getMilliseconds() / 100);
181
- },
182
-
183
- // 1/100 of second: 00, 01, ..., 99
184
- SS(date: Date) {
185
- return pad(Math.floor(date.getMilliseconds() / 10));
186
- },
187
-
188
- // Millisecond: 000, 001, ..., 999
189
- SSS(date: Date) {
190
- return pad(date.getMilliseconds(), 3);
191
- },
192
-
193
- // Meridiem: AM, PM
194
- A(date: Date) {
195
- return this.H(date) < 12 ? 'AM' : 'PM';
196
- },
197
-
198
- // Meridiem: am, pm
199
- a(date: Date) {
200
- return this.H(date) < 12 ? 'am' : 'pm';
201
- },
202
-
203
- // Meridiem: a.m., p.m.
204
- aa(date: Date) {
205
- return this.H(date) < 12 ? 'a.m.' : 'p.m.';
206
- },
207
-
208
- // Timezone: -01:00, +00:00, ... +12:00
209
- Z(date: Date, _dateLocale: TDateLocale, _forcedYear: number, forcedTimezoneOffset: number) {
210
- const tzOffset =
211
- forcedTimezoneOffset === void 0 || forcedTimezoneOffset === null
212
- ? date.getTimezoneOffset()
213
- : forcedTimezoneOffset;
214
-
215
- return formatTimezone(tzOffset, ':');
216
- },
217
-
218
- // Timezone: -0100, +0000, ... +1200
219
- ZZ(date: Date, _dateLocale: TDateLocale, _forcedYear: number, forcedTimezoneOffset: number) {
220
- const tzOffset =
221
- forcedTimezoneOffset === void 0 || forcedTimezoneOffset === null
222
- ? date.getTimezoneOffset()
223
- : forcedTimezoneOffset;
224
-
225
- return formatTimezone(tzOffset);
226
- },
227
-
228
- // Seconds timestamp: 512969520
229
- X(date: Date) {
230
- return Math.floor(date.getTime() / 1000);
231
- },
232
-
233
- // Milliseconds timestamp: 512969520900
234
- x(date: Date) {
235
- return date.getTime();
236
- }
237
- };
238
-
239
- /**
240
- * Format date by mask
241
- * @param value
242
- * @param mask
243
- * @param dateLocale
244
- * @param __forcedYear
245
- * @param __forcedTimezoneOffset
246
- */
247
- export function formatDate(
248
- value: any,
249
- mask: string,
250
- dateLocale: TDateLocale,
251
- __forcedYear: number,
252
- __forcedTimezoneOffset: number
253
- ): string | undefined {
254
- if ((value !== 0 && !value) || value === Infinity || value === -Infinity) {
255
- return;
256
- }
257
-
258
- const date = new Date(value) as unknown as number;
259
-
260
- if (isNaN(date)) {
261
- return;
262
- }
263
-
264
- if (mask === void 0) {
265
- mask = defaultMask;
266
- }
267
-
268
- const locale = getDateLocale(dateLocale, defaultLang);
269
-
270
- return mask.replace(token, (match, text) =>
271
- match in formatter
272
- ? formatter[match](date, locale, __forcedYear, __forcedTimezoneOffset)
273
- : text === void 0
274
- ? match
275
- : text.split('\\]').join(']')
276
- );
277
- }
278
-
279
- /**
280
- * Set a specific year, month and day to a date.
281
- * @param date
282
- * @param rawModified
283
- * @param utc
284
- */
285
- export function adjustDate(date: Date, rawModified: IRawDate, utc: boolean) {
286
- const modified = normalizeModified(rawModified);
287
- const middle = utc ? 'UTC' : '';
288
- const _date = new Date(date);
289
- const adjustedDate =
290
- modified.year || modified.month || modified.date ? applyYearMonthDay(_date, modified, middle) : _date;
291
-
292
- for (const key in modified) {
293
- const op = key.charAt(0).toUpperCase() + key.slice(1);
294
- (adjustedDate as Date & Record<string, any>)[`set${middle}${op}`](modified[key]);
295
- }
296
-
297
- return adjustedDate;
298
- }
299
-
300
- /**
301
- * Parse any string into a date object based on the format passed.
302
- * @param str
303
- * @param mask
304
- * @param dateLocale
305
- */
306
- export function extractDate(str: string, mask: string, dateLocale: TDateLocale) {
307
- const sDate = splitDate(str, mask, dateLocale);
308
-
309
- const date = new Date(
310
- sDate.year as number,
311
- (sDate.month ? sDate.month - 1 : null) as number,
312
- sDate.day ? sDate.day : 1,
313
- sDate.hour,
314
- sDate.minute,
315
- sDate.second,
316
- sDate.millisecond
317
- );
318
-
319
- const tzOffset = date.getTimezoneOffset();
320
-
321
- return !sDate.timezoneOffset || sDate.timezoneOffset === tzOffset
322
- ? date
323
- : getChange(date, { minutes: sDate.timezoneOffset - tzOffset }, 1);
324
- }
325
-
326
- /**
327
- * Split date by mask.
328
- * @param str
329
- * @param mask
330
- * @param dateLocale
331
- * @param defaultModel
332
- */
333
- export function splitDate(
334
- str?: string | any,
335
- mask?: string,
336
- dateLocale?: TDateLocale,
337
- defaultModel?: IRawDate
338
- ): IRawDate {
339
- const date: IRawDate | Record<string, number | string | null> = {
340
- year: null,
341
- month: null,
342
- day: null,
343
- hour: null,
344
- minute: null,
345
- second: null,
346
- millisecond: null,
347
- timezoneOffset: null,
348
- dateHash: null,
349
- timeHash: null
350
- };
351
-
352
- defaultModel !== undefined && Object.assign(date, defaultModel);
353
-
354
- if (str === undefined || str === null || str === '' || typeof str !== 'string') return date;
355
-
356
- if (!mask) mask = defaultMask;
357
-
358
- const langOpts = getDateLocale(dateLocale, defaultLang);
359
- const months = langOpts.months;
360
- const monthsShort = langOpts.monthsShort;
361
-
362
- const { regex, map } = getRegexData(mask, langOpts);
363
- const match = str.match(regex);
364
-
365
- if (match === null) return date;
366
-
367
- let tzString = '';
368
-
369
- if (map.X || map.x) {
370
- const stamp = parseInt(match[map.X ? map.X : map.x], 10);
371
-
372
- if (isNaN(stamp) || stamp < 0) return date;
373
-
374
- const _date = new Date(stamp * (map.X ? 1000 : 0));
375
-
376
- date.year = _date.getFullYear();
377
- date.month = _date.getMonth() + 1;
378
- date.date = _date.getDate();
379
- date.hour = _date.getHours();
380
- date.minute = _date.getMinutes();
381
- date.second = _date.getSeconds();
382
- date.millisecond = _date.getMilliseconds();
383
- } else {
384
- if (map.YYYY) {
385
- date.year = parseInt(match[map.YYYY], 10);
386
- } else if (map.YY) {
387
- const year = parseInt(match[map.YY], 10);
388
- date.year = year < 0 ? year : 2000 + year;
389
- }
390
-
391
- if (map.M) {
392
- date.month = parseInt(match[map.M], 10);
393
- if (date.month < 1 || date.month > 12) return date;
394
- } else if (map.MMM) {
395
- date.month = monthsShort.indexOf(match[map.MMM]) + 1;
396
- } else if (map.MMMM) {
397
- date.month = months.indexOf(match[map.MMMM]) + 1;
398
- }
399
-
400
- if (map.D) {
401
- date.day = parseInt(match[map.D], 10);
402
- if (date.year === null || date.month === null || date.day < 1) return date;
403
-
404
- const maxDay = new Date(date.year as number, date.month as number, 0).getDate();
405
- if (date.day > maxDay) return date;
406
- }
407
-
408
- if (map.H) {
409
- date.hour = parseInt(match[map.H], 10) % 24;
410
- } else if (map.h) {
411
- date.hour = parseInt(match[map.h], 10) % 12;
412
-
413
- if (
414
- (map.A && match[map.A] === 'PM') ||
415
- (map.a && match[map.a] === 'pm') ||
416
- (map.aa && match[map.aa] === 'p.m.')
417
- ) {
418
- date.hour += 12;
419
- }
420
-
421
- date.hour = date.hour % 24;
422
- }
423
-
424
- if (map.m) {
425
- date.minute = parseInt(match[map.m], 10) % 60;
426
- }
427
-
428
- if (map.s) {
429
- date.second = parseInt(match[map.s], 10) % 60;
430
- }
431
-
432
- if (map.S) {
433
- date.millisecond = parseInt(match[map.S], 10) * 10 ** (3 - match[map.S].length);
434
- }
435
-
436
- if (map.Z || map.ZZ) {
437
- tzString = map.Z ? match[map.Z].replace(':', '') : match[map.ZZ];
438
- date.timezoneOffset = (tzString[0] === '+' ? -1 : 1) * (60 * +tzString.slice(1, 3) + +tzString.slice(3, 5));
439
- }
440
- }
441
-
442
- date.dateHash = pad(date.year as number, 6) + '/' + pad(date.month as number) + '/' + pad(date.day as number);
443
- date.timeHash =
444
- pad(date.hour as number) + ':' + pad(date.minute as number) + ':' + pad(date.second as number) + tzString;
445
-
446
- return date;
447
- }
448
-
449
- /**
450
- * Check a date string for validity.
451
- * @param date
452
- */
453
- export function isValid(date: any): boolean {
454
- return typeof date === 'number' ? true : !isNaN(Date.parse(date));
455
- }
456
-
457
- /**
458
- * The following method is just a wrapper to help you in cases where you just need current time but with a different year, or month, or second etc.
459
- * @param modified
460
- * @param utc
461
- */
462
- export function buildDate(modified: IRawDate, utc: boolean) {
463
- return adjustDate(new Date(), modified, utc);
464
- }
465
-
466
- /**
467
- * Get the day number in week for a given date.
468
- * @param date
469
- */
470
- export function getDayOfWeek(date: Date): number {
471
- const dow = new Date(date).getDay();
472
- return dow === 0 ? 7 : dow;
473
- }
474
-
475
- /**
476
- * Get the week number in year for a given date.
477
- * @param date
478
- */
479
- export function getWeekOfYear(date: Date): number {
480
- const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
481
- thursday.setDate(thursday.getDate() - ((thursday.getDay() + 6) % 7) + 3);
482
-
483
- const firstThursday = new Date(thursday.getFullYear(), 0, 4);
484
- firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3);
485
-
486
- const ds = thursday.getTimezoneOffset() - firstThursday.getTimezoneOffset();
487
- thursday.setHours(thursday.getHours() - ds);
488
-
489
- const weekDiff = (+thursday - +firstThursday) / (MILLISECONDS_IN_DAY * 7);
490
- return 1 + Math.floor(weekDiff);
491
- }
492
-
493
- /**
494
- * Get the day of the year from a given date.
495
- * @param date
496
- */
497
- export function getDayOfYear(date: Date): number | undefined {
498
- const dayOfYear = getDateDiff(date, startOfDate(date, 'year'), 'days');
499
- return dayOfYear ? dayOfYear + 1 : undefined;
500
- }
501
-
502
- /**
503
- * Check a date is in a given date/time range.
504
- * @param date
505
- * @param from
506
- * @param to
507
- * @param opts
508
- */
509
- export function isBetweenDates(
510
- date: Date,
511
- from: Date,
512
- to: Date,
513
- opts: { onlyDate: boolean; inclusiveFrom: boolean; inclusiveTo: boolean }
514
- ): boolean {
515
- const date1 = getDateIdentifier(from, opts.onlyDate);
516
- const date2 = getDateIdentifier(to, opts.onlyDate);
517
- const current = getDateIdentifier(date, opts.onlyDate);
518
-
519
- return (
520
- (current > date1 || (opts.inclusiveFrom && current === date1)) &&
521
- (current < date2 || (opts.inclusiveTo && current === date2))
522
- );
523
- }
524
-
525
- /**
526
- * Add a given date/time unit from a given date.
527
- * @param date
528
- * @param modified
529
- */
530
- export function addToDate(date: Date, modified: IRawDate): Date {
531
- return getChange(date, modified, 1);
532
- }
533
-
534
- /**
535
- * Subtract a given date/time unit from a given date.
536
- * @param date
537
- * @param modified
538
- */
539
- export function subtractFromDate(date: Date, modified: IRawDate): Date {
540
- return getChange(date, modified, -1);
541
- }
542
-
543
- /**
544
- * Beginning of a given date/time unit for a given date.
545
- * @param date
546
- * @param unit
547
- * @param utc
548
- */
549
- export function startOfDate(date: Date, unit: string, utc?: boolean): Date {
550
- const _date = new Date(date);
551
- const prefix = `set${utc ? 'UTC' : ''}`;
552
-
553
- switch (unit) {
554
- case 'year':
555
- case 'years':
556
- (_date as Date & Record<string, any>)[`${prefix}Month`](0);
557
- // eslint-disable-next-line no-fallthrough
558
- case 'month':
559
- case 'months':
560
- (_date as Date & Record<string, any>)[`${prefix}Date`](1);
561
- // eslint-disable-next-line no-fallthrough
562
- case 'day':
563
- case 'days':
564
- case 'date':
565
- (_date as Date & Record<string, any>)[`${prefix}Hours`](0);
566
- // eslint-disable-next-line no-fallthrough
567
- case 'hour':
568
- case 'hours':
569
- (_date as Date & Record<string, any>)[`${prefix}Minutes`](0);
570
- // eslint-disable-next-line no-fallthrough
571
- case 'minute':
572
- case 'minutes':
573
- (_date as Date & Record<string, any>)[`${prefix}Seconds`](0);
574
- // eslint-disable-next-line no-fallthrough
575
- case 'second':
576
- case 'seconds':
577
- (_date as Date & Record<string, any>)[`${prefix}Milliseconds`](0);
578
- }
579
-
580
- return _date;
581
- }
582
-
583
- /**
584
- * End of a given date/time unit for a given date.
585
- * @param date
586
- * @param unit
587
- * @param utc
588
- */
589
- export function endOfDate(date: Date, unit: string, utc: boolean): Date {
590
- const _date = new Date(date),
591
- prefix = `set${utc ? 'UTC' : ''}`;
592
-
593
- switch (unit) {
594
- case 'year':
595
- case 'years':
596
- (_date as Date & Record<string, any>)[`${prefix}Month`](11);
597
- // eslint-disable-next-line no-fallthrough
598
- case 'month':
599
- case 'months':
600
- (_date as Date & Record<string, any>)[`${prefix}Date`](daysInMonth(_date));
601
- // eslint-disable-next-line no-fallthrough
602
- case 'day':
603
- case 'days':
604
- case 'date':
605
- (_date as Date & Record<string, any>)[`${prefix}Hours`](23);
606
- // eslint-disable-next-line no-fallthrough
607
- case 'hour':
608
- case 'hours':
609
- (_date as Date & Record<string, any>)[`${prefix}Minutes`](59);
610
- // eslint-disable-next-line no-fallthrough
611
- case 'minute':
612
- case 'minutes':
613
- (_date as Date & Record<string, any>)[`${prefix}Seconds`](59);
614
- // eslint-disable-next-line no-fallthrough
615
- case 'second':
616
- case 'seconds':
617
- (_date as Date & Record<string, any>)[`${prefix}Milliseconds`](999);
618
- }
619
- return _date;
620
- }
621
-
622
- /**
623
- * Get the maximum date from a given date and other dates.
624
- * @param date
625
- * @param args
626
- */
627
- export function getMaxDate(date: Date, ...args: Date[]): Date {
628
- let _date = new Date(date) as unknown as number;
629
- Array.prototype.slice.call(args, 1).forEach((date) => {
630
- _date = Math.max(_date, new Date(date) as unknown as number);
631
- });
632
- return _date as unknown as Date;
633
- }
634
-
635
- /**
636
- * Get the minimum date from a given date and other dates.
637
- * @param date
638
- * @param args
639
- */
640
- export function getMinDate(date: Date, ...args: Date[]): Date {
641
- let _date = new Date(date) as unknown as number;
642
- Array.prototype.slice.call(args, 1).forEach((date) => {
643
- _date = Math.min(_date, new Date(date) as unknown as number);
644
- });
645
- return _date as unknown as Date;
646
- }
647
-
648
- /**
649
- * Get the difference between two dates.
650
- * @param date
651
- * @param subtract
652
- * @param unit
653
- */
654
- export function getDateDiff(date: Date, subtract: Date, unit = 'days'): number {
655
- const date1 = new Date(date);
656
- const date2 = new Date(subtract);
657
-
658
- switch (unit) {
659
- case 'years':
660
- case 'year':
661
- return date1.getFullYear() - date2.getFullYear();
662
-
663
- case 'months':
664
- case 'month':
665
- return (date1.getFullYear() - date2.getFullYear()) * 12 + date1.getMonth() - date2.getMonth();
666
-
667
- case 'days':
668
- case 'day':
669
- case 'date':
670
- return getDiff(startOfDate(date1, 'day'), startOfDate(date2, 'day'), MILLISECONDS_IN_DAY);
671
-
672
- case 'hours':
673
- case 'hour':
674
- return getDiff(startOfDate(date1, 'hour'), startOfDate(date2, 'hour'), MILLISECONDS_IN_HOUR);
675
-
676
- case 'minutes':
677
- case 'minute':
678
- return getDiff(startOfDate(date1, 'minute'), startOfDate(date2, 'minute'), MILLISECONDS_IN_MINUTE);
679
-
680
- case 'seconds':
681
- case 'second':
682
- return getDiff(startOfDate(date1, 'second'), startOfDate(date2, 'second'), 1000);
683
-
684
- default:
685
- return 0;
686
- }
687
- }
688
-
689
- /**
690
- * Get date format
691
- * @param date
692
- */
693
- export function inferDateFormat(date: Date | any): 'date' | 'number' | 'string' {
694
- return isDate(date) ? 'date' : typeof date === 'number' ? 'number' : 'string';
695
- }
696
-
697
- /**
698
- * Normalize a date in a given date/time range.
699
- * @param date
700
- * @param min
701
- * @param max
702
- */
703
- export function getDateBetween(date: Date, min: Date, max: Date): Date {
704
- const t = new Date(date);
705
-
706
- if (min) {
707
- const low = new Date(min);
708
- if (t < low) {
709
- return low;
710
- }
711
- }
712
-
713
- if (max) {
714
- const high = new Date(max);
715
- if (t > high) {
716
- return high;
717
- }
718
- }
719
-
720
- return t;
721
- }
722
-
723
- /**
724
- * Check of two dates unit are equal.
725
- * @param date1
726
- * @param date2
727
- * @param unit
728
- */
729
- export function isSameDate(date1: Date, date2: Date, unit: string): boolean {
730
- const _date1 = new Date(date1);
731
- const _date2 = new Date(date2);
732
-
733
- if (unit === void 0) {
734
- return _date1.getTime() === _date2.getTime();
735
- }
736
-
737
- switch (unit) {
738
- case 'second':
739
- case 'seconds':
740
- if (_date1.getSeconds() !== _date2.getSeconds()) {
741
- return false;
742
- }
743
- // eslint-disable-next-line no-fallthrough
744
- case 'minute':
745
- case 'minutes':
746
- if (_date1.getMinutes() !== _date2.getMinutes()) {
747
- return false;
748
- }
749
- // eslint-disable-next-line no-fallthrough
750
- case 'hour':
751
- case 'hours':
752
- if (_date1.getHours() !== _date2.getHours()) {
753
- return false;
754
- }
755
- // eslint-disable-next-line no-fallthrough
756
- case 'day':
757
- case 'days':
758
- case 'date':
759
- if (_date1.getDate() !== _date2.getDate()) {
760
- return false;
761
- }
762
- // eslint-disable-next-line no-fallthrough
763
- case 'month':
764
- case 'months':
765
- if (_date1.getMonth() !== _date2.getMonth()) {
766
- return false;
767
- }
768
- // eslint-disable-next-line no-fallthrough
769
- case 'year':
770
- case 'years':
771
- if (_date1.getFullYear() !== _date2.getFullYear()) {
772
- return false;
773
- }
774
- break;
775
- default:
776
- throw new Error(`date isSameDate unknown unit ${unit}`);
777
- }
778
-
779
- return true;
780
- }
781
-
782
- /**
783
- * Get the number of days in a month.
784
- * @param date
785
- */
786
- export function daysInMonth(date: Date): number {
787
- return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
788
- }
789
-
790
- /**
791
- * Cloning date object.
792
- * @param date
793
- */
794
- export function clone(date: Date): Date {
795
- return isDate(date) ? new Date(date.getTime()) : date;
796
- }
797
-
798
- function getRegexData(mask: string, dateLocale: TDateLocale): { map: Record<string, any>; regex: RegExp } {
799
- const days = '(' + dateLocale.days.join('|') + ')';
800
- const key = mask + days;
801
-
802
- if (regexStore[key]) return regexStore[key];
803
-
804
- const daysShort = '(' + dateLocale.daysShort.join('|') + ')';
805
- const months = '(' + dateLocale.months.join('|') + ')';
806
- const monthsShort = '(' + dateLocale.monthsShort.join('|') + ')';
807
-
808
- const map: Record<string, any> = {};
809
- let index = 0;
810
-
811
- const regexText = mask.replace(reverseToken, (match) => {
812
- index++;
813
- switch (match) {
814
- case 'YY':
815
- map.YY = index;
816
- return '(-?\\d{1,2})';
817
- case 'YYYY':
818
- map.YYYY = index;
819
- return '(-?\\d{1,4})';
820
- case 'M':
821
- map.M = index;
822
- return '(\\d{1,2})';
823
- case 'MM':
824
- map.M = index; // bumping to M
825
- return '(\\d{2})';
826
- case 'MMM':
827
- map.MMM = index;
828
- return monthsShort;
829
- case 'MMMM':
830
- map.MMMM = index;
831
- return months;
832
- case 'D':
833
- map.D = index;
834
- return '(\\d{1,2})';
835
- case 'Do':
836
- map.D = index++; // bumping to D
837
- return '(\\d{1,2}(st|nd|rd|th))';
838
- case 'DD':
839
- map.D = index; // bumping to D
840
- return '(\\d{2})';
841
- case 'H':
842
- map.H = index;
843
- return '(\\d{1,2})';
844
- case 'HH':
845
- map.H = index; // bumping to H
846
- return '(\\d{2})';
847
- case 'h':
848
- map.h = index;
849
- return '(\\d{1,2})';
850
- case 'hh':
851
- map.h = index; // bumping to h
852
- return '(\\d{2})';
853
- case 'm':
854
- map.m = index;
855
- return '(\\d{1,2})';
856
- case 'mm':
857
- map.m = index; // bumping to m
858
- return '(\\d{2})';
859
- case 's':
860
- map.s = index;
861
- return '(\\d{1,2})';
862
- case 'ss':
863
- map.s = index; // bumping to s
864
- return '(\\d{2})';
865
- case 'S':
866
- map.S = index;
867
- return '(\\d{1})';
868
- case 'SS':
869
- map.S = index; // bump to S
870
- return '(\\d{2})';
871
- case 'SSS':
872
- map.S = index; // bump to S
873
- return '(\\d{3})';
874
- case 'A':
875
- map.A = index;
876
- return '(AM|PM)';
877
- case 'a':
878
- map.a = index;
879
- return '(am|pm)';
880
- case 'aa':
881
- map.aa = index;
882
- return '(a\\.m\\.|p\\.m\\.)';
883
-
884
- case 'ddd':
885
- return daysShort;
886
- case 'dddd':
887
- return days;
888
- case 'Q':
889
- case 'd':
890
- case 'E':
891
- return '(\\d{1})';
892
- case 'Qo':
893
- return '(1st|2nd|3rd|4th)';
894
- case 'DDD':
895
- case 'DDDD':
896
- return '(\\d{1,3})';
897
- case 'w':
898
- return '(\\d{1,2})';
899
- case 'ww':
900
- return '(\\d{2})';
901
-
902
- case 'Z': // to split: (?:(Z)()()|([+-])?(\\d{2}):?(\\d{2}))
903
- map.Z = index;
904
- return '(Z|[+-]\\d{2}:\\d{2})';
905
- case 'ZZ':
906
- map.ZZ = index;
907
- return '(Z|[+-]\\d{2}\\d{2})';
908
-
909
- case 'X':
910
- map.X = index;
911
- return '(-?\\d+)';
912
- case 'x':
913
- map.x = index;
914
- return '(-?\\d{4,})';
915
-
916
- default:
917
- index--;
918
- if (match[0] === '[') {
919
- match = match.substring(1, match.length - 1);
920
- }
921
- return match.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
922
- }
923
- });
924
-
925
- const result = { map, regex: new RegExp('^' + regexText) };
926
- regexStore[key] = result;
927
-
928
- return result;
929
- }
930
-
931
- function getDateLocale(paramDateLocale?: TDateLocale, locale?: ILocale): TDateLocale {
932
- return paramDateLocale ? paramDateLocale : locale ? locale.date : defaultLang.date;
933
- }
934
-
935
- function formatTimezone(offset: number, delimiter: string = ''): string {
936
- const sign = offset > 0 ? '-' : '+';
937
- const absOffset = Math.abs(offset);
938
- const hours = Math.floor(absOffset / 60);
939
- const minutes = absOffset % 60;
940
-
941
- return sign + pad(hours) + delimiter + pad(minutes);
942
- }
943
-
944
- function applyYearMonthDayChange(date: Date, modified: IRawDate, sign: number): Date {
945
- let year = date.getFullYear();
946
- let month = date.getMonth();
947
- const day = date.getMonth();
948
-
949
- if (modified.year) {
950
- year += sign * modified.year;
951
- delete modified.year;
952
- }
953
-
954
- if (modified.month) {
955
- month += sign * modified.month;
956
- delete modified.month;
957
- }
958
-
959
- date.setDate(1);
960
- date.setMonth(2);
961
- date.setFullYear(year);
962
- date.setMonth(month);
963
- date.setDate(Math.min(day, daysInMonth(date)));
964
-
965
- if (modified.date) {
966
- date.setDate(date.getDate() + sign * modified.date);
967
- delete modified.date;
968
- }
969
-
970
- return date;
971
- }
972
-
973
- function applyYearMonthDay(date: Date, modified: IRawDate, middle: string): Date {
974
- const year = modified.year ? modified.year : (date as any)[`get${middle}FullYear`]();
975
- const month = modified.month ? modified.month - 1 : (date as any)[`get${middle}Month`]();
976
- const maxDay = new Date(year, month + 1, 0).getDate();
977
- const day = Math.min(maxDay, modified.date ? modified.date : (date as any)[`get${middle}Date`]());
978
-
979
- (date as Date & Record<string, any>)[`set${middle}Date`](1);
980
- (date as Date & Record<string, any>)[`set${middle}Month`](2);
981
- (date as Date & Record<string, any>)[`set${middle}FullYear`](year);
982
- (date as Date & Record<string, any>)[`set${middle}Month`](month);
983
- (date as Date & Record<string, any>)[`set${middle}Date`](day);
984
-
985
- delete modified.year;
986
- delete modified.month;
987
- delete modified.date;
988
-
989
- return date;
990
- }
991
-
992
- function getChange(date: Date, rawModified: IRawDate & Record<string, any>, sign: number): Date {
993
- const modified = normalizeModified(rawModified);
994
- const _date = new Date(date);
995
- const changedDate =
996
- modified.year || modified.month || modified.date ? applyYearMonthDayChange(date, modified, sign) : _date;
997
-
998
- for (const key in modified) {
999
- const op = key.charAt(0).toUpperCase() + key.slice(1);
1000
- (changedDate as Date & Record<string, any>)[`set${op}`]((changedDate as Date & Record<string, any>)[`get${op}`])() +
1001
- sign * modified[key];
1002
- }
1003
-
1004
- return changedDate;
1005
- }
1006
-
1007
- function normalizeModified(modified: IRawDate & Record<string, any>) {
1008
- const acc = { ...modified };
1009
-
1010
- if (modified.years) {
1011
- acc.year = modified.years;
1012
- delete acc.years;
1013
- }
1014
-
1015
- if (modified.months) {
1016
- acc.month = modified.months;
1017
- delete acc.months;
1018
- }
1019
-
1020
- if (modified.days) {
1021
- acc.date = modified.days;
1022
- delete acc.days;
1023
- }
1024
- if (modified.day) {
1025
- acc.date = modified.day;
1026
- delete acc.day;
1027
- }
1028
-
1029
- if (modified.hour) {
1030
- acc.hours = modified.hour;
1031
- delete acc.hour;
1032
- }
1033
-
1034
- if (modified.minute) {
1035
- acc.minutes = modified.minute;
1036
- delete acc.minute;
1037
- }
1038
-
1039
- if (modified.second) {
1040
- acc.seconds = modified.second;
1041
- delete acc.second;
1042
- }
1043
-
1044
- if (modified.millisecond) {
1045
- acc.milliseconds = modified.millisecond;
1046
- delete acc.millisecond;
1047
- }
1048
-
1049
- return acc;
1050
- }
1051
-
1052
- function getDayIdentifier(date: Date): number {
1053
- return date.getFullYear() * 10000 + date.getMonth() * 100 + date.getDate();
1054
- }
1055
-
1056
- function getDateIdentifier(date: Date, onlyDate?: boolean): number {
1057
- const _date = new Date(date);
1058
- return onlyDate ? getDayIdentifier(_date) : _date.getTime();
1059
- }
1060
-
1061
- function getDiff(date1: Date, date2: Date, interval: number): number {
1062
- return (
1063
- (date1.getTime() -
1064
- date1.getTimezoneOffset() * MILLISECONDS_IN_MINUTE -
1065
- (date2.getTime() - date2.getTimezoneOffset() * MILLISECONDS_IN_MINUTE)) /
1066
- interval
1067
- );
1068
- }
1069
-
1070
- function getOrdinal(n: number) {
1071
- if (n >= 11 && n <= 13) {
1072
- return `${n}th`;
1073
- }
1074
- switch (n % 10) {
1075
- case 1:
1076
- return `${n}st`;
1077
- case 2:
1078
- return `${n}nd`;
1079
- case 3:
1080
- return `${n}rd`;
1081
- }
1082
- return `${n}th`;
1083
- }
1084
-
1085
- export default {
1086
- formatDate,
1087
- clone,
1088
- isValid,
1089
- buildDate,
1090
- getDayOfWeek,
1091
- getWeekOfYear,
1092
- isBetweenDates,
1093
- addToDate,
1094
- subtractFromDate,
1095
- adjustDate,
1096
- startOfDate,
1097
- endOfDate,
1098
- getMaxDate,
1099
- getMinDate,
1100
- getDateDiff,
1101
- getDayOfYear,
1102
- inferDateFormat,
1103
- getDateBetween,
1104
- isSameDate,
1105
- daysInMonth
1106
- };