@nivinjoseph/n-date 1.0.1

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 (43) hide show
  1. package/.editorconfig +14 -0
  2. package/.vscode/launch.json +56 -0
  3. package/.vscode/settings.json +111 -0
  4. package/.vscode/tasks.json +12 -0
  5. package/.yarn/releases/yarn-4.14.1.cjs +940 -0
  6. package/.yarnrc.yml +8 -0
  7. package/LICENSE +21 -0
  8. package/README.md +21 -0
  9. package/dist/date-time-format.d.ts +11 -0
  10. package/dist/date-time-format.d.ts.map +1 -0
  11. package/dist/date-time-format.js +11 -0
  12. package/dist/date-time-format.js.map +1 -0
  13. package/dist/date-time-span.d.ts +70 -0
  14. package/dist/date-time-span.d.ts.map +1 -0
  15. package/dist/date-time-span.js +122 -0
  16. package/dist/date-time-span.js.map +1 -0
  17. package/dist/date-time.d.ts +391 -0
  18. package/dist/date-time.d.ts.map +1 -0
  19. package/dist/date-time.js +753 -0
  20. package/dist/date-time.js.map +1 -0
  21. package/dist/index.d.ts +4 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +4 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/tsconfig.json +13 -0
  26. package/docs/README.md +33 -0
  27. package/docs/date-time-span.md +72 -0
  28. package/docs/date-time.md +169 -0
  29. package/docs/formats.md +56 -0
  30. package/docs/getting-started.md +151 -0
  31. package/eslint.config.js +596 -0
  32. package/package.json +57 -0
  33. package/src/date-time-format.ts +35 -0
  34. package/src/date-time-span.ts +130 -0
  35. package/src/date-time.ts +950 -0
  36. package/src/index.ts +3 -0
  37. package/test/date-time-comparison.test.ts +1579 -0
  38. package/test/date-time-create.test.ts +1147 -0
  39. package/test/date-time-math.test.ts +324 -0
  40. package/test/date-time-properties.test.ts +200 -0
  41. package/test/date-time-utility.test.ts +432 -0
  42. package/test/date-time-validations.test.ts +521 -0
  43. package/tsconfig.json +31 -0
