UNPKG

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