@formatjs/intl-durationformat 0.12.3 → 0.13.0

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/index.d.ts CHANGED
@@ -61,6 +61,12 @@ interface DurationFormat$1 {
61
61
  formatToParts(duration: DurationInput | string): DurationFormatPart[];
62
62
  format(duration: DurationInput | string): string;
63
63
  }
64
+ interface TemporalDurationNamespace {
65
+ Duration: {
66
+ prototype: object;
67
+ from(value: string): object;
68
+ };
69
+ }
64
70
  interface RawDurationLocaleData {
65
71
  data: DurationFormatLocaleInternalData;
66
72
  locale: string;
@@ -112,6 +118,7 @@ interface DurationFormatConstructor {
112
118
  * Implements the Intl.DurationFormat API from the TC39 proposal
113
119
  */
114
120
  export declare class DurationFormat implements DurationFormat$1 {
121
+ static __addTemporalNamespace(namespace: TemporalDurationNamespace): void;
115
122
  constructor(locales?: string | string[], options?: DurationFormatOptions);
116
123
  resolvedOptions(): ResolvedDurationFormatOptions;
117
124
  formatToParts(duration: DurationInput | string): DurationFormatPart[];
@@ -123,5 +130,4 @@ export declare class DurationFormat implements DurationFormat$1 {
123
130
  static getDefaultLocale: () => string;
124
131
  static polyfilled: boolean;
125
132
  }
126
- export type { DurationFormatConstructor, DurationFormatLocaleInternalData, DurationFormatOptions, DurationFormatPart, DurationInput, DurationRecord, IntlDurationFormatInternal, RawDurationLocaleData, ResolvedDurationFormatOptions };
127
- //# sourceMappingURL=index.d.ts.map
133
+ export type { DurationFormatConstructor, DurationFormatLocaleInternalData, DurationFormatOptions, DurationFormatPart, DurationInput, DurationRecord, IntlDurationFormatInternal, RawDurationLocaleData, ResolvedDurationFormatOptions };
package/index.js CHANGED
@@ -115,9 +115,9 @@ function IsCallable(fn) {
115
115
  function OrdinaryHasInstance(C, O, internalSlots) {
116
116
  if (!IsCallable(C)) return false;
117
117
  if (internalSlots?.boundTargetFunction) return O instanceof internalSlots?.boundTargetFunction;
118
- if (typeof O !== "object") return false;
118
+ if (O === null || typeof O !== "object" && typeof O !== "function") return false;
119
119
  let P = C.prototype;
120
- if (typeof P !== "object") throw new TypeError("OrdinaryHasInstance called on an object with an invalid prototype property.");
120
+ if (P === null || typeof P !== "object" && typeof P !== "function") throw new TypeError("OrdinaryHasInstance called on an object with an invalid prototype property.");
121
121
  return Object.prototype.isPrototypeOf.call(P, O);
122
122
  }
123
123
  // packages/ecma262-abstract/ToObject.js
@@ -599,9 +599,7 @@ function ToDurationRecord(input) {
599
599
  if (!IsValidDurationRecord(result)) throw new RangeError("Invalid duration format");
600
600
  return result;
601
601
  }
602
- // packages/intl-durationformat/abstract/ToTemporalDurationRecord.ts
603
- const temporalDuration = globalThis.Temporal?.Duration;
604
- const durationFrom = temporalDuration?.from;
602
+ // packages/ecma402-abstract/DurationFormat/ToTemporalDurationRecord.js
605
603
  const fields = [
606
604
  "years",
607
605
  "months",
@@ -614,19 +612,38 @@ const fields = [
614
612
  "microseconds",
615
613
  "nanoseconds"
616
614
  ];
617
- const getters = temporalDuration && fields.map((field) => Object.getOwnPropertyDescriptor(temporalDuration.prototype, field).get);
615
+ const apply = Reflect.apply;
616
+ const readers = [];
617
+ function registerTemporalDurationNamespace(namespace) {
618
+ const constructor = namespace?.Duration;
619
+ if (!constructor || readers.some((reader) => reader.constructor === constructor)) return;
620
+ const from = constructor.from, prototype = constructor.prototype;
621
+ if (typeof from !== "function" || prototype === null || typeof prototype !== "object" && typeof prototype !== "function") return;
622
+ const getters = fields.map((field) => Object.getOwnPropertyDescriptor(prototype, field)?.get);
623
+ if (getters.some((getter) => typeof getter !== "function")) return;
624
+ readers.push({
625
+ constructor,
626
+ from,
627
+ getters
628
+ });
629
+ }
630
+ const temporal = globalThis.Temporal;
631
+ if (temporal) registerTemporalDurationNamespace(temporal);
618
632
  function ToTemporalDurationRecord(input) {
619
- if (typeof input === "string" && durationFrom) input = durationFrom.call(temporalDuration, input);
620
- if (getters && input !== null && (typeof input === "object" || typeof input === "function")) {
633
+ if (typeof input === "string" && readers.length) {
634
+ const reader = readers[readers.length - 1];
635
+ input = apply(reader.from, reader.constructor, [input]);
636
+ }
637
+ if (input !== null && (typeof input === "object" || typeof input === "function")) for (const reader of readers) {
621
638
  let years;
622
639
  try {
623
- years = getters[0].call(input);
640
+ years = apply(reader.getters[0], input, []);
624
641
  } catch {
625
- return ToDurationRecord(input);
642
+ continue;
626
643
  }
627
644
  const record = Object.create(null);
628
645
  record.years = years;
629
- for (let i = 1; i < fields.length; i++) record[fields[i]] = getters[i].call(input);
646
+ for (let index = 1; index < fields.length; index++) record[fields[index]] = apply(reader.getters[index], input, []);
630
647
  return record;
631
648
  }
632
649
  return ToDurationRecord(input);
@@ -1507,6 +1524,9 @@ const TABLE_3 = [
1507
1524
  * Implements the Intl.DurationFormat API from the TC39 proposal
1508
1525
  */
1509
1526
  var DurationFormat = class DurationFormat {
1527
+ static __addTemporalNamespace(namespace) {
1528
+ registerTemporalDurationNamespace(namespace);
1529
+ }
1510
1530
  constructor(locales, options) {
1511
1531
  if (!(this && this instanceof DurationFormat ? this.constructor : void 0)) throw new TypeError("Intl.DurationFormat must be called with 'new'");
1512
1532
  const requestedLocales = CanonicalizeLocaleList(locales);