@formatjs/intl-durationformat 0.9.1 → 0.9.2

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
@@ -1 +1 @@
1
- export * from './src/core.js';
1
+ export * from "./src/core.js";
package/index.js CHANGED
@@ -1 +1 @@
1
- export * from './src/core.js';
1
+ export * from "./src/core.js";
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.9.1",
4
+ "version": "0.9.2",
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.7",
18
- "@formatjs/intl-localematcher": "0.7.4"
17
+ "@formatjs/ecma402-abstract": "3.0.8",
18
+ "@formatjs/intl-localematcher": "0.7.5"
19
19
  },
20
20
  "bugs": "https://github.com/formatjs/formatjs/issues",
21
21
  "homepage": "https://github.com/formatjs/formatjs#readme",
package/polyfill-force.js CHANGED
@@ -1,18 +1,10 @@
1
- import { DurationFormat } from './index.js';
2
- if (typeof Intl === 'undefined') {
3
- if (typeof window !== 'undefined') {
4
- Object.defineProperty(window, 'Intl', {
5
- value: {},
6
- });
7
- // @ts-ignore we don't include @types/node so global isn't a thing
8
- }
9
- else if (typeof global !== 'undefined') {
10
- // @ts-ignore we don't include @types/node so global isn't a thing
11
- Object.defineProperty(global, 'Intl', {
12
- value: {},
13
- });
14
- }
1
+ import { DurationFormat } from "./index.js";
2
+ if (typeof Intl === "undefined") {
3
+ if (typeof window !== "undefined") {
4
+ Object.defineProperty(window, "Intl", { value: {} });
5
+ } else if (typeof global !== "undefined") {
6
+ // @ts-ignore we don't include @types/node so global isn't a thing
7
+ Object.defineProperty(global, "Intl", { value: {} });
8
+ }
15
9
  }
16
- Object.defineProperty(Intl, 'DurationFormat', {
17
- value: DurationFormat,
18
- });
10
+ Object.defineProperty(Intl, "DurationFormat", { value: DurationFormat });
package/polyfill.js CHANGED
@@ -1,21 +1,13 @@
1
- import { DurationFormat } from './index.js';
2
- import { shouldPolyfill } from './should-polyfill.js';
3
- if (typeof Intl === 'undefined') {
4
- if (typeof window !== 'undefined') {
5
- Object.defineProperty(window, 'Intl', {
6
- value: {},
7
- });
8
- // @ts-ignore we don't include @types/node so global isn't a thing
9
- }
10
- else if (typeof global !== 'undefined') {
11
- // @ts-ignore we don't include @types/node so global isn't a thing
12
- Object.defineProperty(global, 'Intl', {
13
- value: {},
14
- });
15
- }
1
+ import { DurationFormat } from "./index.js";
2
+ import { shouldPolyfill } from "./should-polyfill.js";
3
+ if (typeof Intl === "undefined") {
4
+ if (typeof window !== "undefined") {
5
+ Object.defineProperty(window, "Intl", { value: {} });
6
+ } else if (typeof global !== "undefined") {
7
+ // @ts-ignore we don't include @types/node so global isn't a thing
8
+ Object.defineProperty(global, "Intl", { value: {} });
9
+ }
16
10
  }
17
11
  if (shouldPolyfill()) {
18
- Object.defineProperty(Intl, 'DurationFormat', {
19
- value: DurationFormat,
20
- });
12
+ Object.defineProperty(Intl, "DurationFormat", { value: DurationFormat });
21
13
  }
@@ -1,3 +1,3 @@
1
1
  export function shouldPolyfill() {
2
- return typeof Intl === 'undefined' || !('DurationFormat' in Intl);
2
+ return typeof Intl === "undefined" || !("DurationFormat" in Intl);
3
3
  }
@@ -1,2 +1,2 @@
1
- import { DurationRecord } from '../types.js';
1
+ import { type DurationRecord } from "../types.js";
2
2
  export declare function DurationRecordSign(record: DurationRecord): -1 | 0 | 1;
@@ -1,13 +1,13 @@
1
- import { TABLE_1 } from '../constants.js';
1
+ import { TABLE_1 } from "../constants.js";
2
+ import "../types.js";
2
3
  export function DurationRecordSign(record) {
3
- for (var _i = 0, TABLE_1_1 = TABLE_1; _i < TABLE_1_1.length; _i++) {
4
- var key = TABLE_1_1[_i];
5
- if (record[key] < 0) {
6
- return -1;
7
- }
8
- if (record[key] > 0) {
9
- return 1;
10
- }
11
- }
12
- return 0;
4
+ for (const key of TABLE_1) {
5
+ if (record[key] < 0) {
6
+ return -1;
7
+ }
8
+ if (record[key] > 0) {
9
+ return 1;
10
+ }
11
+ }
12
+ return 0;
13
13
  }
@@ -1,4 +1,4 @@
1
1
  export declare function GetDurationUnitOptions<T extends object>(unit: keyof T, options: T, baseStyle: Exclude<T[keyof T], undefined>, stylesList: readonly T[keyof T][], digitalBase: Exclude<T[keyof T], undefined>, prevStyle: string): {
2
- style: Exclude<T[keyof T], undefined>;
3
- display: string | Exclude<T[keyof T], undefined>;
2
+ style: Exclude<T[keyof T], undefined>;
3
+ display: string | Exclude<T[keyof T], undefined>;
4
4
  };
@@ -1,43 +1,36 @@
1
- import { GetOption } from '@formatjs/ecma402-abstract';
1
+ import { GetOption } from "@formatjs/ecma402-abstract";
2
2
  export function GetDurationUnitOptions(unit, options, baseStyle, stylesList, digitalBase, prevStyle) {
3
- var style = GetOption(options, unit, 'string', stylesList, undefined);
4
- var displayDefault = 'always';
5
- if (style === undefined) {
6
- if (baseStyle === 'digital') {
7
- if (unit !== 'hours' && unit !== 'minutes' && unit !== 'seconds') {
8
- displayDefault = 'auto';
9
- }
10
- style = digitalBase;
11
- }
12
- else {
13
- displayDefault = 'auto';
14
- if (prevStyle === 'numeric' || prevStyle === '2-digit') {
15
- style = 'numeric';
16
- }
17
- else {
18
- style = baseStyle;
19
- }
20
- }
21
- }
22
- var displayField = "".concat(unit, "Display");
23
- var display = GetOption(options, displayField, 'string', ['always', 'auto'], displayDefault);
24
- if (prevStyle === 'numeric' || prevStyle === '2-digit') {
25
- if (style !== 'numeric' && style !== '2-digit') {
26
- throw new RangeError("Can't mix numeric and non-numeric styles");
27
- }
28
- else if (unit === 'minutes' || unit === 'seconds') {
29
- style = '2-digit';
30
- }
31
- if (style === 'numeric' &&
32
- display === 'always' &&
33
- (unit === 'milliseconds' ||
34
- unit === 'microseconds' ||
35
- unit === 'nanoseconds')) {
36
- throw new RangeError("Can't display milliseconds, microseconds, or nanoseconds in numeric format");
37
- }
38
- }
39
- return {
40
- style: style,
41
- display: display,
42
- };
3
+ let style = GetOption(options, unit, "string", stylesList, undefined);
4
+ let displayDefault = "always";
5
+ if (style === undefined) {
6
+ if (baseStyle === "digital") {
7
+ if (unit !== "hours" && unit !== "minutes" && unit !== "seconds") {
8
+ displayDefault = "auto";
9
+ }
10
+ style = digitalBase;
11
+ } else {
12
+ displayDefault = "auto";
13
+ if (prevStyle === "numeric" || prevStyle === "2-digit") {
14
+ style = "numeric";
15
+ } else {
16
+ style = baseStyle;
17
+ }
18
+ }
19
+ }
20
+ let displayField = `${unit}Display`;
21
+ let display = GetOption(options, displayField, "string", ["always", "auto"], displayDefault);
22
+ if (prevStyle === "numeric" || prevStyle === "2-digit") {
23
+ if (style !== "numeric" && style !== "2-digit") {
24
+ throw new RangeError("Can't mix numeric and non-numeric styles");
25
+ } else if (unit === "minutes" || unit === "seconds") {
26
+ style = "2-digit";
27
+ }
28
+ if (style === "numeric" && display === "always" && (unit === "milliseconds" || unit === "microseconds" || unit === "nanoseconds")) {
29
+ throw new RangeError("Can't display milliseconds, microseconds, or nanoseconds in numeric format");
30
+ }
31
+ }
32
+ return {
33
+ style,
34
+ display
35
+ };
43
36
  }
@@ -1,2 +1,2 @@
1
- import { DurationRecord } from '../types.js';
1
+ import { type DurationRecord } from "../types.js";
2
2
  export declare function IsValidDurationRecord(record: DurationRecord): boolean;
@@ -1,18 +1,18 @@
1
- import { invariant } from '@formatjs/ecma402-abstract';
2
- import { TABLE_1 } from '../constants.js';
3
- import { DurationRecordSign } from './DurationRecordSign.js';
1
+ import { invariant } from "@formatjs/ecma402-abstract";
2
+ import { TABLE_1 } from "../constants.js";
3
+ import "../types.js";
4
+ import { DurationRecordSign } from "./DurationRecordSign.js";
4
5
  export function IsValidDurationRecord(record) {
5
- var sign = DurationRecordSign(record);
6
- for (var _i = 0, TABLE_1_1 = TABLE_1; _i < TABLE_1_1.length; _i++) {
7
- var key = TABLE_1_1[_i];
8
- var v = record[key];
9
- invariant(isFinite(Number(v)), "".concat(key, " is not finite"));
10
- if (v < 0 && sign > 0) {
11
- return false;
12
- }
13
- if (v > 0 && sign < 0) {
14
- return false;
15
- }
16
- }
17
- return true;
6
+ const sign = DurationRecordSign(record);
7
+ for (const key of TABLE_1) {
8
+ const v = record[key];
9
+ invariant(isFinite(Number(v)), `${key} is not finite`);
10
+ if (v < 0 && sign > 0) {
11
+ return false;
12
+ }
13
+ if (v > 0 && sign < 0) {
14
+ return false;
15
+ }
16
+ }
17
+ return true;
18
18
  }
@@ -1,3 +1,3 @@
1
- import { DurationFormat } from '../core.js';
2
- import { DurationFormatPart, DurationRecord } from '../types.js';
1
+ import { DurationFormat } from "../core.js";
2
+ import { type DurationFormatPart, type DurationRecord } from "../types.js";
3
3
  export declare function PartitionDurationFormatPattern(df: DurationFormat, duration: DurationRecord): DurationFormatPart[];
@@ -1,149 +1,129 @@
1
- import { createMemoizedListFormat, createMemoizedNumberFormat, invariant, } from '@formatjs/ecma402-abstract';
2
- import { TABLE_2 } from '../constants.js';
3
- import { DurationFormat } from '../core.js';
4
- import { getInternalSlots } from '../get_internal_slots.js';
1
+ import { createMemoizedListFormat, createMemoizedNumberFormat, invariant } from "@formatjs/ecma402-abstract";
2
+ import { TABLE_2 } from "../constants.js";
3
+ import { DurationFormat } from "../core.js";
4
+ import { getInternalSlots } from "../get_internal_slots.js";
5
+ import "../types.js";
5
6
  export function PartitionDurationFormatPattern(df, duration) {
6
- var result = [];
7
- var done = false;
8
- var separated = false;
9
- var internalSlots = getInternalSlots(df);
10
- var dataLocale = internalSlots.dataLocale;
11
- var dataLocaleData = DurationFormat.localeData[dataLocale];
12
- if (!dataLocaleData) {
13
- throw new TypeError('Invalid locale');
14
- }
15
- var numberingSystem = internalSlots.numberingSystem;
16
- var separator = dataLocaleData.digitalFormat[numberingSystem];
17
- var _loop_1 = function (i) {
18
- var row = TABLE_2[i];
19
- var value = duration[row.valueField];
20
- var style = internalSlots[row.styleSlot];
21
- var display = internalSlots[row.displaySlot];
22
- var unit = row.unit, numberFormatUnit = row.numberFormatUnit;
23
- var nfOpts = Object.create(null);
24
- if (unit === 'seconds' ||
25
- unit === 'milliseconds' ||
26
- unit === 'microseconds') {
27
- var nextStyle = void 0;
28
- if (unit === 'seconds') {
29
- nextStyle = internalSlots.milliseconds;
30
- }
31
- else if (unit === 'milliseconds') {
32
- nextStyle = internalSlots.microseconds;
33
- }
34
- else {
35
- nextStyle = internalSlots.nanoseconds;
36
- }
37
- if (nextStyle === 'numeric') {
38
- if (unit === 'seconds') {
39
- value +=
40
- duration.milliseconds / 1e3 +
41
- duration.microseconds / 1e6 +
42
- duration.nanoseconds / 1e9;
43
- }
44
- else if (unit === 'milliseconds') {
45
- value += duration.microseconds / 1e3 + duration.nanoseconds / 1e6;
46
- }
47
- else {
48
- value += duration.nanoseconds / 1e3;
49
- }
50
- if (internalSlots.fractionalDigits === undefined) {
51
- nfOpts.maximumFractionDigits = 9;
52
- nfOpts.minimumFractionDigits = 0;
53
- }
54
- else {
55
- nfOpts.maximumFractionDigits = internalSlots.fractionalDigits;
56
- nfOpts.minimumFractionDigits = internalSlots.fractionalDigits;
57
- }
58
- nfOpts.roundingMode = 'trunc';
59
- done = true;
60
- }
61
- }
62
- if (value !== 0 || display !== 'auto') {
63
- nfOpts.numberingSystem = internalSlots.numberingSystem;
64
- if (style === '2-digit') {
65
- nfOpts.minimumIntegerDigits = 2;
66
- }
67
- if (style !== '2-digit' && style !== 'numeric') {
68
- nfOpts.style = 'unit';
69
- nfOpts.unit = numberFormatUnit;
70
- nfOpts.unitDisplay = style;
71
- }
72
- var nf = createMemoizedNumberFormat(internalSlots.locale, nfOpts);
73
- var list_1;
74
- if (!separated) {
75
- list_1 = [];
76
- }
77
- else {
78
- list_1 = result[result.length - 1];
79
- list_1.push({
80
- type: 'literal',
81
- value: separator,
82
- });
83
- }
84
- var parts = nf.formatToParts(value);
85
- parts.forEach(function (_a) {
86
- var type = _a.type, value = _a.value;
87
- list_1.push({
88
- type: type,
89
- value: value,
90
- unit: numberFormatUnit,
91
- });
92
- });
93
- if (!separated) {
94
- if (style === '2-digit' || style === 'numeric') {
95
- separated = true;
96
- }
97
- result.push(list_1);
98
- }
99
- }
100
- else {
101
- separated = false;
102
- }
103
- };
104
- for (var i = 0; i < TABLE_2.length && !done; i++) {
105
- _loop_1(i);
106
- }
107
- var lfOpts = Object.create(null);
108
- lfOpts.type = 'unit';
109
- var listStyle = internalSlots.style;
110
- if (listStyle === 'digital') {
111
- listStyle = 'short';
112
- }
113
- lfOpts.style = listStyle;
114
- var lf = createMemoizedListFormat(internalSlots.locale, lfOpts);
115
- var strings = [];
116
- for (var _i = 0, result_1 = result; _i < result_1.length; _i++) {
117
- var parts = result_1[_i];
118
- var string = '';
119
- for (var _a = 0, parts_1 = parts; _a < parts_1.length; _a++) {
120
- var value = parts_1[_a].value;
121
- string += value;
122
- }
123
- strings.push(string);
124
- }
125
- var formatted = lf.formatToParts(strings);
126
- var resultIndex = 0;
127
- var resultLength = result.length;
128
- var flattened = [];
129
- for (var _b = 0, formatted_1 = formatted; _b < formatted_1.length; _b++) {
130
- var _c = formatted_1[_b], type = _c.type, value = _c.value;
131
- if (type === 'element') {
132
- invariant(resultIndex < resultLength, 'Index out of bounds');
133
- var parts = result[resultIndex];
134
- for (var _d = 0, parts_2 = parts; _d < parts_2.length; _d++) {
135
- var part = parts_2[_d];
136
- flattened.push(part);
137
- }
138
- resultIndex++;
139
- }
140
- else {
141
- invariant(type === 'literal', 'Type must be literal');
142
- flattened.push({
143
- type: 'literal',
144
- value: value,
145
- });
146
- }
147
- }
148
- return flattened;
7
+ const result = [];
8
+ let done = false;
9
+ let separated = false;
10
+ const internalSlots = getInternalSlots(df);
11
+ let dataLocale = internalSlots.dataLocale;
12
+ const dataLocaleData = DurationFormat.localeData[dataLocale];
13
+ if (!dataLocaleData) {
14
+ throw new TypeError("Invalid locale");
15
+ }
16
+ const numberingSystem = internalSlots.numberingSystem;
17
+ const separator = dataLocaleData.digitalFormat[numberingSystem];
18
+ for (let i = 0; i < TABLE_2.length && !done; i++) {
19
+ const row = TABLE_2[i];
20
+ let value = duration[row.valueField];
21
+ const style = internalSlots[row.styleSlot];
22
+ const display = internalSlots[row.displaySlot];
23
+ const { unit, numberFormatUnit } = row;
24
+ const nfOpts = Object.create(null);
25
+ if (unit === "seconds" || unit === "milliseconds" || unit === "microseconds") {
26
+ let nextStyle;
27
+ if (unit === "seconds") {
28
+ nextStyle = internalSlots.milliseconds;
29
+ } else if (unit === "milliseconds") {
30
+ nextStyle = internalSlots.microseconds;
31
+ } else {
32
+ nextStyle = internalSlots.nanoseconds;
33
+ }
34
+ if (nextStyle === "numeric") {
35
+ if (unit === "seconds") {
36
+ value += duration.milliseconds / 1e3 + duration.microseconds / 1e6 + duration.nanoseconds / 1e9;
37
+ } else if (unit === "milliseconds") {
38
+ value += duration.microseconds / 1e3 + duration.nanoseconds / 1e6;
39
+ } else {
40
+ value += duration.nanoseconds / 1e3;
41
+ }
42
+ if (internalSlots.fractionalDigits === undefined) {
43
+ nfOpts.maximumFractionDigits = 9;
44
+ nfOpts.minimumFractionDigits = 0;
45
+ } else {
46
+ nfOpts.maximumFractionDigits = internalSlots.fractionalDigits;
47
+ nfOpts.minimumFractionDigits = internalSlots.fractionalDigits;
48
+ }
49
+ nfOpts.roundingMode = "trunc";
50
+ done = true;
51
+ }
52
+ }
53
+ if (value !== 0 || display !== "auto") {
54
+ nfOpts.numberingSystem = internalSlots.numberingSystem;
55
+ if (style === "2-digit") {
56
+ nfOpts.minimumIntegerDigits = 2;
57
+ }
58
+ if (style !== "2-digit" && style !== "numeric") {
59
+ nfOpts.style = "unit";
60
+ nfOpts.unit = numberFormatUnit;
61
+ nfOpts.unitDisplay = style;
62
+ }
63
+ const nf = createMemoizedNumberFormat(internalSlots.locale, nfOpts);
64
+ let list;
65
+ if (!separated) {
66
+ list = [];
67
+ } else {
68
+ list = result[result.length - 1];
69
+ list.push({
70
+ type: "literal",
71
+ value: separator
72
+ });
73
+ }
74
+ let parts = nf.formatToParts(value);
75
+ parts.forEach(({ type, value }) => {
76
+ list.push({
77
+ type,
78
+ value,
79
+ unit: numberFormatUnit
80
+ });
81
+ });
82
+ if (!separated) {
83
+ if (style === "2-digit" || style === "numeric") {
84
+ separated = true;
85
+ }
86
+ result.push(list);
87
+ }
88
+ } else {
89
+ separated = false;
90
+ }
91
+ }
92
+ const lfOpts = Object.create(null);
93
+ lfOpts.type = "unit";
94
+ let listStyle = internalSlots.style;
95
+ if (listStyle === "digital") {
96
+ listStyle = "short";
97
+ }
98
+ lfOpts.style = listStyle;
99
+ const lf = createMemoizedListFormat(internalSlots.locale, lfOpts);
100
+ const strings = [];
101
+ for (const parts of result) {
102
+ let string = "";
103
+ for (const { value } of parts) {
104
+ string += value;
105
+ }
106
+ strings.push(string);
107
+ }
108
+ let formatted = lf.formatToParts(strings);
109
+ let resultIndex = 0;
110
+ let resultLength = result.length;
111
+ let flattened = [];
112
+ for (const { type, value } of formatted) {
113
+ if (type === "element") {
114
+ invariant(resultIndex < resultLength, "Index out of bounds");
115
+ let parts = result[resultIndex];
116
+ for (const part of parts) {
117
+ flattened.push(part);
118
+ }
119
+ resultIndex++;
120
+ } else {
121
+ invariant(type === "literal", "Type must be literal");
122
+ flattened.push({
123
+ type: "literal",
124
+ value
125
+ });
126
+ }
127
+ }
128
+ return flattened;
149
129
  }
@@ -1,2 +1,2 @@
1
- import { DurationInput, DurationRecord } from '../types.js';
1
+ import { type DurationInput, type DurationRecord } from "../types.js";
2
2
  export declare function ToDurationRecord(input: DurationInput): DurationRecord;