@bbn/bbn 2.0.76 → 2.0.78

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.
@@ -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
- if (a.constructor !== b.constructor) {
129
- throw new TypeError('Cannot compare different Temporal types');
130
- }
131
- const Ctor = a.constructor;
132
- // No unit delegate to the built-in static compare
133
- if (unit === undefined) {
134
- if (typeof Ctor.compare !== 'function') {
135
- throw new TypeError('This Temporal type has no static compare');
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
- return Ctor.compare(a, b); // -1, 0, 1
138
- }
139
- // With unit → use .until() and let Temporal validate the unit
140
- if (typeof a.until !== 'function') {
141
- throw new TypeError('This Temporal type does not support until/since');
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
- // If `unit` is invalid for this Temporal type, this will throw RangeError
144
- const diff = a.until(b, { largestUnit: unit });
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);
@@ -154,7 +216,7 @@ export class bbnDt {
154
216
  if (!(other instanceof bbnDt)) {
155
217
  other = bbn.dt(other, null, this.kind);
156
218
  }
157
- return bbnDt.compare(this.value, other.value, unit);
219
+ return bbnDt.compare(this, other, unit);
158
220
  }
159
221
  add(amount, unit) {
160
222
  if (!this.value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.76",
3
+ "version": "2.0.78",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",