corebasic 1.0.205 → 1.0.208

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.
@@ -0,0 +1,416 @@
1
+ import { describe, test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { formatDate, fillDates } from "../date.js";
5
+
6
+
7
+ describe("formatDate()", () => {
8
+
9
+ test("formats full date correctly", () => {
10
+ const date = new Date(2026, 6, 9);
11
+
12
+ assert.equal(
13
+ formatDate(date, "YYYY-MM-DD"),
14
+ "2026-07-09"
15
+ );
16
+ });
17
+
18
+
19
+ test("formats all tokens", () => {
20
+ const date = new Date(2026, 6, 9);
21
+
22
+ assert.equal(
23
+ formatDate(date, "YYYY YY MMMM MMM MM DD D"),
24
+ "2026 26 July Jul 07 09 9"
25
+ );
26
+ });
27
+
28
+
29
+ test("formats month names", () => {
30
+ const date = new Date(2026, 0, 15);
31
+
32
+ assert.equal(
33
+ formatDate(date, "MMMM"),
34
+ "January"
35
+ );
36
+
37
+ assert.equal(
38
+ formatDate(date, "MMM"),
39
+ "Jan"
40
+ );
41
+ });
42
+
43
+
44
+ test("formats december correctly", () => {
45
+ const date = new Date(2026, 11, 31);
46
+
47
+ assert.equal(
48
+ formatDate(date, "MMMM MMM"),
49
+ "December Dec"
50
+ );
51
+ });
52
+
53
+
54
+ test("pads single digit days", () => {
55
+ const date = new Date(2026, 6, 1);
56
+
57
+ assert.equal(
58
+ formatDate(date, "DD"),
59
+ "01"
60
+ );
61
+ });
62
+
63
+
64
+ test("does not pad D token", () => {
65
+ const date = new Date(2026, 6, 1);
66
+
67
+ assert.equal(
68
+ formatDate(date, "D"),
69
+ "1"
70
+ );
71
+ });
72
+
73
+
74
+ test("pads single digit months", () => {
75
+ const date = new Date(2026, 0, 5);
76
+
77
+ assert.equal(
78
+ formatDate(date, "MM"),
79
+ "01"
80
+ );
81
+ });
82
+
83
+
84
+ test("handles Date object input", () => {
85
+ const date = new Date(2026, 5, 20);
86
+
87
+ assert.equal(
88
+ formatDate(date, "YYYY-MM-DD"),
89
+ "2026-06-20"
90
+ );
91
+ });
92
+
93
+
94
+ test("handles timestamp input", () => {
95
+ const timestamp = new Date(2026, 5, 20).getTime();
96
+
97
+ assert.equal(
98
+ formatDate(timestamp, "YYYY-MM-DD"),
99
+ "2026-06-20"
100
+ );
101
+ });
102
+
103
+
104
+ test("handles ISO string input", () => {
105
+
106
+ assert.equal(
107
+ formatDate("2026-07-09", "YYYY-MM-DD"),
108
+ "2026-07-09"
109
+ );
110
+
111
+ });
112
+
113
+
114
+ test("handles arbitrary separators", () => {
115
+ const date = new Date(2026, 6, 9);
116
+
117
+ assert.equal(
118
+ formatDate(date, "DD/MM/YYYY"),
119
+ "09/07/2026"
120
+ );
121
+
122
+
123
+ assert.equal(
124
+ formatDate(date, "D-MMM-YY"),
125
+ "9-Jul-26"
126
+ );
127
+ });
128
+
129
+
130
+ test("throws for invalid date string", () => {
131
+
132
+ assert.throws(
133
+ () => formatDate("invalid-date", "YYYY-MM-DD")
134
+ );
135
+
136
+ });
137
+
138
+
139
+ test("throws for invalid timestamp", () => {
140
+
141
+ assert.throws(
142
+ () => formatDate(NaN, "YYYY")
143
+ );
144
+
145
+ });
146
+
147
+
148
+ test("handles leap day", () => {
149
+ const date = new Date(2024, 1, 29);
150
+
151
+ assert.equal(
152
+ formatDate(date, "YYYY-MM-DD"),
153
+ "2024-02-29"
154
+ );
155
+ });
156
+
157
+
158
+ test("handles year boundary", () => {
159
+ const date = new Date(2025, 11, 31);
160
+
161
+ assert.equal(
162
+ formatDate(date, "YYYY-MM-DD"),
163
+ "2025-12-31"
164
+ );
165
+ });
166
+
167
+
168
+ test("replaces longer tokens before shorter tokens", () => {
169
+ const date = new Date(2026, 6, 9);
170
+
171
+ assert.equal(
172
+ formatDate(date, "MMMM MMM MM"),
173
+ "July Jul 07"
174
+ );
175
+ });
176
+
177
+
178
+ test("keeps unknown characters untouched", () => {
179
+ const date = new Date(2026, 6, 9);
180
+
181
+ assert.equal(
182
+ formatDate(date, "Today is DD-th"),
183
+ "Today is 09-th"
184
+ );
185
+ });
186
+
187
+ });
188
+
189
+
190
+
191
+ describe("fillDates()", () => {
192
+
193
+
194
+ test("fills daily dates", () => {
195
+ assert.deepEqual(
196
+ fillDates({from: "2026-07-01", to: "2026-07-05", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD"),
197
+ [
198
+ "2026-07-01",
199
+ "2026-07-02",
200
+ "2026-07-03",
201
+ "2026-07-04",
202
+ "2026-07-05",
203
+ ]
204
+ );
205
+
206
+ });
207
+
208
+
209
+
210
+ test("fills monthly dates", () => {
211
+ assert.deepEqual(
212
+ fillDates({from: "2026-01-01", to: "2026-05-01", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM"),
213
+ [
214
+ "2026-01",
215
+ "2026-02",
216
+ "2026-03",
217
+ "2026-04",
218
+ "2026-05",
219
+ ]
220
+ );
221
+
222
+ });
223
+
224
+
225
+
226
+ test("fills yearly dates", () => {
227
+ assert.deepEqual(
228
+ fillDates({from: "2020-01-01", to: "2024-01-01", fromOp: "$gte", toOp: "$lte"}, "YYYY"),
229
+ [
230
+ "2020",
231
+ "2021",
232
+ "2022",
233
+ "2023",
234
+ "2024",
235
+ ]
236
+ );
237
+
238
+ });
239
+
240
+
241
+
242
+ test("returns one value for same date", () => {
243
+
244
+ assert.deepEqual(
245
+ fillDates({from: "2026-07-01", to: "2026-07-01", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD"),
246
+ [
247
+ "2026-07-01"
248
+ ]
249
+ );
250
+
251
+ });
252
+
253
+
254
+
255
+ test("returns empty for reversed range", () => {
256
+
257
+ assert.deepEqual(
258
+ fillDates({from: "2026-07-10", to: "2026-07-01", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD"),
259
+ []
260
+ );
261
+
262
+ });
263
+
264
+
265
+
266
+ test("removes duplicates when format loses precision", () => {
267
+
268
+ assert.deepEqual(
269
+ fillDates({from: "2026-01-01", to: "2026-01-31", fromOp: "$gte", toOp: "$lte"}, "MMMM"),
270
+ [
271
+ "January"
272
+ ]
273
+ );
274
+
275
+ });
276
+
277
+
278
+
279
+ test("handles leap year traversal", () => {
280
+
281
+ assert.deepEqual(
282
+ fillDates({from: "2024-02-28", to: "2024-03-01", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD"),
283
+ [
284
+ "2024-02-28",
285
+ "2024-02-29",
286
+ "2024-03-01",
287
+ ]
288
+ );
289
+
290
+ });
291
+
292
+
293
+
294
+ test("handles year rollover", () => {
295
+
296
+ assert.deepEqual(
297
+ fillDates({from: "2025-12-30", to: "2026-01-02", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD"),
298
+ [
299
+ "2025-12-30",
300
+ "2025-12-31",
301
+ "2026-01-01",
302
+ "2026-01-02",
303
+ ]
304
+ );
305
+
306
+ });
307
+
308
+
309
+
310
+ test("fills across years using months", () => {
311
+
312
+ assert.deepEqual(
313
+ fillDates({from: "2025-11-01", to: "2026-02-01", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM"),
314
+ [
315
+ "2025-11",
316
+ "2025-12",
317
+ "2026-01",
318
+ "2026-02",
319
+ ]
320
+ );
321
+
322
+ });
323
+
324
+
325
+
326
+ test("large yearly range", () => {
327
+
328
+ const result = fillDates({from: "2000-01-01", to: "2020-01-01", fromOp: "$gte", toOp: "$lte"}, "YYYY");
329
+
330
+ assert.equal(
331
+ result.length,
332
+ 21
333
+ );
334
+
335
+ });
336
+
337
+
338
+
339
+ test("large daily range contains no duplicates", () => {
340
+
341
+ const result = fillDates({from: "2026-01-01", to: "2026-12-31", fromOp: "$gte", toOp: "$lte"}, "YYYY-MM-DD");
342
+ assert.equal(new Set(result).size, result.length);
343
+ });
344
+
345
+
346
+ test("excludes lower bound with $gt", () => {
347
+ assert.deepEqual(
348
+ fillDates(
349
+ {
350
+ from: "2026-07-01",
351
+ to: "2026-07-03",
352
+ fromOp: "$gt",
353
+ toOp: null,
354
+ },
355
+ "YYYY-MM-DD"
356
+ ),
357
+ [
358
+ "2026-07-02",
359
+ "2026-07-03",
360
+ ]
361
+ );
362
+ });
363
+
364
+ test("excludes upper bound with $lt", () => {
365
+ assert.deepEqual(
366
+ fillDates(
367
+ {
368
+ from: "2026-07-01",
369
+ to: "2026-07-03",
370
+ fromOp: null,
371
+ toOp: "$lt",
372
+ },
373
+ "YYYY-MM-DD"
374
+ ),
375
+ [
376
+ "2026-07-01",
377
+ "2026-07-02",
378
+ ]
379
+ );
380
+ });
381
+
382
+ test("excludes both bounds", () => {
383
+ assert.deepEqual(
384
+ fillDates(
385
+ {
386
+ from: "2026-07-01",
387
+ to: "2026-07-05",
388
+ fromOp: "$gt",
389
+ toOp: "$lt",
390
+ },
391
+ "YYYY-MM-DD"
392
+ ),
393
+ [
394
+ "2026-07-02",
395
+ "2026-07-03",
396
+ "2026-07-04",
397
+ ]
398
+ );
399
+ });
400
+
401
+ test("returns empty when exclusive bounds collapse range", () => {
402
+ assert.deepEqual(
403
+ fillDates(
404
+ {
405
+ from: "2026-07-01",
406
+ to: "2026-07-02",
407
+ fromOp: "$gt",
408
+ toOp: "$lt",
409
+ },
410
+ "YYYY-MM-DD"
411
+ ),
412
+ []
413
+ );
414
+ });
415
+
416
+ });