@kwiz/common 1.0.105 → 1.0.106

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 (106) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/browser.js +26 -1
  6. package/lib/cjs/helpers/browser.js.map +1 -1
  7. package/lib/cjs/types/libs/msal.types.js +26 -26
  8. package/lib/cjs/utils/date.js +139 -1
  9. package/lib/cjs/utils/date.js.map +1 -1
  10. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  11. package/lib/esm/helpers/browser.js +24 -0
  12. package/lib/esm/helpers/browser.js.map +1 -1
  13. package/lib/esm/types/libs/msal.types.js +26 -26
  14. package/lib/esm/utils/date.js +137 -0
  15. package/lib/esm/utils/date.js.map +1 -1
  16. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  17. package/lib/types/helpers/browser.d.ts +1 -0
  18. package/lib/types/types/locales.d.ts +1 -0
  19. package/lib/types/utils/date.d.ts +1 -0
  20. package/package.json +81 -81
  21. package/readme.md +17 -17
  22. package/src/_dependencies.ts +12 -12
  23. package/src/config.ts +17 -17
  24. package/src/helpers/Guid.ts +181 -181
  25. package/src/helpers/base64.ts +173 -173
  26. package/src/helpers/browser.test.js +13 -13
  27. package/src/helpers/browser.ts +1428 -1428
  28. package/src/helpers/browserinfo.ts +292 -292
  29. package/src/helpers/collections.base.test.js +25 -25
  30. package/src/helpers/collections.base.ts +437 -437
  31. package/src/helpers/collections.ts +107 -107
  32. package/src/helpers/color.ts +54 -54
  33. package/src/helpers/cookies.ts +59 -59
  34. package/src/helpers/date.test.js +119 -119
  35. package/src/helpers/date.ts +188 -188
  36. package/src/helpers/debug.ts +186 -186
  37. package/src/helpers/diagrams.ts +43 -43
  38. package/src/helpers/emails.ts +6 -6
  39. package/src/helpers/eval.ts +5 -5
  40. package/src/helpers/file.test.js +50 -50
  41. package/src/helpers/file.ts +63 -63
  42. package/src/helpers/flatted.ts +149 -149
  43. package/src/helpers/functions.ts +16 -16
  44. package/src/helpers/graph/calendar.types.ts +10 -10
  45. package/src/helpers/http.ts +69 -69
  46. package/src/helpers/images.ts +22 -22
  47. package/src/helpers/json.ts +44 -44
  48. package/src/helpers/md5.ts +189 -189
  49. package/src/helpers/objects.test.js +33 -33
  50. package/src/helpers/objects.ts +274 -274
  51. package/src/helpers/promises.test.js +37 -37
  52. package/src/helpers/promises.ts +165 -165
  53. package/src/helpers/random.ts +27 -27
  54. package/src/helpers/scheduler/scheduler.test.js +103 -103
  55. package/src/helpers/scheduler/scheduler.ts +131 -131
  56. package/src/helpers/sharepoint.ts +785 -785
  57. package/src/helpers/strings.test.js +122 -122
  58. package/src/helpers/strings.ts +337 -337
  59. package/src/helpers/typecheckers.test.js +34 -34
  60. package/src/helpers/typecheckers.ts +266 -266
  61. package/src/helpers/url.test.js +43 -43
  62. package/src/helpers/url.ts +207 -207
  63. package/src/helpers/urlhelper.ts +111 -111
  64. package/src/index.ts +6 -6
  65. package/src/types/auth.ts +54 -54
  66. package/src/types/common.types.ts +15 -15
  67. package/src/types/flatted.types.ts +59 -59
  68. package/src/types/globals.types.ts +6 -6
  69. package/src/types/graph/calendar.types.ts +80 -80
  70. package/src/types/knownscript.types.ts +18 -18
  71. package/src/types/libs/datajs.types.ts +28 -28
  72. package/src/types/libs/ics.types.ts +30 -30
  73. package/src/types/libs/msal.types.ts +49 -49
  74. package/src/types/locales.ts +125 -124
  75. package/src/types/localstoragecache.types.ts +8 -8
  76. package/src/types/location.types.ts +27 -27
  77. package/src/types/moment.ts +11 -11
  78. package/src/types/regex.types.ts +16 -16
  79. package/src/types/rest.types.ts +95 -95
  80. package/src/types/sharepoint.types.ts +1466 -1466
  81. package/src/types/sharepoint.utils.types.ts +287 -287
  82. package/src/utils/auth/common.ts +74 -74
  83. package/src/utils/auth/discovery.test.js +12 -12
  84. package/src/utils/auth/discovery.ts +132 -132
  85. package/src/utils/base64.ts +27 -27
  86. package/src/utils/consolelogger.ts +320 -320
  87. package/src/utils/date.ts +172 -172
  88. package/src/utils/emails.ts +24 -24
  89. package/src/utils/knownscript.ts +286 -286
  90. package/src/utils/localstoragecache.ts +446 -446
  91. package/src/utils/rest.ts +501 -501
  92. package/src/utils/script.ts +170 -170
  93. package/src/utils/sharepoint.rest/common.ts +154 -154
  94. package/src/utils/sharepoint.rest/date.ts +62 -62
  95. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  96. package/src/utils/sharepoint.rest/item.ts +547 -547
  97. package/src/utils/sharepoint.rest/list.ts +1481 -1481
  98. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  99. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  100. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  101. package/src/utils/sharepoint.rest/location.ts +141 -141
  102. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  103. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  104. package/src/utils/sharepoint.rest/user.ts +491 -491
  105. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  106. package/src/utils/sod.ts +194 -194