@@ -0,0 +1,521 @@
1
+ import assert from "node:assert";
2
+ import { describe, test } from "node:test";
3
+ import { DateTime, DateTimeFormat } from "../src/index.js";
4
+
5
+
6
+ await describe("DateTime Format Validations", async () =>
7
+ {
8
+ await describe("Date Time Format", async () =>
9
+ {
10
+ await test(`Given value "2024-01-01 10:00" with correct format
11
+ when the format is validated
12
+ then it should return true`,
13
+ () =>
14
+ {
15
+ assert.ok(DateTime.validateDateTimeFormat("2024-01-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
16
+ }
17
+ );
18
+
19
+ await test(`Given value as an empty string
20
+ when the format is validated
21
+ then it should return false`,
22
+ () =>
23
+ {
24
+ assert.ok(!DateTime.validateDateTimeFormat("", DateTimeFormat.yearMonthDayHourMinute));
25
+ }
26
+ );
27
+
28
+ await test(`Given value "2024-01-01 10:60" with an invalid minute
29
+ when the format is validated
30
+ then it should return false`,
31
+ () =>
32
+ {
33
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-01 10:60", DateTimeFormat.yearMonthDayHourMinute));
34
+ }
35
+ );
36
+
37
+ await test(`Given value "2024-01-01 10:0" with an invalid minute format
38
+ when the format is validated
39
+ then it should return false`,
40
+ () =>
41
+ {
42
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-01 10:0", DateTimeFormat.yearMonthDayHourMinute));
43
+ }
44
+ );
45
+
46
+ await test(`Given value "2024-01-01 25:00" with an invalid hour
47
+ when the format is validated
48
+ then it should return false`,
49
+ () =>
50
+ {
51
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-01 25:00", DateTimeFormat.yearMonthDayHourMinute));
52
+ }
53
+ );
54
+
55
+ await test(`Given value "2024-01-01 1:00" with an invalid hour format
56
+ when the format is validated
57
+ then it should return false`,
58
+ () =>
59
+ {
60
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-01 1:00", DateTimeFormat.yearMonthDayHourMinute));
61
+ }
62
+ );
63
+
64
+ await test(`Given value "2024-01-00 10:00" with an invalid day(0)
65
+ when the format is validated
66
+ then it should return false`,
67
+ () =>
68
+ {
69
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-00 10:00", DateTimeFormat.yearMonthDayHourMinute));
70
+ }
71
+ );
72
+
73
+ await test(`Given value "2024-01-32 10:00" with an invalid day(32)
74
+ when the format is validated
75
+ then it should return false`,
76
+ () =>
77
+ {
78
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-32 10:00", DateTimeFormat.yearMonthDayHourMinute));
79
+ }
80
+ );
81
+
82
+ await test(`Given value "2023-02-29 10:00" with an invalid day(february 29 on non-leap year)
83
+ when the format is validated
84
+ then it should return false`,
85
+ () =>
86
+ {
87
+ assert.ok(!DateTime.validateDateTimeFormat("2023-02-29 10:00", DateTimeFormat.yearMonthDayHourMinute));
88
+ }
89
+ );
90
+
91
+ await test(`Given value "2024-02-30 10:00" with an invalid day(february 30 on leap year)
92
+ when the format is validated
93
+ then it should return false`,
94
+ () =>
95
+ {
96
+ assert.ok(!DateTime.validateDateTimeFormat("2024-02-30 10:00", DateTimeFormat.yearMonthDayHourMinute));
97
+ }
98
+ );
99
+
100
+ await test(`Given value "2024-02-29 10:00" with an valid day(february 29 on leap year)
101
+ when the format is validated
102
+ then it should return true`,
103
+ () =>
104
+ {
105
+ assert.ok(DateTime.validateDateTimeFormat("2024-02-29 10:00", DateTimeFormat.yearMonthDayHourMinute));
106
+ }
107
+ );
108
+
109
+ await test(`Given value "2024-04-31 10:00" with an invalid day(April 31)
110
+ when the format is validated
111
+ then it should return false`,
112
+ () =>
113
+ {
114
+ assert.ok(!DateTime.validateDateTimeFormat("2024-04-31 10:00", DateTimeFormat.yearMonthDayHourMinute));
115
+ }
116
+ );
117
+
118
+ await test(`Given value "2024-04-1 10:00" with an invalid day format
119
+ when the format is validated
120
+ then it should return false`,
121
+ () =>
122
+ {
123
+ assert.ok(!DateTime.validateDateTimeFormat("2024-01-1 10:00", DateTimeFormat.yearMonthDayHourMinute));
124
+ }
125
+ );
126
+
127
+ await test(`Given value "2024-00-01 10:00" with an invalid month(0)
128
+ when the format is validated
129
+ then it should return false`,
130
+ () =>
131
+ {
132
+ assert.ok(!DateTime.validateDateTimeFormat("2024-00-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
133
+ }
134
+ );
135
+
136
+ await test(`Given value "2024-13-01 10:00" with an invalid month(13)
137
+ when the format is validated
138
+ then it should return false`,
139
+ () =>
140
+ {
141
+ assert.ok(!DateTime.validateDateTimeFormat("2024-13-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
142
+ }
143
+ );
144
+
145
+ await test(`Given value "2024-1-01 10:00" with an invalid month format
146
+ when the format is validated
147
+ then it should return false`,
148
+ () =>
149
+ {
150
+ assert.ok(!DateTime.validateDateTimeFormat("2024-1-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
151
+ }
152
+ );
153
+
154
+ await test(`Given value "24-01-01 10:00" with an invalid year format
155
+ when the format is validated
156
+ then it should return false`,
157
+ () =>
158
+ {
159
+ assert.ok(!DateTime.validateDateTimeFormat("24-01-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
160
+ }
161
+ );
162
+
163
+ await test(`Given value "10000-01-01 10:00" with year greater than 9999
164
+ when the format is validated
165
+ then it should return false`,
166
+ () =>
167
+ {
168
+ assert.ok(!DateTime.validateDateTimeFormat("10000-01-01 10:00", DateTimeFormat.yearMonthDayHourMinute));
169
+ }
170
+ );
171
+ }
172
+ );
173
+
174
+
175
+ await describe("Date Format", async () =>
176
+ {
177
+ await test(`Given value "2024-01-01" with correct format
178
+ when the format is validated
179
+ then it should return true`,
180
+ () =>
181
+ {
182
+ assert.ok(DateTime.validateDateFormat("2024-01-01"));
183
+ }
184
+ );
185
+
186
+ await test(`Given value as an empty string
187
+ when the format is validated
188
+ then it should return false`,
189
+ () =>
190
+ {
191
+ assert.ok(!DateTime.validateDateFormat(""));
192
+ }
193
+ );
194
+
195
+ await test(`Given value "2024-01-00" with an invalid day(0)
196
+ when the format is validated
197
+ then it should return false`,
198
+ () =>
199
+ {
200
+ assert.ok(!DateTime.validateDateFormat("2024-01-00"));
201
+ }
202
+ );
203
+
204
+ await test(`Given value "2024-01-32" with an invalid day(32)
205
+ when the format is validated
206
+ then it should return false`,
207
+ () =>
208
+ {
209
+ assert.ok(!DateTime.validateDateFormat("2024-01-32"));
210
+ }
211
+ );
212
+
213
+ await test(`Given value "2023-02-29" with an invalid day(february 29 on non-leap year)
214
+ when the format is validated
215
+ then it should return false`,
216
+ () =>
217
+ {
218
+ assert.ok(!DateTime.validateDateFormat("2023-02-29"));
219
+ }
220
+ );
221
+
222
+ await test(`Given value "2024-02-30" with an invalid day(february 30 on leap year)
223
+ when the format is validated
224
+ then it should return false`,
225
+ () =>
226
+ {
227
+ assert.ok(!DateTime.validateDateFormat("2024-02-30"));
228
+ }
229
+ );
230
+
231
+ await test(`Given value "2024-02-29" with an valid day(february 29 on leap year)
232
+ when the format is validated
233
+ then it should return true`,
234
+ () =>
235
+ {
236
+ assert.ok(DateTime.validateDateFormat("2024-02-29"));
237
+ }
238
+ );
239
+
240
+ await test(`Given value "2024-04-31" with an invalid day(April 31)
241
+ when the format is validated
242
+ then it should return false`,
243
+ () =>
244
+ {
245
+ assert.ok(!DateTime.validateDateFormat("2024-04-31"));
246
+ }
247
+ );
248
+
249
+ await test(`Given value "2024-04-1" with an invalid day format
250
+ when the format is validated
251
+ then it should return false`,
252
+ () =>
253
+ {
254
+ assert.ok(!DateTime.validateDateFormat("2024-01-1"));
255
+ }
256
+ );
257
+
258
+ await test(`Given value "2024-00-01" with an invalid month(0)
259
+ when the format is validated
260
+ then it should return false`,
261
+ () =>
262
+ {
263
+ assert.ok(!DateTime.validateDateFormat("2024-00-01"));
264
+ }
265
+ );
266
+
267
+ await test(`Given value "2024-13-01" with an invalid month(13)
268
+ when the format is validated
269
+ then it should return false`,
270
+ () =>
271
+ {
272
+ assert.ok(!DateTime.validateDateFormat("2024-13-01"));
273
+ }
274
+ );
275
+
276
+ await test(`Given value "2024-1-01" with an invalid month format
277
+ when the format is validated
278
+ then it should return false`,
279
+ () =>
280
+ {
281
+ assert.ok(!DateTime.validateDateFormat("2024-1-01"));
282
+ }
283
+ );
284
+
285
+ await test(`Given value "24-01-01" with an invalid year format
286
+ when the format is validated
287
+ then it should return false`,
288
+ () =>
289
+ {
290
+ assert.ok(!DateTime.validateDateFormat("24-01-01"));
291
+ }
292
+ );
293
+
294
+ await test(`Given value "10000-01-01" with year greater than 9999
295
+ when the format is validated
296
+ then it should return false`,
297
+ () =>
298
+ {
299
+ assert.ok(!DateTime.validateDateFormat("10000-01-01"));
300
+ }
301
+ );
302
+ }
303
+ );
304
+
305
+ await describe("Time Format", async () =>
306
+ {
307
+ await test(`Given value "10:00" with correct format
308
+ when the format is validated
309
+ then it should return true`,
310
+ () =>
311
+ {
312
+ assert.ok(DateTime.validateTimeFormat("10:00"));
313
+ }
314
+ );
315
+
316
+ await test(`Given value as an empty string
317
+ when the format is validated
318
+ then it should return false`,
319
+ () =>
320
+ {
321
+ assert.ok(!DateTime.validateTimeFormat(""));
322
+ }
323
+ );
324
+
325
+ await test(`Given value "10:60" with an invalid minute
326
+ when the format is validated
327
+ then it should return false`,
328
+ () =>
329
+ {
330
+ assert.ok(!DateTime.validateTimeFormat("10:60"));
331
+ }
332
+ );
333
+
334
+ await test(`Given value "10:0" with an invalid minute format
335
+ when the format is validated
336
+ then it should return false`,
337
+ () =>
338
+ {
339
+ assert.ok(!DateTime.validateTimeFormat("10:0"));
340
+ }
341
+ );
342
+
343
+ await test(`Given value "25:00" with an invalid hour
344
+ when the format is validated
345
+ then it should return false`,
346
+ () =>
347
+ {
348
+ assert.ok(!DateTime.validateTimeFormat("25:00"));
349
+ }
350
+ );
351
+
352
+ await test(`Given value "1:00" with an invalid hour format
353
+ when the format is validated
354
+ then it should return false`,
355
+ () =>
356
+ {
357
+ assert.ok(!DateTime.validateTimeFormat("1:00"));
358
+ }
359
+ );
360
+ }
361
+ );
362
+
363
+ await describe("Zone", async () =>
364
+ {
365
+ await test(`Given zone as an empty string
366
+ when the zone is validated
367
+ then it should return false`,
368
+ () =>
369
+ {
370
+ assert.ok(!DateTime.validateTimeZone(""));
371
+ }
372
+ );
373
+
374
+ await test(`Given zone as an invalid random string
375
+ when the zone is validated
376
+ then it should return false`,
377
+ () =>
378
+ {
379
+ assert.ok(!DateTime.validateTimeZone("aksfljn"));
380
+ }
381
+ );
382
+
383
+ await test(`Given zone as valid IANA zone America/Los_Angeles
384
+ when the zone is validated
385
+ then it should return true`,
386
+ () =>
387
+ {
388
+ assert.ok(DateTime.validateTimeZone("America/Los_Angeles"));
389
+ }
390
+ );
391
+
392
+ await test(`Given zone as invalid IANA zone America/LosAngeles (misspelled)
393
+ when the zone is validated
394
+ then it should return false`,
395
+ () =>
396
+ {
397
+ assert.ok(!DateTime.validateTimeZone("America/LosAngeles")); // correct is America/Los_Angeles
398
+ }
399
+ );
400
+
401
+ await test(`Given zone as UTC
402
+ when the zone is validated
403
+ then it should return true`,
404
+ () =>
405
+ {
406
+ assert.ok(DateTime.validateTimeZone("UTC"));
407
+ }
408
+ );
409
+
410
+ await test(`Given zone as utc
411
+ when the zone is validated
412
+ then it should return true`,
413
+ () =>
414
+ {
415
+ assert.ok(DateTime.validateTimeZone("utc"));
416
+ }
417
+ );
418
+
419
+ await test(`Given zone as local
420
+ when the zone is validated
421
+ then it should return false`,
422
+ () =>
423
+ {
424
+ assert.ok(!DateTime.validateTimeZone("local"));
425
+ }
426
+ );
427
+
428
+ await test(`Given zone as valid UTC offset +5:30"
429
+ when the zone is validated
430
+ then it should return true`,
431
+ () =>
432
+ {
433
+ assert.ok(DateTime.validateTimeZone("UTC+5:30"));
434
+ }
435
+ );
436
+
437
+ await test(`Given zone as valid UTC offset -3"
438
+ when the zone is validated
439
+ then it should return true`,
440
+ () =>
441
+ {
442
+ assert.ok(DateTime.validateTimeZone("UTC-3"));
443
+ }
444
+ );
445
+
446
+ await test(`Given zone as valid UTC offset +14:00"
447
+ when the zone is validated
448
+ then it should return true`,
449
+ () =>
450
+ {
451
+ assert.ok(DateTime.validateTimeZone("UTC+14:00"));
452
+ }
453
+ );
454
+
455
+ await test(`Given zone as valid UTC offset -12:00"
456
+ when the zone is validated
457
+ then it should return true`,
458
+ () =>
459
+ {
460
+ assert.ok(DateTime.validateTimeZone("UTC-12:00"));
461
+ }
462
+ );
463
+
464
+ await test(`Given zone as valid UTC offset +00:01"
465
+ when the zone is validated
466
+ then it should return true`,
467
+ () =>
468
+ {
469
+ assert.ok(DateTime.validateTimeZone("UTC+00:01"));
470
+ }
471
+ );
472
+
473
+ await test(`Given zone as valid UTC offset -00:01"
474
+ when the zone is validated
475
+ then it should return true`,
476
+ () =>
477
+ {
478
+ assert.ok(DateTime.validateTimeZone("UTC-00:01"));
479
+ }
480
+ );
481
+
482
+ await test(`Given zone as invalid UTC offset +14:01"
483
+ when the zone is validated
484
+ then it should return false`,
485
+ () =>
486
+ {
487
+ assert.ok(!DateTime.validateTimeZone("UTC+14:01")); // max is +14:00
488
+ }
489
+ );
490
+
491
+ await test(`Given zone as invalid UTC offset -12:01"
492
+ when the zone is validated
493
+ then it should return false`,
494
+ () =>
495
+ {
496
+ assert.ok(!DateTime.validateTimeZone("UTC-12:01")); // min is -12:00
497
+ }
498
+ );
499
+
500
+ await test(`Given zone as invalid UTC offset +15"
501
+ when the zone is validated
502
+ then it should return false`,
503
+ () =>
504
+ {
505
+ assert.ok(!DateTime.validateTimeZone("UTC+15")); // max is +14:00
506
+ }
507
+ );
508
+
509
+ await test(`Given zone as invalid UTC offset -13"
510
+ when the zone is validated
511
+ then it should return false`,
512
+ () =>
513
+ {
514
+ assert.ok(!DateTime.validateTimeZone("UTC-13")); // min is -12:00
515
+ }
516
+ );
517
+ }
518
+ );
519
+ }
520
+ );
521
+
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "NodeNext",
4
+ "target": "ES2023",
5
+ "lib": [
6
+ "ES2023",
7
+ "esnext.decorators"
8
+ ],
9
+ "types": ["node"],
10
+ "strict": true,
11
+ "strictNullChecks": true,
12
+ "strictFunctionTypes": true,
13
+ "noImplicitThis": true,
14
+ "noImplicitReturns": true,
15
+ "noUnusedLocals": true,
16
+ "noUnusedParameters": true,
17
+ "noFallthroughCasesInSwitch": true,
18
+ "noEmitOnError": true,
19
+ "sourceMap": true,
20
+ "removeComments": false,
21
+ "forceConsistentCasingInFileNames": true,
22
+ "incremental": false,
23
+ "skipLibCheck": true,
24
+ "importHelpers": true,
25
+ "noEmitHelpers": true,
26
+ "noImplicitOverride": true,
27
+ "pretty": true,
28
+ "esModuleInterop": true,
29
+ "allowSyntheticDefaultImports": true
30
+ }
31
+ }