@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/intl-durationformat",
3
- "version": "0.12.3",
3
+ "version": "0.13.0",
4
4
  "description": "Intl.DurationFormat polyfill",
5
5
  "keywords": [
6
6
  "duration",
@@ -21,6 +21,10 @@
21
21
  "url": "formatjs/formatjs.git"
22
22
  },
23
23
  "type": "module",
24
+ "sideEffects": [
25
+ "./polyfill.js",
26
+ "./polyfill-force.js"
27
+ ],
24
28
  "types": "index.d.ts",
25
29
  "exports": {
26
30
  ".": "./index.js",
package/polyfill-force.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
@@ -616,9 +616,7 @@ function ToDurationRecord(input) {
616
616
  if (!IsValidDurationRecord(result)) throw new RangeError("Invalid duration format");
617
617
  return result;
618
618
  }
619
- // packages/intl-durationformat/abstract/ToTemporalDurationRecord.ts
620
- const temporalDuration = globalThis.Temporal?.Duration;
621
- const durationFrom = temporalDuration?.from;
619
+ // packages/ecma402-abstract/DurationFormat/ToTemporalDurationRecord.js
622
620
  const fields = [
623
621
  "years",
624
622
  "months",
@@ -631,19 +629,38 @@ const fields = [
631
629
  "microseconds",
632
630
  "nanoseconds"
633
631
  ];
634
- const getters = temporalDuration && fields.map((field) => Object.getOwnPropertyDescriptor(temporalDuration.prototype, field).get);
632
+ const apply = Reflect.apply;
633
+ const readers = [];
634
+ function registerTemporalDurationNamespace(namespace) {
635
+ const constructor = namespace?.Duration;
636
+ if (!constructor || readers.some((reader) => reader.constructor === constructor)) return;
637
+ const from = constructor.from, prototype = constructor.prototype;
638
+ if (typeof from !== "function" || prototype === null || typeof prototype !== "object" && typeof prototype !== "function") return;
639
+ const getters = fields.map((field) => Object.getOwnPropertyDescriptor(prototype, field)?.get);
640
+ if (getters.some((getter) => typeof getter !== "function")) return;
641
+ readers.push({
642
+ constructor,
643
+ from,
644
+ getters
645
+ });
646
+ }
647
+ const temporal = globalThis.Temporal;
648
+ if (temporal) registerTemporalDurationNamespace(temporal);
635
649
  function ToTemporalDurationRecord(input) {
636
- if (typeof input === "string" && durationFrom) input = durationFrom.call(temporalDuration, input);
637
- if (getters && input !== null && (typeof input === "object" || typeof input === "function")) {
650
+ if (typeof input === "string" && readers.length) {
651
+ const reader = readers[readers.length - 1];
652
+ input = apply(reader.from, reader.constructor, [input]);
653
+ }
654
+ if (input !== null && (typeof input === "object" || typeof input === "function")) for (const reader of readers) {
638
655
  let years;
639
656
  try {
640
- years = getters[0].call(input);
657
+ years = apply(reader.getters[0], input, []);
641
658
  } catch {
642
- return ToDurationRecord(input);
659
+ continue;
643
660
  }
644
661
  const record = Object.create(null);
645
662
  record.years = years;
646
- for (let i = 1; i < fields.length; i++) record[fields[i]] = getters[i].call(input);
663
+ for (let index = 1; index < fields.length; index++) record[fields[index]] = apply(reader.getters[index], input, []);
647
664
  return record;
648
665
  }
649
666
  return ToDurationRecord(input);
@@ -1524,6 +1541,9 @@ const TABLE_3 = [
1524
1541
  * Implements the Intl.DurationFormat API from the TC39 proposal
1525
1542
  */
1526
1543
  var DurationFormat = class DurationFormat {
1544
+ static __addTemporalNamespace(namespace) {
1545
+ registerTemporalDurationNamespace(namespace);
1546
+ }
1527
1547
  constructor(locales, options) {
1528
1548
  if (!(this && this instanceof DurationFormat ? this.constructor : void 0)) throw new TypeError("Intl.DurationFormat must be called with 'new'");
1529
1549
  const requestedLocales = CanonicalizeLocaleList(locales);