UNPKG

2.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PartitionRelativeTimePattern = void 0;
4var ecma402_abstract_1 = require("@formatjs/ecma402-abstract");
5var SingularRelativeTimeUnit_1 = require("./SingularRelativeTimeUnit");
6var MakePartsList_1 = require("./MakePartsList");
7function PartitionRelativeTimePattern(rtf, value, unit, _a) {
8 var getInternalSlots = _a.getInternalSlots;
9 (0, ecma402_abstract_1.invariant)((0, ecma402_abstract_1.Type)(value) === 'Number', "value must be number, instead got ".concat(typeof value), TypeError);
10 (0, ecma402_abstract_1.invariant)((0, ecma402_abstract_1.Type)(unit) === 'String', "unit must be number, instead got ".concat(typeof value), TypeError);
11 if (isNaN(value) || !isFinite(value)) {
12 throw new RangeError("Invalid value ".concat(value));
13 }
14 var resolvedUnit = (0, SingularRelativeTimeUnit_1.SingularRelativeTimeUnit)(unit);
15 var _b = getInternalSlots(rtf), fields = _b.fields, style = _b.style, numeric = _b.numeric, pluralRules = _b.pluralRules, numberFormat = _b.numberFormat;
16 var entry = resolvedUnit;
17 if (style === 'short') {
18 entry = "".concat(resolvedUnit, "-short");
19 }
20 else if (style === 'narrow') {
21 entry = "".concat(resolvedUnit, "-narrow");
22 }
23 if (!(entry in fields)) {
24 entry = resolvedUnit;
25 }
26 var patterns = fields[entry];
27 if (numeric === 'auto') {
28 if ((0, ecma402_abstract_1.ToString)(value) in patterns) {
29 return [
30 {
31 type: 'literal',
32 value: patterns[(0, ecma402_abstract_1.ToString)(value)],
33 },
34 ];
35 }
36 }
37 var tl = 'future';
38 if ((0, ecma402_abstract_1.SameValue)(value, -0) || value < 0) {
39 tl = 'past';
40 }
41 var po = patterns[tl];
42 var fv = typeof numberFormat.formatToParts === 'function'
43 ? numberFormat.formatToParts(Math.abs(value))
44 : // TODO: If formatToParts is not supported, we assume the whole formatted
45 // number is a part
46 [
47 {
48 type: 'literal',
49 value: numberFormat.format(Math.abs(value)),
50 unit: unit,
51 },
52 ];
53 var pr = pluralRules.select(value);
54 var pattern = po[pr];
55 return (0, MakePartsList_1.MakePartsList)(pattern, resolvedUnit, fv);
56}
57exports.PartitionRelativeTimePattern = PartitionRelativeTimePattern;