@miso.ai/server-commons 0.6.5-beta.16 → 0.6.5-beta.18

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 CHANGED
@@ -23,5 +23,5 @@
23
23
  "uuid": "^9.0.0",
24
24
  "yargs": "^17.5.1"
25
25
  },
26
- "version": "0.6.5-beta.16"
26
+ "version": "0.6.5-beta.18"
27
27
  }
package/src/date.js CHANGED
@@ -52,7 +52,8 @@ export function floorDate(expr, unit) {
52
52
  return undefined;
53
53
  }
54
54
  const [_unit, ts] = parseDateExpr(expr);
55
- if (unit === undefined || !isGranular(unit, _unit)) {
55
+ //console.error('floorDate', expr, unit, _unit, ts, unit === undefined || !isGranular(unit, _unit));
56
+ if (unit === undefined || isGranular(unit, _unit)) {
56
57
  return ts;
57
58
  }
58
59
  return floorTimestamp(ts, unit);
@@ -81,7 +82,7 @@ function floorTimestamp(ts, unit) {
81
82
  case 'month':
82
83
  return Date.UTC(year, month - 1);
83
84
  case 'quarter':
84
- return Date.UTC(year, month - month % 3);
85
+ return Date.UTC(year, month - 1 - (month - 1) % 3);
85
86
  case 'year':
86
87
  return Date.UTC(year);
87
88
  default:
@@ -95,10 +96,21 @@ export function ceilDate(expr, unit) {
95
96
  return undefined;
96
97
  }
97
98
  const [_unit, ts] = parseDateExpr(expr);
98
- return ceilTimestamp(ts, unit || _unit);
99
+ unit = unit || _unit;
100
+ const floored = floorTimestamp(ts, unit);
101
+ return floored === ts ? floored : nextTimestamp(floored, unit);
99
102
  }
100
103
 
101
- function ceilTimestamp(ts, unit) {
104
+ export function nextDate(expr, unit) {
105
+ validateUnit(unit);
106
+ if (expr === undefined) {
107
+ return undefined;
108
+ }
109
+ const [_unit, ts] = parseDateExpr(expr);
110
+ return nextTimestamp(ts + 1, unit || _unit);
111
+ }
112
+
113
+ function nextTimestamp(ts, unit) {
102
114
  switch (unit) {
103
115
  case 'millisecond':
104
116
  return ts;
@@ -129,15 +141,6 @@ function ceilTimestamp(ts, unit) {
129
141
  }
130
142
  }
131
143
 
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
144
  export function prevDate(expr, unit) {
142
145
  validateUnit(unit);
143
146
  if (expr === undefined) {
package/test/date.test.js CHANGED
@@ -1,11 +1,15 @@
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})`);
7
7
  }
8
8
 
9
+ function testDateFnWithUnit(fn, input, unit, output) {
10
+ assert.equal(new Date(fn(input, unit)).toISOString(), output, `${fn.name}(${input}, ${unit})`);
11
+ }
12
+
9
13
  test('startOfDate', () => {
10
14
  testDateFn(startOfDate, '2025', '2025-01-01T00:00:00.000Z');
11
15
  testDateFn(startOfDate, '2025-Q1', '2025-01-01T00:00:00.000Z');
@@ -32,4 +36,76 @@ test('endOfDate', () => {
32
36
  testDateFn(endOfDate, '2025-03-03', '2025-03-03T23:59:59.000Z');
33
37
  });
34
38
 
39
+ test('floorDate', () => {
40
+ testDateFn(floorDate, '2025', '2025-01-01T00:00:00.000Z');
41
+ testDateFn(floorDate, '2025-Q1', '2025-01-01T00:00:00.000Z');
42
+ testDateFn(floorDate, '2025-q2', '2025-04-01T00:00:00.000Z');
43
+ testDateFn(floorDate, '2025-Q3', '2025-07-01T00:00:00.000Z');
44
+ testDateFn(floorDate, '2025-q4', '2025-10-01T00:00:00.000Z');
45
+ testDateFn(floorDate, '2025-02', '2025-02-01T00:00:00.000Z');
46
+ testDateFn(floorDate, '2025-02-W1', '2025-02-01T00:00:00.000Z');
47
+ testDateFn(floorDate, '2025-02-w2', '2025-02-08T00:00:00.000Z');
48
+ testDateFn(floorDate, '2025-02-03', '2025-02-03T00:00:00.000Z');
49
+
50
+ const ts = Date.parse('2025-05-10T04:05:06Z');
51
+ testDateFnWithUnit(floorDate, ts, 'day', '2025-05-10T00:00:00.000Z');
52
+ testDateFnWithUnit(floorDate, ts, 'week', '2025-05-08T00:00:00.000Z');
53
+ testDateFnWithUnit(floorDate, ts, 'month', '2025-05-01T00:00:00.000Z');
54
+ testDateFnWithUnit(floorDate, ts, 'quarter', '2025-04-01T00:00:00.000Z');
55
+ testDateFnWithUnit(floorDate, ts, 'year', '2025-01-01T00:00:00.000Z');
56
+ });
57
+
58
+ test('ceilDate', () => {
59
+ testDateFn(ceilDate, '2025', '2025-01-01T00:00:00.000Z');
60
+ testDateFn(ceilDate, '2025-Q1', '2025-01-01T00:00:00.000Z');
61
+ testDateFn(ceilDate, '2025-q2', '2025-04-01T00:00:00.000Z');
62
+ testDateFn(ceilDate, '2025-Q3', '2025-07-01T00:00:00.000Z');
63
+ testDateFn(ceilDate, '2025-q4', '2025-10-01T00:00:00.000Z');
64
+ testDateFn(ceilDate, '2025-02', '2025-02-01T00:00:00.000Z');
65
+ testDateFn(ceilDate, '2025-02-W1', '2025-02-01T00:00:00.000Z');
66
+ testDateFn(ceilDate, '2025-02-w2', '2025-02-08T00:00:00.000Z');
67
+ testDateFn(ceilDate, '2025-02-03', '2025-02-03T00:00:00.000Z');
68
+ });
69
+
70
+ test('nextDate', () => {
71
+ testDateFn(nextDate, '2025', '2026-01-01T00:00:00.000Z');
72
+ testDateFn(nextDate, '2025-Q1', '2025-04-01T00:00:00.000Z');
73
+ testDateFn(nextDate, '2025-q2', '2025-07-01T00:00:00.000Z');
74
+ testDateFn(nextDate, '2025-Q3', '2025-10-01T00:00:00.000Z');
75
+ testDateFn(nextDate, '2025-q4', '2026-01-01T00:00:00.000Z');
76
+ testDateFn(nextDate, '2025-02', '2025-03-01T00:00:00.000Z');
77
+ testDateFn(nextDate, '2025-02-W1', '2025-02-08T00:00:00.000Z');
78
+ testDateFn(nextDate, '2025-02-w2', '2025-02-15T00:00:00.000Z');
79
+ testDateFn(nextDate, '2025-02-W3', '2025-02-22T00:00:00.000Z');
80
+ testDateFn(nextDate, '2025-02-w4', '2025-03-01T00:00:00.000Z');
81
+ testDateFn(nextDate, '2025-02-03', '2025-02-04T00:00:00.000Z');
82
+ });
83
+
84
+ test('prevDate', () => {
85
+ testDateFn(prevDate, '2025', '2024-01-01T00:00:00.000Z');
86
+ testDateFn(prevDate, '2025-Q1', '2024-10-01T00:00:00.000Z');
87
+ testDateFn(prevDate, '2025-q2', '2025-01-01T00:00:00.000Z');
88
+ testDateFn(prevDate, '2025-Q3', '2025-04-01T00:00:00.000Z');
89
+ testDateFn(prevDate, '2025-q4', '2025-07-01T00:00:00.000Z');
90
+ testDateFn(prevDate, '2025-02', '2025-01-01T00:00:00.000Z');
91
+ testDateFn(prevDate, '2025-02-W1', '2025-01-22T00:00:00.000Z');
92
+ testDateFn(prevDate, '2025-02-w2', '2025-02-01T00:00:00.000Z');
93
+ testDateFn(prevDate, '2025-02-W3', '2025-02-08T00:00:00.000Z');
94
+ testDateFn(prevDate, '2025-02-w4', '2025-02-15T00:00:00.000Z');
95
+ testDateFn(prevDate, '2025-02-03', '2025-02-02T00:00:00.000Z');
96
+
97
+ const ts = Date.parse('2025-05-10T04:05:06Z');
98
+ testDateFnWithUnit(prevDate, ts, 'day', '2025-05-10T00:00:00.000Z');
99
+ testDateFnWithUnit(prevDate, ts, 'week', '2025-05-08T00:00:00.000Z');
100
+ testDateFnWithUnit(prevDate, ts, 'month', '2025-05-01T00:00:00.000Z');
101
+ testDateFnWithUnit(prevDate, ts, 'quarter', '2025-04-01T00:00:00.000Z');
102
+ testDateFnWithUnit(prevDate, ts, 'year', '2025-01-01T00:00:00.000Z');
103
+
104
+ testDateFnWithUnit(prevDate, '2025', 'day', '2024-12-31T00:00:00.000Z');
105
+ testDateFnWithUnit(prevDate, '2025', 'week', '2024-12-22T00:00:00.000Z');
106
+ testDateFnWithUnit(prevDate, '2025', 'month', '2024-12-01T00:00:00.000Z');
107
+ testDateFnWithUnit(prevDate, '2025', 'quarter', '2024-10-01T00:00:00.000Z');
108
+
109
+ });
110
+
35
111
  test.run();