UNPKG

977 BJavaScriptView Raw
1import { invariant, Type, } from '@formatjs/ecma402-abstract';
2/**
3 * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4 * @param unit
5 */
6export function SingularRelativeTimeUnit(unit) {
7 invariant(Type(unit) === 'String', 'unit must be a string');
8 if (unit === 'seconds')
9 return 'second';
10 if (unit === 'minutes')
11 return 'minute';
12 if (unit === 'hours')
13 return 'hour';
14 if (unit === 'days')
15 return 'day';
16 if (unit === 'weeks')
17 return 'week';
18 if (unit === 'months')
19 return 'month';
20 if (unit === 'quarters')
21 return 'quarter';
22 if (unit === 'years')
23 return 'year';
24 if (unit !== 'second' &&
25 unit !== 'minute' &&
26 unit !== 'hour' &&
27 unit !== 'day' &&
28 unit !== 'week' &&
29 unit !== 'month' &&
30 unit !== 'quarter' &&
31 unit !== 'year') {
32 throw new RangeError('invalid unit');
33 }
34 return unit;
35}