@jskit-ai/database-runtime 0.1.147 → 0.1.148
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.descriptor.mjs +1 -1
- package/package.json +1 -1
- package/src/shared/dateUtils.js +159 -1
- package/src/shared/index.js +4 -1
- package/test/dateUtils.test.js +38 -1
package/package.descriptor.mjs
CHANGED
package/package.json
CHANGED
package/src/shared/dateUtils.js
CHANGED
|
@@ -45,6 +45,155 @@ function pad(value, size = 2) {
|
|
|
45
45
|
return String(value).padStart(size, "0");
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
function requireValidDateParts(year, month, day) {
|
|
49
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
50
|
+
const daysByMonth = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
51
|
+
if (
|
|
52
|
+
!Number.isInteger(year) || year < 0 || year > 9999 ||
|
|
53
|
+
!Number.isInteger(month) || month < 1 || month > 12 ||
|
|
54
|
+
!Number.isInteger(day) || day < 1 || day > daysByMonth[month - 1]
|
|
55
|
+
) {
|
|
56
|
+
throw new TypeError("Invalid date value.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function requireValidTimeParts(hours, minutes, seconds = 0) {
|
|
61
|
+
if (
|
|
62
|
+
!Number.isInteger(hours) || hours < 0 || hours > 23 ||
|
|
63
|
+
!Number.isInteger(minutes) || minutes < 0 || minutes > 59 ||
|
|
64
|
+
!Number.isInteger(seconds) || seconds < 0 || seconds > 59
|
|
65
|
+
) {
|
|
66
|
+
throw new TypeError("Invalid time value.");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseDateParts(value) {
|
|
71
|
+
const match = String(value || "").match(/^(\d{4})-(\d{2})-(\d{2})$/u);
|
|
72
|
+
if (!match) {
|
|
73
|
+
throw new TypeError("Invalid date value.");
|
|
74
|
+
}
|
|
75
|
+
const parts = match.slice(1).map(Number);
|
|
76
|
+
requireValidDateParts(parts[0], parts[1], parts[2]);
|
|
77
|
+
return parts;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizeTemporalPrecision(value) {
|
|
81
|
+
if (value === undefined) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
85
|
+
throw new TypeError("Invalid temporal precision.");
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function formatFraction(milliseconds, temporalPrecision) {
|
|
91
|
+
const precision = normalizeTemporalPrecision(temporalPrecision);
|
|
92
|
+
if (precision === 0) {
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const millisecondDigits = pad(milliseconds, 3);
|
|
97
|
+
if (precision === undefined || precision === 3) {
|
|
98
|
+
return `.${millisecondDigits}`;
|
|
99
|
+
}
|
|
100
|
+
if (precision < 3) {
|
|
101
|
+
const discardedUnit = 10 ** (3 - precision);
|
|
102
|
+
if (milliseconds % discardedUnit !== 0) {
|
|
103
|
+
throw new TypeError("Temporal value exceeds configured precision.");
|
|
104
|
+
}
|
|
105
|
+
return `.${millisecondDigits.slice(0, precision)}`;
|
|
106
|
+
}
|
|
107
|
+
return `.${millisecondDigits}${"0".repeat(precision - 3)}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function requireAllowedFraction(fraction, temporalPrecision) {
|
|
111
|
+
const precision = normalizeTemporalPrecision(temporalPrecision);
|
|
112
|
+
if (precision !== undefined && fraction && fraction.length - 1 > precision) {
|
|
113
|
+
throw new TypeError("Temporal value exceeds configured precision.");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function toJsonDate(value) {
|
|
118
|
+
if (value == null) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (value instanceof Date) {
|
|
122
|
+
const date = toDateOrThrow(value);
|
|
123
|
+
return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const normalized = String(value).trim();
|
|
127
|
+
parseDateParts(normalized);
|
|
128
|
+
return normalized;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function toJsonTime(value, { temporalPrecision } = {}) {
|
|
132
|
+
if (value == null) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (value instanceof Date) {
|
|
136
|
+
const date = toDateOrThrow(value);
|
|
137
|
+
return [
|
|
138
|
+
`${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}`,
|
|
139
|
+
formatFraction(date.getUTCMilliseconds(), temporalPrecision)
|
|
140
|
+
].join("");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const normalized = String(value).trim();
|
|
144
|
+
const match = normalized.match(/^(\d{2}):(\d{2})(?::(\d{2})(\.\d{1,9})?)?$/u);
|
|
145
|
+
if (!match) {
|
|
146
|
+
throw new TypeError("Invalid time value.");
|
|
147
|
+
}
|
|
148
|
+
requireValidTimeParts(Number(match[1]), Number(match[2]), Number(match[3] || 0));
|
|
149
|
+
requireAllowedFraction(match[4], temporalPrecision);
|
|
150
|
+
return normalized;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function toJsonDateTime(value, { temporalPrecision } = {}) {
|
|
154
|
+
if (value == null) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
if (value instanceof Date) {
|
|
158
|
+
const date = toDateOrThrow(value);
|
|
159
|
+
const year = date.getUTCFullYear();
|
|
160
|
+
if (year < 0 || year > 9999) {
|
|
161
|
+
throw new TypeError("Invalid date-time value.");
|
|
162
|
+
}
|
|
163
|
+
return [
|
|
164
|
+
`${pad(year, 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`,
|
|
165
|
+
`T${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}`,
|
|
166
|
+
formatFraction(date.getUTCMilliseconds(), temporalPrecision),
|
|
167
|
+
"Z"
|
|
168
|
+
].join("");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const normalized = String(value).trim();
|
|
172
|
+
const match = normalized.match(
|
|
173
|
+
/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})?$/u
|
|
174
|
+
);
|
|
175
|
+
if (!match) {
|
|
176
|
+
throw new TypeError("Invalid date-time value.");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
requireValidDateParts(Number(match[1]), Number(match[2]), Number(match[3]));
|
|
180
|
+
requireValidTimeParts(Number(match[4]), Number(match[5]), Number(match[6]));
|
|
181
|
+
requireAllowedFraction(match[7], temporalPrecision);
|
|
182
|
+
const offset = match[8] || "";
|
|
183
|
+
if (offset && offset !== "Z") {
|
|
184
|
+
const [offsetHours, offsetMinutes] = offset.slice(1).split(":").map(Number);
|
|
185
|
+
requireValidTimeParts(offsetHours, offsetMinutes, 0);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (normalized.includes("T") && offset) {
|
|
189
|
+
return normalized;
|
|
190
|
+
}
|
|
191
|
+
if (offset) {
|
|
192
|
+
return `${normalized.slice(0, 10)}T${normalized.slice(11)}`;
|
|
193
|
+
}
|
|
194
|
+
return `${normalized.slice(0, 10)}T${normalized.slice(11)}Z`;
|
|
195
|
+
}
|
|
196
|
+
|
|
48
197
|
function toIsoString(value) {
|
|
49
198
|
return toDateOrThrow(value).toISOString();
|
|
50
199
|
}
|
|
@@ -83,4 +232,13 @@ function toDatabaseDateTimeUtc(value) {
|
|
|
83
232
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
|
|
84
233
|
}
|
|
85
234
|
|
|
86
|
-
export {
|
|
235
|
+
export {
|
|
236
|
+
normalizeDateInput,
|
|
237
|
+
toIsoString,
|
|
238
|
+
toInsertDateTime,
|
|
239
|
+
toNullableDateTime,
|
|
240
|
+
toDatabaseDateTimeUtc,
|
|
241
|
+
toJsonDate,
|
|
242
|
+
toJsonTime,
|
|
243
|
+
toJsonDateTime
|
|
244
|
+
};
|
package/src/shared/index.js
CHANGED
|
@@ -11,7 +11,10 @@ export {
|
|
|
11
11
|
toIsoString,
|
|
12
12
|
toInsertDateTime,
|
|
13
13
|
toNullableDateTime,
|
|
14
|
-
toDatabaseDateTimeUtc
|
|
14
|
+
toDatabaseDateTimeUtc,
|
|
15
|
+
toJsonDate,
|
|
16
|
+
toJsonTime,
|
|
17
|
+
toJsonDateTime
|
|
15
18
|
} from "./dateUtils.js";
|
|
16
19
|
export { normalizeDialect, detectDialectFromClient } from "./dialect.js";
|
|
17
20
|
export { normalizeText, normalizeDatabaseClient, toKnexClientId } from "./databaseClient.js";
|
package/test/dateUtils.test.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
toIsoString,
|
|
5
|
+
toDatabaseDateTimeUtc,
|
|
6
|
+
toJsonDate,
|
|
7
|
+
toJsonTime,
|
|
8
|
+
toJsonDateTime
|
|
9
|
+
} from "../src/shared/dateUtils.js";
|
|
4
10
|
|
|
5
11
|
test("toIsoString normalizes valid date input", () => {
|
|
6
12
|
assert.equal(toIsoString("2024-01-01T00:00:00.000Z"), "2024-01-01T00:00:00.000Z");
|
|
@@ -25,3 +31,34 @@ test("date utils throw on invalid date", () => {
|
|
|
25
31
|
assert.throws(() => toIsoString("2024-01-01T00:00:00"), /Invalid date value\./);
|
|
26
32
|
assert.throws(() => toDatabaseDateTimeUtc("not-a-date"), /Invalid date value\./);
|
|
27
33
|
});
|
|
34
|
+
|
|
35
|
+
test("JSON temporal serializers produce json-rest-schema 1.0.17 string shapes", () => {
|
|
36
|
+
assert.equal(toJsonDate("2026-08-13"), "2026-08-13");
|
|
37
|
+
assert.equal(toJsonDate(new Date("2026-08-13T23:59:58.123Z")), "2026-08-13");
|
|
38
|
+
assert.equal(toJsonTime("07:08"), "07:08");
|
|
39
|
+
assert.equal(toJsonTime("07:08:09.123456"), "07:08:09.123456");
|
|
40
|
+
assert.equal(toJsonTime(new Date("2026-08-13T07:08:09.000Z"), { temporalPrecision: 0 }), "07:08:09");
|
|
41
|
+
assert.equal(toJsonDateTime("2026-08-13 07:08:09.123456"), "2026-08-13T07:08:09.123456Z");
|
|
42
|
+
assert.equal(toJsonDateTime("2026-08-13T07:08:09+08:00"), "2026-08-13T07:08:09+08:00");
|
|
43
|
+
assert.equal(toJsonDateTime(new Date("2026-08-13T07:08:09.123Z")), "2026-08-13T07:08:09.123Z");
|
|
44
|
+
assert.equal(
|
|
45
|
+
toJsonDateTime(new Date("2026-08-13T07:08:09.123Z"), { temporalPrecision: 6 }),
|
|
46
|
+
"2026-08-13T07:08:09.123000Z"
|
|
47
|
+
);
|
|
48
|
+
assert.equal(toJsonDateTime(null), null);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("JSON temporal serializers reject invalid or ambiguous values", () => {
|
|
52
|
+
assert.throws(() => toJsonDate("2026-02-30"), /Invalid date value/);
|
|
53
|
+
assert.throws(() => toJsonTime("25:00"), /Invalid time value/);
|
|
54
|
+
assert.throws(() => toJsonDateTime("2026-08-13T07:08"), /Invalid date-time value/);
|
|
55
|
+
assert.throws(() => toJsonDateTime("2026-08-13 07:08:09+25:00"), /Invalid time value/);
|
|
56
|
+
assert.throws(
|
|
57
|
+
() => toJsonDateTime("2026-08-13T07:08:09.123Z", { temporalPrecision: 2 }),
|
|
58
|
+
/exceeds configured precision/
|
|
59
|
+
);
|
|
60
|
+
assert.throws(
|
|
61
|
+
() => toJsonTime(new Date("2026-08-13T07:08:09.123Z"), { temporalPrecision: 2 }),
|
|
62
|
+
/exceeds configured precision/
|
|
63
|
+
);
|
|
64
|
+
});
|