@@ -1,120 +1,120 @@
1
- import assert from 'assert/strict';
2
- import test from 'node:test';
3
- import { getDateFromToken, getTotalDaysInMonth, isISODate, isISODateUTC, isNowToken, isStandardDate, isTodayToken, shiftMonths } from "./date";
4
-
5
- test('isTodayToken', t => {
6
- assert.strictEqual(isTodayToken(''), false);
7
- assert.strictEqual(isTodayToken('today'), false);
8
- assert.strictEqual(isTodayToken('[ to day]'), false);
9
- assert.strictEqual(isTodayToken('[today ]'), false);
10
- assert.strictEqual(isTodayToken('asd[today]'), false);
11
- assert.strictEqual(isTodayToken('[today]adsd'), true);
12
- assert.strictEqual(isTodayToken('[today] asd'), true);
13
- assert.strictEqual(isTodayToken('[today]'), true);
14
- });
15
-
16
- test('isNowToken', t => {
17
- assert.strictEqual(isNowToken(''), false);
18
- assert.strictEqual(isNowToken('now'), false);
19
- assert.strictEqual(isNowToken('[ now]'), false);
20
- assert.strictEqual(isNowToken('[now ]'), false);
21
- assert.strictEqual(isNowToken('asd[now]'), false);
22
- assert.strictEqual(isNowToken('[now]adsd'), true);
23
- assert.strictEqual(isNowToken('[now] asd'), true);
24
- assert.strictEqual(isNowToken('[now]'), true);
25
- });
26
-
27
- test('getDateFromToken', async t => {
28
- let now = new Date();
29
- var result = getDateFromToken('[today]+1', { now: now });
30
- var expected = new Date(now.getTime());
31
- expected.setDate(expected.getDate() + 1);
32
- await t.test('1. [today]+1', () => assert.strictEqual(result.getTime(), expected.getTime()));
33
-
34
- result = getDateFromToken('[today]-1', { now: now });
35
- expected = new Date(now.getTime());
36
- expected.setDate(expected.getDate() - 1);
37
- await t.test('1. [today]-1', () => assert.strictEqual(result.getTime(), expected.getTime()));
38
-
39
- result = getDateFromToken('[now]+15', { now: now });
40
- expected = new Date(now.getTime());
41
- expected.setMinutes(expected.getMinutes() + 15);
42
- await t.test('1. [now]+15', () => assert.strictEqual(result.getTime(), expected.getTime()));
43
-
44
- result = getDateFromToken('[now]-15', { now: now });
45
- expected = new Date(now.getTime());
46
- expected.setMinutes(expected.getMinutes() - 15);
47
- await t.test('1. [now]-15', () => assert.strictEqual(result.getTime(), expected.getTime()));
48
-
49
- let test = new Date(now.getTime());
50
- test.setHours(0, 0, 0, 0);
51
-
52
- result = getDateFromToken('[today]+1', { now: now, zeroTimeForToday: true });
53
- expected = new Date(test.getTime());
54
- expected.setDate(expected.getDate() + 1);
55
- await t.test('2. [today]+1', () => assert.strictEqual(result.getTime(), expected.getTime()));
56
-
57
- result = getDateFromToken('[today]-1', { now: now, zeroTimeForToday: true });
58
- expected = new Date(test.getTime());
59
- expected.setDate(expected.getDate() - 1);
60
- await t.test('2. [today]-1', () => assert.strictEqual(result.getTime(), expected.getTime()));
61
-
62
- });
63
-
64
- test('isStandardDate', t => {
65
- assert.strictEqual(isStandardDate(''), false);
66
- assert.strictEqual(isStandardDate('2023-08-21T15:54:14.954Z'), false);
67
- assert.strictEqual(isStandardDate('2023-08-21T15:54:14.954'), false);
68
- assert.strictEqual(isStandardDate('2023-08-21T15:54:14Z'), false);
69
- assert.strictEqual(isStandardDate('2023-08-21T15:54:14'), false);
70
- assert.strictEqual(isStandardDate('2023-08-21T15:54'), true);
71
- });
72
-
73
- test('isISODate', t => {
74
- assert.strictEqual(isISODate(''), false);
75
- assert.strictEqual(isISODate('2023-08-21T15:54:14.954Z'), true);
76
- assert.strictEqual(isISODate('2023-08-21T15:54:14.954'), true);
77
- assert.strictEqual(isISODate('2023-08-21T15:54:14Z'), true);
78
- assert.strictEqual(isISODate('2023-08-21T15:54:14'), true);
79
- assert.strictEqual(isISODate('2023-08-21T15:54'), false);
80
- });
81
-
82
- test('isISODateUTC', t => {
83
- assert.strictEqual(isISODateUTC(''), false);
84
- assert.strictEqual(isISODateUTC('2023-08-21T15:54:14.954Z'), true);
85
- assert.strictEqual(isISODateUTC('2023-08-21T15:54:14.954'), false);
86
- assert.strictEqual(isISODateUTC('2023-08-21T15:54:14Z'), true);
87
- assert.strictEqual(isISODateUTC('2023-08-21T15:54:14'), false);
88
- assert.strictEqual(isISODateUTC('2023-08-21T15:54'), false);
89
- });
90
-
91
- test('getTotalDaysInMonth', t => {
92
- var d = new Date("01-01-2023");
93
- assert.strictEqual(getTotalDaysInMonth(d), 31);
94
-
95
- d = new Date("09-01-2023");
96
- assert.strictEqual(getTotalDaysInMonth(d), 30);
97
-
98
- d = new Date("02-01-2023");
99
- assert.strictEqual(getTotalDaysInMonth(d), 28);
100
-
101
- d = new Date("02-01-2024"); //leap year
102
- assert.strictEqual(getTotalDaysInMonth(d), 29);
103
- });
104
-
105
- test('shiftMonths', t => {
106
- var d = new Date("01-31-2023");
107
- shiftMonths(d, 1)
108
- assert.strictEqual(d.getMonth() + 1, 2);
109
- assert.strictEqual(d.getDate(), 28);
110
-
111
- d = new Date("01-31-2023");
112
- shiftMonths(d, 13)
113
- assert.strictEqual(d.getMonth() + 1, 2);
114
- assert.strictEqual(d.getDate(), 29); //leap year
115
-
116
- d = new Date("01-31-2023");
117
- shiftMonths(d, -11)
118
- assert.strictEqual(d.getMonth() + 1, 2);
119
- assert.strictEqual(d.getDate(), 28);
1
+ import assert from 'assert/strict';
2
+ import test from 'node:test';
3
+ import { getDateFromToken, getTotalDaysInMonth, isISODate, isISODateUTC, isNowToken, isStandardDate, isTodayToken, shiftMonths } from "./date";
4
+
5
+ test('isTodayToken', t => {
6
+ assert.strictEqual(isTodayToken(''), false);
7
+ assert.strictEqual(isTodayToken('today'), false);
8
+ assert.strictEqual(isTodayToken('[ to day]'), false);
9
+ assert.strictEqual(isTodayToken('[today ]'), false);
10
+ assert.strictEqual(isTodayToken('asd[today]'), false);
11
+ assert.strictEqual(isTodayToken('[today]adsd'), true);
12
+ assert.strictEqual(isTodayToken('[today] asd'), true);
13
+ assert.strictEqual(isTodayToken('[today]'), true);
14
+ });
15
+
16
+ test('isNowToken', t => {
17
+ assert.strictEqual(isNowToken(''), false);
18
+ assert.strictEqual(isNowToken('now'), false);
19
+ assert.strictEqual(isNowToken('[ now]'), false);
20
+ assert.strictEqual(isNowToken('[now ]'), false);
21
+ assert.strictEqual(isNowToken('asd[now]'), false);
22
+ assert.strictEqual(isNowToken('[now]adsd'), true);
23
+ assert.strictEqual(isNowToken('[now] asd'), true);
24
+ assert.strictEqual(isNowToken('[now]'), true);
25
+ });
26
+
27
+ test('getDateFromToken', async t => {
28
+ let now = new Date();
29
+ var result = getDateFromToken('[today]+1', { now: now });
30
+ var expected = new Date(now.getTime());
31
+ expected.setDate(expected.getDate() + 1);
32
+ await t.test('1. [today]+1', () => assert.strictEqual(result.getTime(), expected.getTime()));
33
+
34
+ result = getDateFromToken('[today]-1', { now: now });
35
+ expected = new Date(now.getTime());
36
+ expected.setDate(expected.getDate() - 1);
37
+ await t.test('1. [today]-1', () => assert.strictEqual(result.getTime(), expected.getTime()));
38
+
39
+ result = getDateFromToken('[now]+15', { now: now });
40
+ expected = new Date(now.getTime());
41
+ expected.setMinutes(expected.getMinutes() + 15);
42
+ await t.test('1. [now]+15', () => assert.strictEqual(result.getTime(), expected.getTime()));
43
+
44
+ result = getDateFromToken('[now]-15', { now: now });
45
+ expected = new Date(now.getTime());
46
+ expected.setMinutes(expected.getMinutes() - 15);
47
+ await t.test('1. [now]-15', () => assert.strictEqual(result.getTime(), expected.getTime()));
48
+
49
+ let test = new Date(now.getTime());
50
+ test.setHours(0, 0, 0, 0);
51
+
52
+ result = getDateFromToken('[today]+1', { now: now, zeroTimeForToday: true });
53
+ expected = new Date(test.getTime());
54
+ expected.setDate(expected.getDate() + 1);
55
+ await t.test('2. [today]+1', () => assert.strictEqual(result.getTime(), expected.getTime()));
56
+
57
+ result = getDateFromToken('[today]-1', { now: now, zeroTimeForToday: true });
58
+ expected = new Date(test.getTime());
59
+ expected.setDate(expected.getDate() - 1);
60
+ await t.test('2. [today]-1', () => assert.strictEqual(result.getTime(), expected.getTime()));
61
+
62
+ });
63
+
64
+ test('isStandardDate', t => {
65
+ assert.strictEqual(isStandardDate(''), false);
66
+ assert.strictEqual(isStandardDate('2023-08-21T15:54:14.954Z'), false);
67
+ assert.strictEqual(isStandardDate('2023-08-21T15:54:14.954'), false);
68
+ assert.strictEqual(isStandardDate('2023-08-21T15:54:14Z'), false);
69
+ assert.strictEqual(isStandardDate('2023-08-21T15:54:14'), false);
70
+ assert.strictEqual(isStandardDate('2023-08-21T15:54'), true);
71
+ });
72
+
73
+ test('isISODate', t => {
74
+ assert.strictEqual(isISODate(''), false);
75
+ assert.strictEqual(isISODate('2023-08-21T15:54:14.954Z'), true);
76
+ assert.strictEqual(isISODate('2023-08-21T15:54:14.954'), true);
77
+ assert.strictEqual(isISODate('2023-08-21T15:54:14Z'), true);
78
+ assert.strictEqual(isISODate('2023-08-21T15:54:14'), true);
79
+ assert.strictEqual(isISODate('2023-08-21T15:54'), false);
80
+ });
81
+
82
+ test('isISODateUTC', t => {
83
+ assert.strictEqual(isISODateUTC(''), false);
84
+ assert.strictEqual(isISODateUTC('2023-08-21T15:54:14.954Z'), true);
85
+ assert.strictEqual(isISODateUTC('2023-08-21T15:54:14.954'), false);
86
+ assert.strictEqual(isISODateUTC('2023-08-21T15:54:14Z'), true);
87
+ assert.strictEqual(isISODateUTC('2023-08-21T15:54:14'), false);
88
+ assert.strictEqual(isISODateUTC('2023-08-21T15:54'), false);
89
+ });
90
+
91
+ test('getTotalDaysInMonth', t => {
92
+ var d = new Date("01-01-2023");
93
+ assert.strictEqual(getTotalDaysInMonth(d), 31);
94
+
95
+ d = new Date("09-01-2023");
96
+ assert.strictEqual(getTotalDaysInMonth(d), 30);
97
+
98
+ d = new Date("02-01-2023");
99
+ assert.strictEqual(getTotalDaysInMonth(d), 28);
100
+
101
+ d = new Date("02-01-2024"); //leap year
102
+ assert.strictEqual(getTotalDaysInMonth(d), 29);
103
+ });
104
+
105
+ test('shiftMonths', t => {
106
+ var d = new Date("01-31-2023");
107
+ shiftMonths(d, 1)
108
+ assert.strictEqual(d.getMonth() + 1, 2);
109
+ assert.strictEqual(d.getDate(), 28);
110
+
111
+ d = new Date("01-31-2023");
112
+ shiftMonths(d, 13)
113
+ assert.strictEqual(d.getMonth() + 1, 2);
114
+ assert.strictEqual(d.getDate(), 29); //leap year
115
+
116
+ d = new Date("01-31-2023");
117
+ shiftMonths(d, -11)
118
+ assert.strictEqual(d.getMonth() + 1, 2);
119
+ assert.strictEqual(d.getDate(), 28);
120
120
  });
@@ -1,189 +1,189 @@
1
- /**
2
- * a date helper that is not relying on SharePoint or any utils at all
3
- * can be synced to the server code and external projects
4
- * do not add code that needs SP or utils -> move it to utils/date
5
- */
6
-
7
- import { endsWith } from "./strings";
8
- import { isDate, isNullOrUndefined, isString } from "./typecheckers";
9
-
10
- export function isISODateUTC(d: string) {
11
- //2023-08-21T15:54:14.954Z = true
12
- //2023-08-21T15:54:14.954 = false
13
- //2023-08-21T15:54:14Z = true
14
- //2023-08-21T15:54:14 = false
15
- //2023-08-21T15:54 = false
16
- return isISODate(d) && endsWith(d, "Z", true);
17
- }
18
-
19
- export function isISODate(d: string) {
20
- //2023-08-21T15:54:14.954Z = true
21
- //2023-08-21T15:54:14.954 = true
22
- //2023-08-21T15:54:14Z = true
23
- //2023-08-21T15:54:14 = true
24
- //2023-08-21T15:54 = false
25
- return isString(d) && /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/.test(d);
26
- }
27
-
28
- export function isStandardDate(d: string) {
29
- //2023-08-21T15:54:14.954Z = false
30
- //2023-08-21T15:54:14.954 = false
31
- //2023-08-21T15:54:14Z = false
32
- //2023-08-21T15:54:14 = false
33
- //2023-08-21T15:54 = true
34
- return isString(d) && /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/.test(d);
35
- }
36
-
37
- export function parseISODate(a: string): Date | null {
38
- if (!Date.prototype.toISOString) {
39
- if (a.length < 19) {
40
- return null;
41
- }
42
- var i = Number(a.substr(0, 4)),
43
- f = Number(a.substr(5, 2)),
44
- d = Number(a.substr(8, 2)),
45
- h = Number(a.substr(11, 2)),
46
- e = Number(a.substr(14, 2)),
47
- g = Number(a.substr(17, 2));
48
- if (isNaN(i) || isNaN(f) || isNaN(d) || isNaN(h) || isNaN(e) || isNaN(g)) {
49
- return null;
50
- }
51
- // b = new Date(Date.UTC(i, f - 1, d, h, e, g));
52
- return new Date(Date.UTC(i, f - 1, d, h, e, g));
53
- } else {
54
- return new Date(a);
55
- }
56
- }
57
-
58
- /** Get now date, without seconds or milliseconds. */
59
- export function getNow() {
60
- let now = new Date();
61
- now.setSeconds(0, 0);
62
- return now;
63
- }
64
- /** Get today date, without hours, minutes, seconds or milliseconds. */
65
- export function getToday() {
66
- let today = new Date();
67
- today.setHours(0, 0, 0, 0);
68
- return today;
69
- }
70
-
71
- // eslint-disable-next-line no-shadow
72
- export enum DateFormats {
73
- YMD = "yyyy-MM-dd"
74
- }
75
- export function changeDate(options: { years?: number; months?: number; days?: number; hours?: number; minutes?: number; }, startDate?: Date): Date {
76
- let newDate = isDate(startDate) ? new Date(startDate.getTime()) : new Date();//today, or the day that was passed
77
-
78
- if (options) {
79
- if (!isNullOrUndefined(options.years))
80
- newDate.setFullYear(newDate.getFullYear() + options.years);
81
-
82
- if (!isNullOrUndefined(options.months))
83
- newDate.setMonth(newDate.getMonth() + options.months);
84
-
85
- if (!isNullOrUndefined(options.days))
86
- options.hours = (isNullOrUndefined(options.hours) ? 0 : options.hours) + (options.days * 24);
87
-
88
- if (!isNullOrUndefined(options.hours))
89
- options.minutes = (isNullOrUndefined(options.minutes) ? 0 : options.minutes) + (options.hours * 60);
90
- if (!isNullOrUndefined(options.minutes) && options.minutes !== 0)
91
- newDate = new Date(newDate.getTime() + (options.minutes * 60 * 1000));
92
- }
93
-
94
- return newDate;
95
- }
96
-
97
- export function cloneDate(date: Date) {
98
- return new Date(date.getTime());
99
- }
100
-
101
- /** Returns a new date object adjusted for the time format */
102
- export function getDateFromToken(str: string, options?: { now?: Date; zeroTimeForToday?: boolean; }) {
103
- let now = options && options.now ? new Date(options.now.getTime()) : new Date();
104
-
105
- if (!isTodayToken(str) && !isNowToken(str)) return now;
106
- let token: "Now" | "Today" = isTodayToken(str) ? "Today" : "Now";
107
- var includeTime = token === "Now";
108
- if (!includeTime && options && options.zeroTimeForToday) {
109
- now.setHours(0, 0, 0, 0);
110
- }
111
-
112
- var n = str.replace(new RegExp(`\\[${token}\\]`, "im"), "");
113
- n = n.replace(/\s/g, "");
114
- var offset = n && n !== "" ? Number(n) : 0;
115
-
116
- if (includeTime) {
117
- now.setMinutes(now.getMinutes() + offset);
118
- } else {
119
- now.setDate(now.getDate() + offset);
120
- }
121
-
122
- return now;
123
- }
124
-
125
- export function isTodayToken(str: string) {
126
- return str.match(/^\[today\]/im) !== null;
127
- }
128
- export function isNowToken(str: string) {
129
- return str.match(/^\[now\]/im) !== null;
130
- }
131
-
132
- /** Get the number of seconds from date (in the past) to now */
133
- export function getSecondsElapsed(from: Date) {
134
- let secondsElapsed = (new Date().getTime() - from.getTime()) / 1000;
135
- return secondsElapsed;
136
- }
137
-
138
- /** Get the total number of days in a given month specified by a date object */
139
- export function getTotalDaysInMonth(obj: Date) {
140
- if (isDate(obj)) {
141
- let d = new Date(obj.getFullYear(), obj.getMonth() + 1, 0);
142
- return d.getDate();
143
- }
144
- return 0;
145
- }
146
-
147
- /** Shifts the date by a certain number of months and takes into account the total number of days in the new month.
148
- * For example, if you shift January 31st by one month it will return February 28th (29th for a leap year) instead of March 3rd (2nd in leap year)
149
- */
150
- export function shiftMonths(obj: Date, months: number) {
151
- let desiredDate = obj.getDate();
152
- //move to first day to avoid skipping months (ie. Jan 31 -> March 3)
153
- obj.setDate(1);
154
- obj.setMonth(obj.getMonth() + months);
155
- obj.setDate(Math.min(desiredDate, getTotalDaysInMonth(obj)));
156
- }
157
-
158
- /**
159
- * Gets last day of month given a date
160
- */
161
- export function getLastDayOfMonth(obj: Date) {
162
- return new Date(obj.getFullYear(), obj.getMonth() + 1, 0);
163
- }
164
-
165
- // in miliseconds
166
- var units = {
167
- year: 24 * 60 * 60 * 1000 * 365,
168
- month: 24 * 60 * 60 * 1000 * 365 / 12,
169
- day: 24 * 60 * 60 * 1000,
170
- hour: 60 * 60 * 1000,
171
- minute: 60 * 1000,
172
- second: 1000
173
- }
174
-
175
- const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
176
- //const rtf = new Intl.RelativeTimeFormat('en', { style: 'short' });
177
-
178
- export function getRelativeTime(d1: Date, d2 = new Date()) {
179
- try {
180
- var elapsed = d1.getTime() - d2.getTime();
181
-
182
- // "Math.abs" accounts for both "past" & "future" scenarios
183
- for (var u in units)
184
- if (Math.abs(elapsed) > units[u] || u === 'second')
185
- return rtf.format(Math.round(elapsed / units[u]), u as Intl.RelativeTimeFormatUnit);
186
- } catch (e) {
187
- return "";
188
- }
1
+ /**
2
+ * a date helper that is not relying on SharePoint or any utils at all
3
+ * can be synced to the server code and external projects
4
+ * do not add code that needs SP or utils -> move it to utils/date
5
+ */
6
+
7
+ import { endsWith } from "./strings";
8
+ import { isDate, isNullOrUndefined, isString } from "./typecheckers";
9
+
10
+ export function isISODateUTC(d: string) {
11
+ //2023-08-21T15:54:14.954Z = true
12
+ //2023-08-21T15:54:14.954 = false
13
+ //2023-08-21T15:54:14Z = true
14
+ //2023-08-21T15:54:14 = false
15
+ //2023-08-21T15:54 = false
16
+ return isISODate(d) && endsWith(d, "Z", true);
17
+ }
18
+
19
+ export function isISODate(d: string) {
20
+ //2023-08-21T15:54:14.954Z = true
21
+ //2023-08-21T15:54:14.954 = true
22
+ //2023-08-21T15:54:14Z = true
23
+ //2023-08-21T15:54:14 = true
24
+ //2023-08-21T15:54 = false
25
+ return isString(d) && /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/.test(d);
26
+ }
27
+
28
+ export function isStandardDate(d: string) {
29
+ //2023-08-21T15:54:14.954Z = false
30
+ //2023-08-21T15:54:14.954 = false
31
+ //2023-08-21T15:54:14Z = false
32
+ //2023-08-21T15:54:14 = false
33
+ //2023-08-21T15:54 = true
34
+ return isString(d) && /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/.test(d);
35
+ }
36
+
37
+ export function parseISODate(a: string): Date | null {
38
+ if (!Date.prototype.toISOString) {
39
+ if (a.length < 19) {
40
+ return null;
41
+ }
42
+ var i = Number(a.substr(0, 4)),
43
+ f = Number(a.substr(5, 2)),
44
+ d = Number(a.substr(8, 2)),
45
+ h = Number(a.substr(11, 2)),
46
+ e = Number(a.substr(14, 2)),
47
+ g = Number(a.substr(17, 2));
48
+ if (isNaN(i) || isNaN(f) || isNaN(d) || isNaN(h) || isNaN(e) || isNaN(g)) {
49
+ return null;
50
+ }
51
+ // b = new Date(Date.UTC(i, f - 1, d, h, e, g));
52
+ return new Date(Date.UTC(i, f - 1, d, h, e, g));
53
+ } else {
54
+ return new Date(a);
55
+ }
56
+ }
57
+
58
+ /** Get now date, without seconds or milliseconds. */
59
+ export function getNow() {
60
+ let now = new Date();
61
+ now.setSeconds(0, 0);
62
+ return now;
63
+ }
64
+ /** Get today date, without hours, minutes, seconds or milliseconds. */
65
+ export function getToday() {
66
+ let today = new Date();
67
+ today.setHours(0, 0, 0, 0);
68
+ return today;
69
+ }
70
+
71
+ // eslint-disable-next-line no-shadow
72
+ export enum DateFormats {
73
+ YMD = "yyyy-MM-dd"
74
+ }
75
+ export function changeDate(options: { years?: number; months?: number; days?: number; hours?: number; minutes?: number; }, startDate?: Date): Date {
76
+ let newDate = isDate(startDate) ? new Date(startDate.getTime()) : new Date();//today, or the day that was passed
77
+
78
+ if (options) {
79
+ if (!isNullOrUndefined(options.years))
80
+ newDate.setFullYear(newDate.getFullYear() + options.years);
81
+
82
+ if (!isNullOrUndefined(options.months))
83
+ newDate.setMonth(newDate.getMonth() + options.months);
84
+
85
+ if (!isNullOrUndefined(options.days))
86
+ options.hours = (isNullOrUndefined(options.hours) ? 0 : options.hours) + (options.days * 24);
87
+
88
+ if (!isNullOrUndefined(options.hours))
89
+ options.minutes = (isNullOrUndefined(options.minutes) ? 0 : options.minutes) + (options.hours * 60);
90
+ if (!isNullOrUndefined(options.minutes) && options.minutes !== 0)
91
+ newDate = new Date(newDate.getTime() + (options.minutes * 60 * 1000));
92
+ }
93
+
94
+ return newDate;
95
+ }
96
+
97
+ export function cloneDate(date: Date) {
98
+ return new Date(date.getTime());
99
+ }
100
+
101
+ /** Returns a new date object adjusted for the time format */
102
+ export function getDateFromToken(str: string, options?: { now?: Date; zeroTimeForToday?: boolean; }) {
103
+ let now = options && options.now ? new Date(options.now.getTime()) : new Date();
104
+
105
+ if (!isTodayToken(str) && !isNowToken(str)) return now;
106
+ let token: "Now" | "Today" = isTodayToken(str) ? "Today" : "Now";
107
+ var includeTime = token === "Now";
108
+ if (!includeTime && options && options.zeroTimeForToday) {
109
+ now.setHours(0, 0, 0, 0);
110
+ }
111
+
112
+ var n = str.replace(new RegExp(`\\[${token}\\]`, "im"), "");
113
+ n = n.replace(/\s/g, "");
114
+ var offset = n && n !== "" ? Number(n) : 0;
115
+
116
+ if (includeTime) {
117
+ now.setMinutes(now.getMinutes() + offset);
118
+ } else {
119
+ now.setDate(now.getDate() + offset);
120
+ }
121
+
122
+ return now;
123
+ }
124
+
125
+ export function isTodayToken(str: string) {
126
+ return str.match(/^\[today\]/im) !== null;
127
+ }
128
+ export function isNowToken(str: string) {
129
+ return str.match(/^\[now\]/im) !== null;
130
+ }
131
+
132
+ /** Get the number of seconds from date (in the past) to now */
133
+ export function getSecondsElapsed(from: Date) {
134
+ let secondsElapsed = (new Date().getTime() - from.getTime()) / 1000;
135
+ return secondsElapsed;
136
+ }
137
+
138
+ /** Get the total number of days in a given month specified by a date object */
139
+ export function getTotalDaysInMonth(obj: Date) {
140
+ if (isDate(obj)) {
141
+ let d = new Date(obj.getFullYear(), obj.getMonth() + 1, 0);
142
+ return d.getDate();
143
+ }
144
+ return 0;
145
+ }
146
+
147
+ /** Shifts the date by a certain number of months and takes into account the total number of days in the new month.
148
+ * For example, if you shift January 31st by one month it will return February 28th (29th for a leap year) instead of March 3rd (2nd in leap year)
149
+ */
150
+ export function shiftMonths(obj: Date, months: number) {
151
+ let desiredDate = obj.getDate();
152
+ //move to first day to avoid skipping months (ie. Jan 31 -> March 3)
153
+ obj.setDate(1);
154
+ obj.setMonth(obj.getMonth() + months);
155
+ obj.setDate(Math.min(desiredDate, getTotalDaysInMonth(obj)));
156
+ }
157
+
158
+ /**
159
+ * Gets last day of month given a date
160
+ */
161
+ export function getLastDayOfMonth(obj: Date) {
162
+ return new Date(obj.getFullYear(), obj.getMonth() + 1, 0);
163
+ }
164
+
165
+ // in miliseconds
166
+ var units = {
167
+ year: 24 * 60 * 60 * 1000 * 365,
168
+ month: 24 * 60 * 60 * 1000 * 365 / 12,
169
+ day: 24 * 60 * 60 * 1000,
170
+ hour: 60 * 60 * 1000,
171
+ minute: 60 * 1000,
172
+ second: 1000
173
+ }
174
+
175
+ const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
176
+ //const rtf = new Intl.RelativeTimeFormat('en', { style: 'short' });
177
+
178
+ export function getRelativeTime(d1: Date, d2 = new Date()) {
179
+ try {
180
+ var elapsed = d1.getTime() - d2.getTime();
181
+
182
+ // "Math.abs" accounts for both "past" & "future" scenarios
183
+ for (var u in units)
184
+ if (Math.abs(elapsed) > units[u] || u === 'second')
185
+ return rtf.format(Math.round(elapsed / units[u]), u as Intl.RelativeTimeFormatUnit);
186
+ } catch (e) {
187
+ return "";
188
+ }
189
189
  }