@miso.ai/server-commons 0.6.5-beta.16 → 0.6.5-beta.17
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.
- package/package.json +1 -1
- package/src/date.js +14 -12
- package/test/date.test.js +53 -1
package/package.json
CHANGED
package/src/date.js
CHANGED
|
@@ -81,7 +81,7 @@ function floorTimestamp(ts, unit) {
|
|
|
81
81
|
case 'month':
|
|
82
82
|
return Date.UTC(year, month - 1);
|
|
83
83
|
case 'quarter':
|
|
84
|
-
return Date.UTC(year, month - month % 3);
|
|
84
|
+
return Date.UTC(year, month - month % 3 - 3);
|
|
85
85
|
case 'year':
|
|
86
86
|
return Date.UTC(year);
|
|
87
87
|
default:
|
|
@@ -95,10 +95,21 @@ export function ceilDate(expr, unit) {
|
|
|
95
95
|
return undefined;
|
|
96
96
|
}
|
|
97
97
|
const [_unit, ts] = parseDateExpr(expr);
|
|
98
|
-
|
|
98
|
+
unit = unit || _unit;
|
|
99
|
+
const floored = floorTimestamp(ts, unit);
|
|
100
|
+
return floored === ts ? floored : nextTimestamp(floored, unit);
|
|
99
101
|
}
|
|
100
102
|
|
|
101
|
-
function
|
|
103
|
+
export function nextDate(expr, unit) {
|
|
104
|
+
validateUnit(unit);
|
|
105
|
+
if (expr === undefined) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const [_unit, ts] = parseDateExpr(expr);
|
|
109
|
+
return nextTimestamp(ts + 1, unit || _unit);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function nextTimestamp(ts, unit) {
|
|
102
113
|
switch (unit) {
|
|
103
114
|
case 'millisecond':
|
|
104
115
|
return ts;
|
|
@@ -129,15 +140,6 @@ function ceilTimestamp(ts, unit) {
|
|
|
129
140
|
}
|
|
130
141
|
}
|
|
131
142
|
|
|
132
|
-
export function nextDate(expr, unit) {
|
|
133
|
-
validateUnit(unit);
|
|
134
|
-
if (expr === undefined) {
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
137
|
-
const [_unit, ts] = parseDateExpr(expr);
|
|
138
|
-
return ceilTimestamp(ts + 1, unit || _unit);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
143
|
export function prevDate(expr, unit) {
|
|
142
144
|
validateUnit(unit);
|
|
143
145
|
if (expr === undefined) {
|
package/test/date.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from 'uvu';
|
|
2
2
|
import * as assert from 'uvu/assert';
|
|
3
|
-
import { startOfDate, endOfDate } from '../src/index.js';
|
|
3
|
+
import { floorDate, ceilDate, nextDate, prevDate, startOfDate, endOfDate } from '../src/index.js';
|
|
4
4
|
|
|
5
5
|
function testDateFn(fn, input, output) {
|
|
6
6
|
assert.equal(new Date(fn(input)).toISOString(), output, `${fn.name}(${input})`);
|
|
@@ -32,4 +32,56 @@ test('endOfDate', () => {
|
|
|
32
32
|
testDateFn(endOfDate, '2025-03-03', '2025-03-03T23:59:59.000Z');
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
test('floorDate', () => {
|
|
36
|
+
testDateFn(floorDate, '2025', '2025-01-01T00:00:00.000Z');
|
|
37
|
+
testDateFn(floorDate, '2025-Q1', '2025-01-01T00:00:00.000Z');
|
|
38
|
+
testDateFn(floorDate, '2025-q2', '2025-04-01T00:00:00.000Z');
|
|
39
|
+
testDateFn(floorDate, '2025-Q3', '2025-07-01T00:00:00.000Z');
|
|
40
|
+
testDateFn(floorDate, '2025-q4', '2025-10-01T00:00:00.000Z');
|
|
41
|
+
testDateFn(floorDate, '2025-02', '2025-02-01T00:00:00.000Z');
|
|
42
|
+
testDateFn(floorDate, '2025-02-W1', '2025-02-01T00:00:00.000Z');
|
|
43
|
+
testDateFn(floorDate, '2025-02-w2', '2025-02-08T00:00:00.000Z');
|
|
44
|
+
testDateFn(floorDate, '2025-02-03', '2025-02-03T00:00:00.000Z');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('ceilDate', () => {
|
|
48
|
+
testDateFn(ceilDate, '2025', '2025-01-01T00:00:00.000Z');
|
|
49
|
+
testDateFn(ceilDate, '2025-Q1', '2025-01-01T00:00:00.000Z');
|
|
50
|
+
testDateFn(ceilDate, '2025-q2', '2025-04-01T00:00:00.000Z');
|
|
51
|
+
testDateFn(ceilDate, '2025-Q3', '2025-07-01T00:00:00.000Z');
|
|
52
|
+
testDateFn(ceilDate, '2025-q4', '2025-10-01T00:00:00.000Z');
|
|
53
|
+
testDateFn(ceilDate, '2025-02', '2025-02-01T00:00:00.000Z');
|
|
54
|
+
testDateFn(ceilDate, '2025-02-W1', '2025-02-01T00:00:00.000Z');
|
|
55
|
+
testDateFn(ceilDate, '2025-02-w2', '2025-02-08T00:00:00.000Z');
|
|
56
|
+
testDateFn(ceilDate, '2025-02-03', '2025-02-03T00:00:00.000Z');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('nextDate', () => {
|
|
60
|
+
testDateFn(nextDate, '2025', '2026-01-01T00:00:00.000Z');
|
|
61
|
+
testDateFn(nextDate, '2025-Q1', '2025-04-01T00:00:00.000Z');
|
|
62
|
+
testDateFn(nextDate, '2025-q2', '2025-07-01T00:00:00.000Z');
|
|
63
|
+
testDateFn(nextDate, '2025-Q3', '2025-10-01T00:00:00.000Z');
|
|
64
|
+
testDateFn(nextDate, '2025-q4', '2026-01-01T00:00:00.000Z');
|
|
65
|
+
testDateFn(nextDate, '2025-02', '2025-03-01T00:00:00.000Z');
|
|
66
|
+
testDateFn(nextDate, '2025-02-W1', '2025-02-08T00:00:00.000Z');
|
|
67
|
+
testDateFn(nextDate, '2025-02-w2', '2025-02-15T00:00:00.000Z');
|
|
68
|
+
testDateFn(nextDate, '2025-02-W3', '2025-02-22T00:00:00.000Z');
|
|
69
|
+
testDateFn(nextDate, '2025-02-w4', '2025-03-01T00:00:00.000Z');
|
|
70
|
+
testDateFn(nextDate, '2025-02-03', '2025-02-04T00:00:00.000Z');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('prevDate', () => {
|
|
74
|
+
testDateFn(prevDate, '2025', '2024-01-01T00:00:00.000Z');
|
|
75
|
+
testDateFn(prevDate, '2025-Q1', '2024-10-01T00:00:00.000Z');
|
|
76
|
+
testDateFn(prevDate, '2025-q2', '2025-01-01T00:00:00.000Z');
|
|
77
|
+
testDateFn(prevDate, '2025-Q3', '2025-04-01T00:00:00.000Z');
|
|
78
|
+
testDateFn(prevDate, '2025-q4', '2025-07-01T00:00:00.000Z');
|
|
79
|
+
testDateFn(prevDate, '2025-02', '2025-01-01T00:00:00.000Z');
|
|
80
|
+
testDateFn(prevDate, '2025-02-W1', '2025-01-22T00:00:00.000Z');
|
|
81
|
+
testDateFn(prevDate, '2025-02-w2', '2025-02-01T00:00:00.000Z');
|
|
82
|
+
testDateFn(prevDate, '2025-02-W3', '2025-02-08T00:00:00.000Z');
|
|
83
|
+
testDateFn(prevDate, '2025-02-w4', '2025-02-15T00:00:00.000Z');
|
|
84
|
+
testDateFn(prevDate, '2025-02-03', '2025-02-02T00:00:00.000Z');
|
|
85
|
+
});
|
|
86
|
+
|
|
35
87
|
test.run();
|