@bbn/bbn 2.0.75 → 2.0.77
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/dist/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/dt/classes/dt.js +86 -21
- package/package.json +1 -1
package/dist/dt/classes/dt.js
CHANGED
|
@@ -125,24 +125,86 @@ export class bbnDt {
|
|
|
125
125
|
if (!a || !b) {
|
|
126
126
|
throw new TypeError('Both arguments must be Temporal values');
|
|
127
127
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
128
|
+
const tz = Temporal.Now.timeZoneId();
|
|
129
|
+
const realUnit = unitsCorrespondence[unit] ? getRow(units, d => d[0] === unitsCorrespondence[unit])[1] : undefined;
|
|
130
|
+
const isBbnDt = (x) => x instanceof bbnDt;
|
|
131
|
+
// --- helper: get underlying Temporal value if wrapper ---
|
|
132
|
+
const unwrap = (x) => isBbnDt(x) ? x.value : x;
|
|
133
|
+
// --- helper: normalized ZonedDateTime for comparison ---
|
|
134
|
+
const toZdt = (x) => {
|
|
135
|
+
// x may be a bbnDt* or a raw Temporal
|
|
136
|
+
if (isBbnDt(x)) {
|
|
137
|
+
switch (x.kind) {
|
|
138
|
+
case 'zoned': {
|
|
139
|
+
return x.value;
|
|
140
|
+
}
|
|
141
|
+
case 'datetime': {
|
|
142
|
+
const v = x.value;
|
|
143
|
+
const iso = `${v.toString()}[${tz}]`;
|
|
144
|
+
return Temporal.ZonedDateTime.from(iso);
|
|
145
|
+
}
|
|
146
|
+
case 'date': {
|
|
147
|
+
const d = x.value;
|
|
148
|
+
const iso = `${d.toString()}T00:00[${tz}]`;
|
|
149
|
+
return Temporal.ZonedDateTime.from(iso);
|
|
150
|
+
}
|
|
151
|
+
case 'year-month': {
|
|
152
|
+
const ym = x.value;
|
|
153
|
+
const d = ym.toPlainDate({ day: 1 });
|
|
154
|
+
const iso = `${d.toString()}T00:00[${tz}]`;
|
|
155
|
+
return Temporal.ZonedDateTime.from(iso);
|
|
156
|
+
}
|
|
157
|
+
default:
|
|
158
|
+
throw new TypeError(`Cannot convert kind '${x.kind}' to ZonedDateTime for comparison`);
|
|
159
|
+
}
|
|
136
160
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
throw new TypeError('
|
|
161
|
+
// If it's already a Temporal.ZonedDateTime, keep it
|
|
162
|
+
if (x instanceof Temporal.ZonedDateTime) {
|
|
163
|
+
return x;
|
|
164
|
+
}
|
|
165
|
+
throw new TypeError('toZdt expects a bbnDt wrapper or ZonedDateTime');
|
|
166
|
+
};
|
|
167
|
+
// ---- CASE 1: same constructor → your original logic on raw Temporal ----
|
|
168
|
+
const rawA = unwrap(a);
|
|
169
|
+
const rawB = unwrap(b);
|
|
170
|
+
if (rawA.constructor === rawB.constructor) {
|
|
171
|
+
const Ctor = rawA.constructor;
|
|
172
|
+
if (realUnit === undefined) {
|
|
173
|
+
if (typeof Ctor.compare !== 'function') {
|
|
174
|
+
throw new TypeError('This Temporal type has no static compare');
|
|
175
|
+
}
|
|
176
|
+
return Ctor.compare(rawA, rawB); // -1, 0, 1
|
|
177
|
+
}
|
|
178
|
+
if (typeof rawA.until !== 'function') {
|
|
179
|
+
throw new TypeError('This Temporal type does not support until/since');
|
|
180
|
+
}
|
|
181
|
+
const diff = rawA.until(rawB, { largestUnit: realUnit });
|
|
182
|
+
return diff.sign;
|
|
183
|
+
}
|
|
184
|
+
// ---- CASE 2: different constructors, but convertible bbnDt kinds ----
|
|
185
|
+
const convertibleKinds = new Set([
|
|
186
|
+
'zoned',
|
|
187
|
+
'datetime',
|
|
188
|
+
'date',
|
|
189
|
+
'year-month',
|
|
190
|
+
]);
|
|
191
|
+
if (isBbnDt(a) &&
|
|
192
|
+
isBbnDt(b) &&
|
|
193
|
+
convertibleKinds.has(a.kind) &&
|
|
194
|
+
convertibleKinds.has(b.kind)) {
|
|
195
|
+
const za = toZdt(a);
|
|
196
|
+
const zb = toZdt(b);
|
|
197
|
+
if (realUnit === undefined) {
|
|
198
|
+
return Temporal.ZonedDateTime.compare(za, zb);
|
|
199
|
+
}
|
|
200
|
+
if (typeof za.until !== 'function') {
|
|
201
|
+
throw new TypeError('ZonedDateTime does not support until/since');
|
|
202
|
+
}
|
|
203
|
+
const diff = za.until(zb, { largestUnit: realUnit });
|
|
204
|
+
return diff.sign;
|
|
142
205
|
}
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
return diff.sign; // -1 / 0 / 1
|
|
206
|
+
// ---- CASE 3: not compatible ----
|
|
207
|
+
throw new TypeError('Cannot compare different Temporal or bbnDt types');
|
|
146
208
|
}
|
|
147
209
|
static parse(input, format, cls = 'auto', locale) {
|
|
148
210
|
return parse(input, format, cls, locale);
|
|
@@ -331,19 +393,22 @@ export class bbnDt {
|
|
|
331
393
|
if (!('year' in this.value) || !('month' in this.value) || !('day' in this.value)) {
|
|
332
394
|
throw new Error('date() is not supported for this type');
|
|
333
395
|
}
|
|
334
|
-
|
|
396
|
+
if (0 in arguments && (v !== undefined) && !(v instanceof Event)) {
|
|
397
|
+
return this.parse(v, 'Y-m-d');
|
|
398
|
+
}
|
|
399
|
+
return this.YYYY + '-' + this.MM + '-' + this.DD;
|
|
335
400
|
}
|
|
336
401
|
datetime(v) {
|
|
337
402
|
if (0 in arguments && (v !== undefined) && !(v instanceof Event)) {
|
|
338
403
|
return this.parse(v, 'Y-m-d H:i:s');
|
|
339
404
|
}
|
|
340
|
-
return this.
|
|
405
|
+
return this.YYYY + '-' + this.MM + '-' + this.DD + ' ' + this.HH + ':' + this.II + ':' + this.SS;
|
|
341
406
|
}
|
|
342
407
|
time(v) {
|
|
343
408
|
if (0 in arguments && (v !== undefined) && !(v instanceof Event)) {
|
|
344
409
|
return this.parse(v, 'H:i:s');
|
|
345
410
|
}
|
|
346
|
-
return this.
|
|
411
|
+
return this.HH + ':' + this.II + ':' + this.SS;
|
|
347
412
|
}
|
|
348
413
|
week() {
|
|
349
414
|
if (!this.value) {
|
|
@@ -368,13 +433,13 @@ export class bbnDt {
|
|
|
368
433
|
}
|
|
369
434
|
get MMMM() {
|
|
370
435
|
if ('month' in this.value) {
|
|
371
|
-
return this.
|
|
436
|
+
return bbn.dt.locales.monthsLong[this.month() - 1];
|
|
372
437
|
}
|
|
373
438
|
return undefined;
|
|
374
439
|
}
|
|
375
440
|
get MMM() {
|
|
376
441
|
if ('month' in this.value) {
|
|
377
|
-
return this.
|
|
442
|
+
return bbn.dt.locales.monthsShort[this.month() - 1];
|
|
378
443
|
}
|
|
379
444
|
return undefined;
|
|
380
445
|
}
|