@formatjs/intl-durationformat 0.8.1 → 0.8.4

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl-durationformat",
3
3
  "description": "Intl.DurationFormat polyfill",
4
- "version": "0.8.1",
4
+ "version": "0.8.4",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -14,8 +14,8 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "tslib": "^2.8.0",
17
- "@formatjs/ecma402-abstract": "3.0.1",
18
- "@formatjs/intl-localematcher": "0.7.1"
17
+ "@formatjs/ecma402-abstract": "3.0.3",
18
+ "@formatjs/intl-localematcher": "0.7.2"
19
19
  },
20
20
  "bugs": "https://github.com/formatjs/formatjs/issues",
21
21
  "homepage": "https://github.com/formatjs/formatjs#readme",
package/src/core.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import type { DurationFormatLocaleInternalData, DurationFormatPart, DurationFormat as DurationFormatType, DurationInput, ResolvedDurationFormatOptions } from './types.js';
2
2
  import { DurationFormatOptions } from './types.js';
3
+ /**
4
+ * DurationFormat provides locale-sensitive duration formatting
5
+ * Implements the Intl.DurationFormat API from the TC39 proposal
6
+ */
3
7
  export declare class DurationFormat implements DurationFormatType {
4
8
  constructor(locales?: string | string[], options?: DurationFormatOptions);
5
9
  resolvedOptions(): ResolvedDurationFormatOptions;
package/src/core.js CHANGED
@@ -1,3 +1,5 @@
1
+ // Core implementation of Intl.DurationFormat polyfill
2
+ // Follows the TC39 Intl.DurationFormat proposal specification
1
3
  import { CanonicalizeLocaleList, GetNumberOption, GetOption, OrdinaryHasInstance, SupportedLocales, ToObject, invariant, } from '@formatjs/ecma402-abstract';
2
4
  import { ResolveLocale } from '@formatjs/intl-localematcher';
3
5
  import { GetDurationUnitOptions } from './abstract/GetDurationUnitOptions.js';
@@ -6,6 +8,8 @@ import { ToDurationRecord } from './abstract/ToDurationRecord.js';
6
8
  import { getInternalSlots } from './get_internal_slots.js';
7
9
  import { numberingSystemNames } from './numbering-systems.generated.js';
8
10
  import { TIME_SEPARATORS } from './time-separators.generated.js';
11
+ // Keys that should be included in resolvedOptions() output
12
+ // These represent all the configurable options for duration formatting
9
13
  var RESOLVED_OPTIONS_KEYS = [
10
14
  'locale',
11
15
  'style',
@@ -32,6 +36,9 @@ var RESOLVED_OPTIONS_KEYS = [
32
36
  'numberingSystem',
33
37
  'fractionalDigits',
34
38
  ];
39
+ // Configuration table for all duration units (from spec Table 3)
40
+ // Defines the style and display options for each unit, along with their defaults
41
+ // Units are processed in order from largest (years) to smallest (nanoseconds)
35
42
  var TABLE_3 = [
36
43
  {
37
44
  styleSlot: 'years',
@@ -104,45 +111,60 @@ var TABLE_3 = [
104
111
  digitalDefault: 'numeric',
105
112
  },
106
113
  ];
114
+ /**
115
+ * DurationFormat provides locale-sensitive duration formatting
116
+ * Implements the Intl.DurationFormat API from the TC39 proposal
117
+ */
107
118
  var DurationFormat = /** @class */ (function () {
108
119
  function DurationFormat(locales, options) {
120
+ // Ensure constructor is called with 'new' keyword
109
121
  // test262/test/intl402/ListFormat/constructor/constructor/newtarget-undefined.js
110
122
  // Cannot use `new.target` bc of IE11 & TS transpiles it to something else
111
123
  var newTarget = this && this instanceof DurationFormat ? this.constructor : void 0;
112
124
  if (!newTarget) {
113
125
  throw new TypeError("Intl.DurationFormat must be called with 'new'");
114
126
  }
127
+ // Canonicalize the requested locales into a standard format
115
128
  var requestedLocales = CanonicalizeLocaleList(locales);
116
129
  var opt = Object.create(null);
117
130
  var opts = options === undefined ? Object.create(null) : ToObject(options);
131
+ // Get locale matching algorithm preference ('best fit' or 'lookup')
118
132
  var matcher = GetOption(opts, 'localeMatcher', 'string', ['best fit', 'lookup'], 'best fit');
133
+ // Get numbering system (e.g., 'latn', 'arab', 'deva')
119
134
  var numberingSystem = GetOption(opts, 'numberingSystem', 'string', undefined, undefined);
120
135
  if (numberingSystem !== undefined &&
121
136
  numberingSystemNames.indexOf(numberingSystem) < 0) {
122
- // 8.a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal,
123
- // throw a RangeError exception.
137
+ // Validate that the numbering system is recognized
124
138
  throw RangeError("Invalid numberingSystems: ".concat(numberingSystem));
125
139
  }
126
140
  opt.nu = numberingSystem;
127
141
  opt.localeMatcher = matcher;
142
+ // Resolve the best matching locale from available locales
128
143
  var localeData = DurationFormat.localeData, availableLocales = DurationFormat.availableLocales;
129
144
  var r = ResolveLocale(availableLocales, requestedLocales, opt,
130
- // [[RelevantExtensionKeys]] slot, which is a constant
145
+ // [[RelevantExtensionKeys]] slot - only 'nu' (numbering system) is supported
131
146
  ['nu'], localeData, DurationFormat.getDefaultLocale);
132
147
  var locale = r.locale;
148
+ // Initialize internal slots for this formatter instance
133
149
  var internalSlots = getInternalSlots(this);
134
150
  internalSlots.initializedDurationFormat = true;
135
151
  internalSlots.locale = locale;
136
152
  internalSlots.numberingSystem = r.nu;
153
+ // Get overall formatting style: 'long' (3 hours), 'short' (3 hr), 'narrow' (3h), or 'digital' (3:00:00)
137
154
  var style = GetOption(opts, 'style', 'string', ['long', 'short', 'narrow', 'digital'], 'short');
138
155
  internalSlots.style = style;
139
156
  internalSlots.dataLocale = r.dataLocale;
157
+ // Process each duration unit (years through nanoseconds) to determine
158
+ // its display style and when to show it (always/auto/never)
140
159
  var prevStyle = '';
141
160
  TABLE_3.forEach(function (row) {
142
161
  var styleSlot = row.styleSlot, displaySlot = row.displaySlot, unit = row.unit, valueList = row.values, digitalBase = row.digitalDefault;
162
+ // Get the style and display options for this unit
143
163
  var unitOptions = GetDurationUnitOptions(unit, opts, style, valueList, digitalBase, prevStyle);
144
164
  internalSlots[styleSlot] = unitOptions.style;
145
165
  internalSlots[displaySlot] = unitOptions.display;
166
+ // Track the previous style for time units (hours through microseconds)
167
+ // This is used to ensure consistent digital formatting across related units
146
168
  if (unit === 'hours' ||
147
169
  unit === 'minutes' ||
148
170
  unit === 'seconds' ||
@@ -151,6 +173,7 @@ var DurationFormat = /** @class */ (function () {
151
173
  prevStyle = unitOptions.style;
152
174
  }
153
175
  });
176
+ // Get the number of fractional digits for sub-second precision (0-9)
154
177
  internalSlots.fractionalDigits = GetNumberOption(opts, 'fractionalDigits', 0, 9, undefined);
155
178
  }
156
179
  DurationFormat.prototype.resolvedOptions = function () {