Initial import from local backup (Documents-Playground/pakerpale)

This commit is contained in:
jeonghwa
2026-07-03 05:27:29 +09:00
commit d918e2eddc
2971 changed files with 264195 additions and 0 deletions

View File

@@ -0,0 +1,276 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = extractRelativeTimeMessages;
var _RelativeTimeFormat = require("../RelativeTimeFormat");
// import { isEqual } from 'lodash'
// Detects short and narrow flavours of labels (yr., mo., etc).
// E.g. there are "month", "month-short", "month-narrow".
// More on "narrow" vs "short":
// http://cldr.unicode.org/translation/plurals#TOC-Narrow-and-Short-Forms
var short = /-short$/;
var narrow = /-narrow$/; // Converts locale data from CLDR format to this library's format.
//
// CLDR locale data example:
//
// ```json
// {
// "main": {
// "en-US-POSIX": {
// "identity": {
// "language": "en",
// ...
// },
// "dates": {
// "fields": {
// "year": {
// "displayName": "year",
// "relative-type--1": "last year",
// "relative-type-0": "this year",
// "relative-type-1": "next year",
// "relativeTime-type-future": {
// "relativeTimePattern-count-one": "in {0} year",
// "relativeTimePattern-count-other": "in {0} years"
// },
// "relativeTime-type-past": {
// "relativeTimePattern-count-one": "{0} year ago",
// "relativeTimePattern-count-other": "{0} years ago"
// }
// },
// ...
// ```
//
// Parsed locale data example:
//
// ```json
// {
// "long":
// {
// ...
// "second": [
// {
// "one": "a second ago",
// "other": "{0} seconds ago"
// },
// {
// "one": "in a second",
// "other": "in {0} seconds"
// }
// ],
// ...
// },
// "short":
// {
// ...
// },
// ...
// }
// ```
function extractRelativeTimeMessages(localeData) {
// Extract `locale` from CLDR locale data.
var locale = Object.keys(localeData.main)[0];
var timeUnitsFormattingRules = localeData.main[locale].dates.fields;
return Object.keys(timeUnitsFormattingRules).filter(function (unit) {
// Take only the generic time measurement units
// (skip exotic ones like "fri" on "thu").
return _RelativeTimeFormat.UNITS.indexOf(parseUnit(unit).unit) >= 0;
}).reduce(function (localeData, _unit) {
var _parseUnit = parseUnit(_unit),
unit = _parseUnit.unit,
type = _parseUnit.type;
return setUnitRules(localeData, type, unit, extractTimeUnitFormattingRules(timeUnitsFormattingRules[_unit]));
}, {});
}
/**
* Parses CLDR time unit formatting rules.
* @param {object} - CLDR time unit formatting rules.
* @return {(object|string)}
*/
function extractTimeUnitFormattingRules(rulesCLDR) {
var rules = {}; // "relative" values aren't suitable for "ago" or "in a" cases,
// because "1 year ago" != "last year" (too vague for Jan 30th)
// and "in 0.49 years" != "this year" (it could be Nov 30th).
// Still including them here for `Intl.RelativeTimeFormat` polyfill.
// "yesterday".
//
// "the day before yesterday".
// For example, in German it's "Vorgestern".
//
// etc.
//
var previousIndex = 1;
while (rulesCLDR["relative-type--".concat(previousIndex)]) {
rules["previous".concat(previousIndex === 1 ? '' : '-' + previousIndex)] = rulesCLDR["relative-type--".concat(previousIndex)];
previousIndex++;
} // "today"
/* istanbul ignore else */
if (rulesCLDR['relative-type-0']) {
rules.current = rulesCLDR['relative-type-0'];
} // "tomorrow".
//
// "the day after tomorrow".
// For example, in German it's "Übermorgen".
//
// etc.
//
var nextIndex = 1;
while (rulesCLDR["relative-type-".concat(nextIndex)]) {
rules["next".concat(nextIndex === 1 ? '' : '-' + nextIndex)] = rulesCLDR["relative-type-".concat(nextIndex)];
nextIndex++;
} // Formatting past times.
//
// E.g.:
//
// "relativeTime-type-past":
// {
// "relativeTimePattern-count-one": "{0} mo. ago",
// "relativeTimePattern-count-other": "{0} mo. ago"
// }
//
/* istanbul ignore else */
if (rulesCLDR['relativeTime-type-past']) {
var past = rulesCLDR['relativeTime-type-past'];
rules.past = {}; // Populate all quantifiers ("one", "other", etc).
var _arr = Object.keys(past);
for (var _i = 0; _i < _arr.length; _i++) {
var quantifier = _arr[_i];
rules.past[quantifier.replace('relativeTimePattern-count-', '')] = past[quantifier];
} // Delete all duplicates of "other" rule.
var _arr2 = Object.keys(rules.past);
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
var _quantifier = _arr2[_i2];
if (_quantifier !== 'other' && rules.past[_quantifier] === rules.past.other) {
delete rules.past[_quantifier];
}
} // If only "other" rule is present then "rules" is not an object and is a string.
if (Object.keys(rules.past).length === 1) {
rules.past = rules.past.other;
}
} // Formatting future times.
//
// E.g.:
//
// "relativeTime-type-future":
// {
// "relativeTimePattern-count-one": "in {0} mo.",
// "relativeTimePattern-count-other": "in {0} mo."
// }
//
/* istanbul ignore else */
if (rulesCLDR['relativeTime-type-future']) {
var future = rulesCLDR['relativeTime-type-future'];
rules.future = {}; // Populate all quantifiers ("one", "other", etc).
var _arr3 = Object.keys(future);
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
var _quantifier2 = _arr3[_i3];
rules.future[_quantifier2.replace('relativeTimePattern-count-', '')] = future[_quantifier2];
} // Delete all duplicates of "other" rule.
var _arr4 = Object.keys(rules.future);
for (var _i4 = 0; _i4 < _arr4.length; _i4++) {
var _quantifier3 = _arr4[_i4];
if (_quantifier3 !== 'other' && rules.future[_quantifier3] === rules.future.other) {
delete rules.future[_quantifier3];
}
} // If only "other" rule is present then "rules" is not an object and is a string.
if (Object.keys(rules.future).length === 1) {
rules.future = rules.future.other;
}
} // // If `.past` === `.future` then replace them with `.other`.
// // (only eligible for "tiny" and "*-time" locale data which is not part of CLDR)
// if (isEqual(rules.past, rules.future)) {
// rules.other = rules.past
// delete rules.future
// }
// // If only "other" rule is defined for a time unit
// // then make "rules" a string rather than an object.
// if (Object.keys(rules).length === 1) {
// rules = rules.other
// }
return rules;
}
/**
* Sets time unit formatting rules in locale data.
* @param {object} localeData
* @param {string} type
* @param {string} unit
* @param {object} rules
* @return {object} Locale data.
*/
function setUnitRules(localeData, type, unit, rules) {
if (!localeData[type]) {
localeData[type] = {};
}
localeData[type][unit] = rules;
return localeData;
}
/**
* Parses CLDR time unit into `unit` and `type`.
* @param {string} CLDR_unit
* @return {object} `{ type, unit }`.
*/
function parseUnit(unit) {
if (narrow.test(unit)) {
return {
type: 'narrow',
unit: unit.replace(narrow, '')
};
}
if (short.test(unit)) {
return {
type: 'short',
unit: unit.replace(short, '')
};
}
return {
type: 'long',
unit: unit
};
}
//# sourceMappingURL=extractRelativeTimeMessages.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,704 @@
"use strict";
var _extractRelativeTimeMessages = _interopRequireDefault(require("./extractRelativeTimeMessages"));
var _en = require("../../locale/en");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('CLDR', function () {
it('should parse Unicode CLDR locale data', function () {
// console.log(JSON.stringify(extractRelativeTimeMessages(englishDateMessagesInCLDR), null, 2))
(0, _extractRelativeTimeMessages.default)(englishDateMessagesInCLDR).should.deep.equal(englishRelativeTimeMessages);
});
});
var englishRelativeTimeMessages = {
"long": {
"year": {
"previous": "last year",
"current": "this year",
"next": "next year",
"past": {
"one": "{0} year ago",
"other": "{0} years ago"
},
"future": {
"one": "in {0} year",
"other": "in {0} years"
}
},
"quarter": {
"previous": "last quarter",
"current": "this quarter",
"next": "next quarter",
"future": {
"one": "in {0} quarter",
"other": "in {0} quarters"
},
"past": {
"one": "{0} quarter ago",
"other": "{0} quarters ago"
}
},
"month": {
"previous": "last month",
"current": "this month",
"next": "next month",
"past": {
"one": "{0} month ago",
"other": "{0} months ago"
},
"future": {
"one": "in {0} month",
"other": "in {0} months"
}
},
"week": {
"previous": "last week",
"current": "this week",
"next": "next week",
"past": {
"one": "{0} week ago",
"other": "{0} weeks ago"
},
"future": {
"one": "in {0} week",
"other": "in {0} weeks"
}
},
"day": {
"previous-2": "the day before yesterday (test)",
"previous": "yesterday",
"current": "today",
"next": "tomorrow",
"next-2": "the day after tomorrow (test)",
"past": {
"one": "{0} day ago",
"other": "{0} days ago"
},
"future": {
"one": "in {0} day",
"other": "in {0} days"
}
},
"hour": {
"current": "this hour",
"past": {
"one": "{0} hour ago",
"other": "{0} hours ago"
},
"future": {
"one": "in {0} hour",
"other": "in {0} hours"
}
},
"minute": {
"current": "this minute",
"past": {
"one": "{0} minute ago",
"other": "{0} minutes ago"
},
"future": {
"one": "in {0} minute",
"other": "in {0} minutes"
}
},
"second": {
"current": "now",
"past": {
"one": "{0} second ago",
"other": "{0} seconds ago"
},
"future": {
"one": "in {0} second",
"other": "in {0} seconds"
}
}
},
"short": {
"year": {
"previous": "last yr.",
"current": "this yr.",
"next": "next yr.",
"past": "{0} yr. ago",
"future": "in {0} yr."
},
"quarter": {
"previous": "last qtr.",
"current": "this qtr.",
"next": "next qtr.",
"future": {
"one": "in {0} qtr.",
"other": "in {0} qtrs."
},
"past": {
"one": "{0} qtr. ago",
"other": "{0} qtrs. ago"
}
},
"month": {
"previous": "last mo.",
"current": "this mo.",
"next": "next mo.",
"past": "{0} mo. ago",
"future": "in {0} mo."
},
"week": {
"previous": "last wk.",
"current": "this wk.",
"next": "next wk.",
"past": "{0} wk. ago",
"future": "in {0} wk."
},
"day": {
"previous": "yesterday",
"current": "today",
"next": "tomorrow",
"past": {
"one": "{0} day ago",
"other": "{0} days ago"
},
"future": {
"one": "in {0} day",
"other": "in {0} days"
}
},
"hour": {
"current": "this hour",
"past": "{0} hr. ago",
"future": "in {0} hr."
},
"minute": {
"current": "this minute",
"past": "{0} min. ago",
"future": "in {0} min."
},
"second": {
"current": "now",
"past": "{0} sec. ago",
"future": "in {0} sec."
}
},
"narrow": {
"year": {
"previous": "last yr.",
"current": "this yr.",
"next": "next yr.",
"future": "in {0} yr.",
"past": "{0} yr. ago"
},
"quarter": {
"previous": "last qtr.",
"current": "this qtr.",
"next": "next qtr.",
"future": {
"one": "in {0} qtr.",
"other": "in {0} qtrs."
},
"past": {
"one": "{0} qtr. ago",
"other": "{0} qtrs. ago"
}
},
"month": {
"previous": "last mo.",
"current": "this mo.",
"next": "next mo.",
"future": "in {0} mo.",
"past": "{0} mo. ago"
},
"week": {
"previous": "last wk.",
"current": "this wk.",
"next": "next wk.",
"future": "in {0} wk.",
"past": "{0} wk. ago"
},
"day": {
"previous": "yesterday",
"current": "today",
"next": "tomorrow",
"past": {
"one": "{0} day ago",
"other": "{0} days ago"
},
"future": {
"one": "in {0} day",
"other": "in {0} days"
}
},
"hour": {
"current": "this hour",
"future": "in {0} hr.",
"past": "{0} hr. ago"
},
"minute": {
"current": "this minute",
"future": "in {0} min.",
"past": "{0} min. ago"
},
"second": {
"current": "now",
"future": "in {0} sec.",
"past": "{0} sec. ago"
}
}
};
var englishDateMessagesInCLDR = {
"main": {
"en-US-POSIX": {
"identity": {
"version": {
"_number": "$Revision: 11914 $",
"_cldrVersion": "29"
},
"language": "en",
"territory": "US",
"variant": "POSIX"
},
"dates": {
"fields": {
"era": {
"displayName": "era"
},
"year": {
"displayName": "year",
"relative-type--1": "last year",
"relative-type-0": "this year",
"relative-type-1": "next year",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} year",
"relativeTimePattern-count-other": "in {0} years"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} year ago",
"relativeTimePattern-count-other": "{0} years ago"
}
},
"year-short": {
"displayName": "yr.",
"relative-type--1": "last yr.",
"relative-type-0": "this yr.",
"relative-type-1": "next yr.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} yr.",
"relativeTimePattern-count-other": "in {0} yr."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} yr. ago",
"relativeTimePattern-count-other": "{0} yr. ago"
}
},
"year-narrow": {
"displayName": "yr.",
"relative-type--1": "last yr.",
"relative-type-0": "this yr.",
"relative-type-1": "next yr.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} yr.",
"relativeTimePattern-count-other": "in {0} yr."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} yr. ago",
"relativeTimePattern-count-other": "{0} yr. ago"
}
},
"quarter": {
"displayName": "quarter",
"relative-type--1": "last quarter",
"relative-type-0": "this quarter",
"relative-type-1": "next quarter",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} quarter",
"relativeTimePattern-count-other": "in {0} quarters"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} quarter ago",
"relativeTimePattern-count-other": "{0} quarters ago"
}
},
"quarter-short": {
"displayName": "qtr.",
"relative-type--1": "last qtr.",
"relative-type-0": "this qtr.",
"relative-type-1": "next qtr.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} qtr.",
"relativeTimePattern-count-other": "in {0} qtrs."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} qtr. ago",
"relativeTimePattern-count-other": "{0} qtrs. ago"
}
},
"quarter-narrow": {
"displayName": "qtr.",
"relative-type--1": "last qtr.",
"relative-type-0": "this qtr.",
"relative-type-1": "next qtr.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} qtr.",
"relativeTimePattern-count-other": "in {0} qtrs."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} qtr. ago",
"relativeTimePattern-count-other": "{0} qtrs. ago"
}
},
"month": {
"displayName": "month",
"relative-type--1": "last month",
"relative-type-0": "this month",
"relative-type-1": "next month",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} month",
"relativeTimePattern-count-other": "in {0} months"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} month ago",
"relativeTimePattern-count-other": "{0} months ago"
}
},
"month-short": {
"displayName": "mo.",
"relative-type--1": "last mo.",
"relative-type-0": "this mo.",
"relative-type-1": "next mo.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} mo.",
"relativeTimePattern-count-other": "in {0} mo."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} mo. ago",
"relativeTimePattern-count-other": "{0} mo. ago"
}
},
"month-narrow": {
"displayName": "mo.",
"relative-type--1": "last mo.",
"relative-type-0": "this mo.",
"relative-type-1": "next mo.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} mo.",
"relativeTimePattern-count-other": "in {0} mo."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} mo. ago",
"relativeTimePattern-count-other": "{0} mo. ago"
}
},
"week": {
"displayName": "week",
"relative-type--1": "last week",
"relative-type-0": "this week",
"relative-type-1": "next week",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} week",
"relativeTimePattern-count-other": "in {0} weeks"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} week ago",
"relativeTimePattern-count-other": "{0} weeks ago"
}
},
"week-short": {
"displayName": "wk.",
"relative-type--1": "last wk.",
"relative-type-0": "this wk.",
"relative-type-1": "next wk.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} wk.",
"relativeTimePattern-count-other": "in {0} wk."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} wk. ago",
"relativeTimePattern-count-other": "{0} wk. ago"
}
},
"week-narrow": {
"displayName": "wk.",
"relative-type--1": "last wk.",
"relative-type-0": "this wk.",
"relative-type-1": "next wk.",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} wk.",
"relativeTimePattern-count-other": "in {0} wk."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} wk. ago",
"relativeTimePattern-count-other": "{0} wk. ago"
}
},
"day": {
"displayName": "day",
"relative-type--2": "the day before yesterday (test)",
"relative-type--1": "yesterday",
"relative-type-0": "today",
"relative-type-1": "tomorrow",
"relative-type-2": "the day after tomorrow (test)",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} day",
"relativeTimePattern-count-other": "in {0} days"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} day ago",
"relativeTimePattern-count-other": "{0} days ago"
}
},
"day-short": {
"displayName": "day",
"relative-type--1": "yesterday",
"relative-type-0": "today",
"relative-type-1": "tomorrow",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} day",
"relativeTimePattern-count-other": "in {0} days"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} day ago",
"relativeTimePattern-count-other": "{0} days ago"
}
},
"day-narrow": {
"displayName": "day",
"relative-type--1": "yesterday",
"relative-type-0": "today",
"relative-type-1": "tomorrow",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} day",
"relativeTimePattern-count-other": "in {0} days"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} day ago",
"relativeTimePattern-count-other": "{0} days ago"
}
},
"weekday": {
"displayName": "day of the week"
},
"sun": {
"relative-type--1": "last Sunday",
"relative-type-0": "this Sunday",
"relative-type-1": "next Sunday"
},
"sun-short": {
"relative-type--1": "last Sun.",
"relative-type-0": "this Sun.",
"relative-type-1": "next Sun."
},
"sun-narrow": {
"relative-type--1": "last Su",
"relative-type-0": "this Su",
"relative-type-1": "next Su"
},
"mon": {
"relative-type--1": "last Monday",
"relative-type-0": "this Monday",
"relative-type-1": "next Monday"
},
"mon-short": {
"relative-type--1": "last Mon.",
"relative-type-0": "this Mon.",
"relative-type-1": "next Mon."
},
"mon-narrow": {
"relative-type--1": "last M",
"relative-type-0": "this M",
"relative-type-1": "next M"
},
"tue": {
"relative-type--1": "last Tuesday",
"relative-type-0": "this Tuesday",
"relative-type-1": "next Tuesday"
},
"tue-short": {
"relative-type--1": "last Tue.",
"relative-type-0": "this Tue.",
"relative-type-1": "next Tue."
},
"tue-narrow": {
"relative-type--1": "last Tu",
"relative-type-0": "this Tu",
"relative-type-1": "next Tu"
},
"wed": {
"relative-type--1": "last Wednesday",
"relative-type-0": "this Wednesday",
"relative-type-1": "next Wednesday"
},
"wed-short": {
"relative-type--1": "last Wed.",
"relative-type-0": "this Wed.",
"relative-type-1": "next Wed."
},
"wed-narrow": {
"relative-type--1": "last W",
"relative-type-0": "this W",
"relative-type-1": "next W"
},
"thu": {
"relative-type--1": "last Thursday",
"relative-type-0": "this Thursday",
"relative-type-1": "next Thursday"
},
"thu-short": {
"relative-type--1": "last Thu.",
"relative-type-0": "this Thu.",
"relative-type-1": "next Thu."
},
"thu-narrow": {
"relative-type--1": "last Th",
"relative-type-0": "this Th",
"relative-type-1": "next Th"
},
"fri": {
"relative-type--1": "last Friday",
"relative-type-0": "this Friday",
"relative-type-1": "next Friday"
},
"fri-short": {
"relative-type--1": "last Fri.",
"relative-type-0": "this Fri.",
"relative-type-1": "next Fri."
},
"fri-narrow": {
"relative-type--1": "last F",
"relative-type-0": "this F",
"relative-type-1": "next F"
},
"sat": {
"relative-type--1": "last Saturday",
"relative-type-0": "this Saturday",
"relative-type-1": "next Saturday"
},
"sat-short": {
"relative-type--1": "last Sat.",
"relative-type-0": "this Sat.",
"relative-type-1": "next Sat."
},
"sat-narrow": {
"relative-type--1": "last Sa",
"relative-type-0": "this Sa",
"relative-type-1": "next Sa"
},
"dayperiod": {
"displayName": "AM/PM",
"displayName-alt-variant": "am/pm"
},
"hour": {
"displayName": "hour",
"relative-type-0": "this hour",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} hour",
"relativeTimePattern-count-other": "in {0} hours"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} hour ago",
"relativeTimePattern-count-other": "{0} hours ago"
}
},
"hour-short": {
"displayName": "hr.",
"relative-type-0": "this hour",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} hr.",
"relativeTimePattern-count-other": "in {0} hr."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} hr. ago",
"relativeTimePattern-count-other": "{0} hr. ago"
}
},
"hour-narrow": {
"displayName": "hr.",
"relative-type-0": "this hour",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} hr.",
"relativeTimePattern-count-other": "in {0} hr."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} hr. ago",
"relativeTimePattern-count-other": "{0} hr. ago"
}
},
"minute": {
"displayName": "minute",
"relative-type-0": "this minute",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} minute",
"relativeTimePattern-count-other": "in {0} minutes"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} minute ago",
"relativeTimePattern-count-other": "{0} minutes ago"
}
},
"minute-short": {
"displayName": "min.",
"relative-type-0": "this minute",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} min.",
"relativeTimePattern-count-other": "in {0} min."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} min. ago",
"relativeTimePattern-count-other": "{0} min. ago"
}
},
"minute-narrow": {
"displayName": "min.",
"relative-type-0": "this minute",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} min.",
"relativeTimePattern-count-other": "in {0} min."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} min. ago",
"relativeTimePattern-count-other": "{0} min. ago"
}
},
"second": {
"displayName": "second",
"relative-type-0": "now",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} second",
"relativeTimePattern-count-other": "in {0} seconds"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} second ago",
"relativeTimePattern-count-other": "{0} seconds ago"
}
},
"second-short": {
"displayName": "sec.",
"relative-type-0": "now",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} sec.",
"relativeTimePattern-count-other": "in {0} sec."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} sec. ago",
"relativeTimePattern-count-other": "{0} sec. ago"
}
},
"second-narrow": {
"displayName": "sec.",
"relative-type-0": "now",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} sec.",
"relativeTimePattern-count-other": "in {0} sec."
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} sec. ago",
"relativeTimePattern-count-other": "{0} sec. ago"
}
},
"zone": {
"displayName": "time zone"
}
}
}
}
}
};
//# sourceMappingURL=extractRelativeTimeMessages.test.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getLocalesList;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Returns a list of all locales supported by CLDR.
* @return {string[]}
*/
function getLocalesList() {
return _fs.default.readdirSync(_path.default.join(__dirname, '../../node_modules/cldr-dates-full/main/')).filter(function (name) {
return _fs.default.statSync(_path.default.join(__dirname, '../../node_modules/cldr-dates-full/main', name)).isDirectory();
});
}
//# sourceMappingURL=getLocalesList.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../source/CLDR/getLocalesList.js"],"names":["getLocalesList","fs","readdirSync","path","join","__dirname","filter","name","statSync","isDirectory"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEA;;;;AAIe,SAASA,cAAT,GAA0B;AACxC,SAAOC,YAAGC,WAAH,CAAeC,cAAKC,IAAL,CAAUC,SAAV,EAAqB,0CAArB,CAAf,EACLC,MADK,CACE,UAAAC,IAAI;AAAA,WAAIN,YAAGO,QAAH,CAAYL,cAAKC,IAAL,CAAUC,SAAV,EAAqB,yCAArB,EAAgEE,IAAhE,CAAZ,EAAmFE,WAAnF,EAAJ;AAAA,GADN,CAAP;AAEA","sourcesContent":["import fs from 'fs'\r\nimport path from 'path'\r\n\r\n/**\r\n * Returns a list of all locales supported by CLDR.\r\n * @return {string[]}\r\n */\r\nexport default function getLocalesList() {\r\n\treturn fs.readdirSync(path.join(__dirname, '../../node_modules/cldr-dates-full/main/'))\r\n\t\t.filter(name => fs.statSync(path.join(__dirname, '../../node_modules/cldr-dates-full/main', name)).isDirectory())\r\n}\r\n"],"file":"getLocalesList.js"}

View File

@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDefaultLocale = getDefaultLocale;
exports.setDefaultLocale = setDefaultLocale;
exports.getLocaleData = getLocaleData;
exports.addLocaleData = addLocaleData;
// Fallback locale.
// (when not a single one of the supplied "preferred" locales is available)
var defaultLocale = 'en'; // For all locales added
// their relative time formatter messages will be stored here.
var localesData = {};
function getDefaultLocale() {
return defaultLocale;
}
function setDefaultLocale(locale) {
defaultLocale = locale;
} // export function isLocaleDataAvailable(locale) {
// return localesData.hasOwnProperty(locale)
// }
function getLocaleData(locale) {
return localesData[locale];
}
function addLocaleData(localeData) {
if (!localeData) {
throw new Error('No locale data passed');
} // This locale data is stored in a global variable
// and later used when calling `.format(time)`.
localesData[localeData.locale] = localeData;
}
//# sourceMappingURL=LocaleDataStore.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../source/LocaleDataStore.js"],"names":["defaultLocale","localesData","getDefaultLocale","setDefaultLocale","locale","getLocaleData","addLocaleData","localeData","Error"],"mappings":";;;;;;;;;AAAA;AACA;AACA,IAAIA,aAAa,GAAG,IAApB,C,CAEA;AACA;;AACA,IAAMC,WAAW,GAAG,EAApB;;AAEO,SAASC,gBAAT,GAA4B;AACjC,SAAOF,aAAP;AACD;;AAEM,SAASG,gBAAT,CAA0BC,MAA1B,EAAkC;AACvCJ,EAAAA,aAAa,GAAGI,MAAhB;AACD,C,CAED;AACA;AACA;;;AAEO,SAASC,aAAT,CAAuBD,MAAvB,EAA+B;AACpC,SAAOH,WAAW,CAACG,MAAD,CAAlB;AACD;;AAEM,SAASE,aAAT,CAAuBC,UAAvB,EAAmC;AACxC,MAAI,CAACA,UAAL,EAAiB;AACf,UAAM,IAAIC,KAAJ,CAAU,uBAAV,CAAN;AACD,GAHuC,CAIxC;AACA;;;AACAP,EAAAA,WAAW,CAACM,UAAU,CAACH,MAAZ,CAAX,GAAiCG,UAAjC;AACD","sourcesContent":["// Fallback locale.\r\n// (when not a single one of the supplied \"preferred\" locales is available)\r\nlet defaultLocale = 'en'\r\n\r\n// For all locales added\r\n// their relative time formatter messages will be stored here.\r\nconst localesData = {}\r\n\r\nexport function getDefaultLocale() {\r\n return defaultLocale\r\n}\r\n\r\nexport function setDefaultLocale(locale) {\r\n defaultLocale = locale\r\n}\r\n\r\n// export function isLocaleDataAvailable(locale) {\r\n// return localesData.hasOwnProperty(locale)\r\n// }\r\n\r\nexport function getLocaleData(locale) {\r\n return localesData[locale]\r\n}\r\n\r\nexport function addLocaleData(localeData) {\r\n if (!localeData) {\r\n throw new Error('No locale data passed')\r\n }\r\n // This locale data is stored in a global variable\r\n // and later used when calling `.format(time)`.\r\n localesData[localeData.locale] = localeData\r\n}"],"file":"LocaleDataStore.js"}

View File

@@ -0,0 +1,10 @@
"use strict";
var _LocaleDataStore = require("./LocaleDataStore");
describe('LocaleDataStore', function () {
it('"addLocaleData" should throw if no locale data passed', function () {
expect(_LocaleDataStore.addLocaleData).to.throw('No locale data passed');
});
});
//# sourceMappingURL=LocaleDataStore.test.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../source/LocaleDataStore.test.js"],"names":["describe","it","expect","addLocaleData","to","throw"],"mappings":";;AAAA;;AAEAA,QAAQ,CAAC,iBAAD,EAAoB,YAAM;AAChCC,EAAAA,EAAE,CAAC,uDAAD,EAA0D,YAAM;AAChEC,IAAAA,MAAM,CAACC,8BAAD,CAAN,CAAsBC,EAAtB,CAAyBC,KAAzB,CAA+B,uBAA/B;AACD,GAFC,CAAF;AAGD,CAJO,CAAR","sourcesContent":["import { addLocaleData } from './LocaleDataStore'\r\n\r\ndescribe('LocaleDataStore', () => {\r\n it('\"addLocaleData\" should throw if no locale data passed', () => {\r\n expect(addLocaleData).to.throw('No locale data passed')\r\n })\r\n})"],"file":"LocaleDataStore.test.js"}

View File

@@ -0,0 +1,369 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.UNITS = void 0;
var _LocaleDataStore = require("./LocaleDataStore");
var _resolveLocale = _interopRequireDefault(require("./resolveLocale"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// Valid time units.
var UNITS = ["second", "minute", "hour", "day", "week", "month", "quarter", "year"]; // Valid values for the `numeric` option.
exports.UNITS = UNITS;
var NUMERIC_VALUES = ["auto", "always"]; // Valid values for the `style` option.
var STYLE_VALUES = ["long", "short", "narrow"];
/**
* Polyfill for `Intl.RelativeTimeFormat` proposal.
* https://github.com/tc39/proposal-intl-relative-time
* https://github.com/tc39/proposal-intl-relative-time/issues/55
*/
var RelativeTimeFormat =
/*#__PURE__*/
function () {
/**
* @param {(string|string[])} [locales] - Preferred locales (or locale).
* @param {Object} [options] - Formatting options.
* @param {string} [options.style="long"] - One of: "long", "short", "narrow".
* @param {string} [options.numeric="always"] - (Version >= 2) One of: "always", "auto".
* @param {string} [options.localeMatcher="lookup"] - One of: "lookup", "best fit". Currently only "lookup" is supported.
*/
function RelativeTimeFormat() {
var locales = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, RelativeTimeFormat);
_defineProperty(this, "numeric", "always");
_defineProperty(this, "style", "long");
_defineProperty(this, "localeMatcher", "lookup");
var numeric = options.numeric,
style = options.style,
localeMatcher = options.localeMatcher; // Set `numeric` option.
if (numeric) {
if (NUMERIC_VALUES.indexOf(numeric) < 0) {
throw new RangeError("Invalid \"numeric\" option: ".concat(numeric));
}
this.numeric = numeric;
} // Set `style` option.
if (style) {
if (STYLE_VALUES.indexOf(style) < 0) {
throw new RangeError("Invalid \"style\" option: ".concat(style));
}
this.style = style;
} // Set `localeMatcher` option.
if (localeMatcher) {
this.localeMatcher = localeMatcher;
} // Set `locale`.
// Convert `locales` to an array.
if (typeof locales === 'string') {
locales = [locales];
} // Add default locale.
locales.push((0, _LocaleDataStore.getDefaultLocale)()); // Choose the most appropriate locale.
this.locale = RelativeTimeFormat.supportedLocalesOf(locales, {
localeMatcher: this.localeMatcher
})[0];
if (!this.locale) {
throw new TypeError("No supported locale was found");
}
this.locale = (0, _resolveLocale.default)(this.locale, {
localeMatcher: this.localeMatcher
}); // Use `Intl.NumberFormat` for formatting numbers (when available).
if (typeof Intl !== 'undefined' && Intl.NumberFormat) {
this.numberFormat = new Intl.NumberFormat(this.locale);
}
}
/**
* Formats time `value` in `units` (either in past or in future).
* @param {number} value - Time interval value.
* @param {string} unit - Time interval measurement unit.
* @return {string}
* @throws {RangeError} If unit is not one of "second", "minute", "hour", "day", "week", "month", "quarter".
* @example
* // Returns "2 days ago"
* rtf.format(-2, "day")
* // Returns "in 5 minutes"
* rtf.format(5, "minute")
*/
_createClass(RelativeTimeFormat, [{
key: "format",
value: function format(value, unit) {
return this.getRule(value, unit).replace('{0}', this.formatNumber(Math.abs(value)));
}
/**
* Formats time `value` in `units` (either in past or in future).
* @param {number} value - Time interval value.
* @param {string} unit - Time interval measurement unit.
* @return {Object[]} The parts (`{ type, value }`).
* @throws {RangeError} If unit is not one of "second", "minute", "hour", "day", "week", "month", "quarter".
* @example
* // Version 1.
* // Returns [
* // { type: "literal", value: "in " },
* // { type: "day", value: "100" },
* // { type: "literal", value: " days" }
* // ]
* rtf.formatToParts(100, "day")
* //
* // Version 2.
* // Returns [
* // { type: "literal", value: "in " },
* // { type: "integer", value: "100", unit: "day" },
* // { type: "literal", value: " days" }
* // ]
* rtf.formatToParts(100, "day")
*/
}, {
key: "formatToParts",
value: function formatToParts(value, unit) {
var rule = this.getRule(value, unit);
var valueIndex = rule.indexOf("{0}"); // "yesterday"/"today"/"tomorrow".
if (valueIndex < 0) {
return [{
type: "literal",
value: rule
}];
}
var parts = [];
if (valueIndex > 0) {
parts.push({
type: "literal",
value: rule.slice(0, valueIndex)
});
}
parts.push({
unit: unit,
type: "integer",
value: this.formatNumber(Math.abs(value))
});
if (valueIndex + "{0}".length < rule.length - 1) {
parts.push({
type: "literal",
value: rule.slice(valueIndex + "{0}".length)
});
}
return parts;
}
/**
* Returns formatting rule for `value` in `units` (either in past or in future).
* @param {number} value - Time interval value.
* @param {string} unit - Time interval measurement unit.
* @return {string}
* @throws {RangeError} If unit is not one of "second", "minute", "hour", "day", "week", "month", "quarter".
* @example
* // Returns "{0} days ago"
* getRule(-2, "day")
*/
}, {
key: "getRule",
value: function getRule(value, unit) {
if (UNITS.indexOf(unit) < 0) {
throw new RangeError("Unknown time unit: ".concat(unit, "."));
} // Get locale-specific time interval formatting rules
// of a given `style` for the given value of measurement `unit`.
//
// E.g.:
//
// ```json
// {
// "past": {
// "one": "a second ago",
// "other": "{0} seconds ago"
// },
// "future": {
// "one": "in a second",
// "other": "in {0} seconds"
// }
// }
// ```
//
var unitRules = (0, _LocaleDataStore.getLocaleData)(this.locale)[this.style][unit]; // Special case for "yesterday"/"today"/"tomorrow".
if (this.numeric === "auto") {
// "yesterday", "the day before yesterday", etc.
if (value === -2 || value === -1) {
var message = unitRules["previous".concat(value === -1 ? '' : '-' + Math.abs(value))];
if (message) {
return message;
}
} // "tomorrow", "the day after tomorrow", etc.
else if (value === 1 || value === 2) {
var _message = unitRules["next".concat(value === 1 ? '' : '-' + Math.abs(value))];
if (_message) {
return _message;
}
} // "today"
else if (value === 0) {
if (unitRules.current) {
return unitRules.current;
}
}
} // Choose either "past" or "future" based on time `value` sign.
// If there's only "other" then it's being collapsed.
// (the resulting bundle size optimization technique)
var quantifierRules = unitRules[value <= 0 ? "past" : "future"]; // Bundle size optimization technique.
if (typeof quantifierRules === "string") {
return quantifierRules;
} // Quantify `value`.
var quantify = (0, _LocaleDataStore.getLocaleData)(this.locale).quantify;
var quantifier = quantify && quantify(Math.abs(value)); // There seems to be no such locale in CLDR
// for which `quantify` is missing
// and still `past` and `future` messages
// contain something other than "other".
/* istanbul ignore next */
quantifier = quantifier || 'other'; // "other" rule is supposed to be always present.
// If only "other" rule is present then "rules" is not an object and is a string.
return quantifierRules[quantifier] || quantifierRules.other;
}
/**
* Formats a number into a string.
* Uses `Intl.NumberFormat` when available.
* @param {number} number
* @return {string}
*/
}, {
key: "formatNumber",
value: function formatNumber(number) {
return this.numberFormat ? this.numberFormat.format(number) : String(number);
}
/**
* Returns a new object with properties reflecting the locale and date and time formatting options computed during initialization of this DateTimeFormat object.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions
* @return {Object}
*/
}, {
key: "resolvedOptions",
value: function resolvedOptions() {
return {
locale: this.locale,
style: this.style,
numeric: this.numeric
};
}
}]);
return RelativeTimeFormat;
}();
/**
* Returns an array containing those of the provided locales
* that are supported in collation without having to fall back
* to the runtime's default locale.
* @param {(string|string[])} locale - A string with a BCP 47 language tag, or an array of such strings. For the general form of the locales argument, see the Intl page.
* @param {Object} [options] - An object that may have the following property:
* @param {string} [options.localeMatcher="lookup"] - The locale matching algorithm to use. Possible values are "lookup" and "best fit". Currently only "lookup" is supported.
* @return {string[]} An array of strings representing a subset of the given locale tags that are supported in collation without having to fall back to the runtime's default locale.
* @example
* var locales = ['ban', 'id-u-co-pinyin', 'es-PY']
* var options = { localeMatcher: 'lookup' }
* // Returns ["id", "es-PY"]
* Intl.RelativeTimeFormat.supportedLocalesOf(locales, options)
*/
exports.default = RelativeTimeFormat;
RelativeTimeFormat.supportedLocalesOf = function (locales) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
// Convert `locales` to an array.
if (typeof locales === 'string') {
locales = [locales];
}
return locales.filter(function (locale) {
return (0, _resolveLocale.default)(locale, options);
});
};
/**
* Adds locale data for a specific locale.
* @param {Object} localeData
*/
RelativeTimeFormat.addLocale = _LocaleDataStore.addLocaleData;
/**
* Sets default locale.
* @param {string} locale
*/
RelativeTimeFormat.setDefaultLocale = _LocaleDataStore.setDefaultLocale;
/**
* Gets default locale.
* @return {string} locale
*/
RelativeTimeFormat.getDefaultLocale = _LocaleDataStore.getDefaultLocale;
/**
* Extracts language from an IETF BCP 47 language tag.
* @param {string} languageTag - IETF BCP 47 language tag.
* @return {string}
* @example
* // Returns "he"
* getLanguageFromLanguageTag("he-IL-u-ca-hebrew-tz-jeruslm")
* // Returns "ar"
* getLanguageFromLanguageTag("ar-u-nu-latn")
*/
// export function getLanguageFromLanguageTag(languageTag) {
// const hyphenIndex = languageTag.indexOf('-')
// if (hyphenIndex > 0) {
// return languageTag.slice(0, hyphenIndex)
// }
// return languageTag
// }
//# sourceMappingURL=RelativeTimeFormat.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,261 @@
"use strict";
var _ccp = _interopRequireDefault(require("../locale/ccp"));
var _de = _interopRequireDefault(require("../locale/de"));
var _en = _interopRequireDefault(require("../locale/en"));
var _ru = _interopRequireDefault(require("../locale/ru"));
var _to = _interopRequireDefault(require("../locale/to"));
var _RelativeTimeFormat = _interopRequireDefault(require("./RelativeTimeFormat"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
_RelativeTimeFormat.default.addLocale(_ccp.default);
_RelativeTimeFormat.default.addLocale(_de.default);
_RelativeTimeFormat.default.addLocale(_en.default);
_RelativeTimeFormat.default.addLocale(_ru.default);
_RelativeTimeFormat.default.addLocale(_to.default); // Just so this function code is covered.
_RelativeTimeFormat.default.setDefaultLocale('en');
describe('Intl.RelativeTimeFormat', function () {
it('should validate options', function () {
expect(function () {
return new _RelativeTimeFormat.default("en", {
style: "postmodern"
});
}).to.throw("Invalid \"style\" option");
expect(function () {
return new _RelativeTimeFormat.default("en", {
numeric: "sometimes"
});
}).to.throw("Invalid \"numeric\" option");
});
it('should fall back to default locale', function () {
var rtf = new _RelativeTimeFormat.default();
expect(rtf.format(-1, "day")).to.equal("1 day ago");
});
it('should throw when "numeric" option is not a valid one', function () {
expect(function () {
return new _RelativeTimeFormat.default("en", {
numeric: "sometimes"
});
}).to.throw('Invalid "numeric" option');
});
it('should use the passed "style" option', function () {
var rtf = new _RelativeTimeFormat.default("en", {
style: "short"
});
expect(rtf.format(-1, "year")).to.equal("1 yr. ago");
});
it('should throw when "style" option is not a valid one', function () {
expect(function () {
return new _RelativeTimeFormat.default("en", {
style: "postmodern"
});
}).to.throw('Invalid "style" option');
});
it('should use the passed "localeMatcher" option', function () {
var rtf = new _RelativeTimeFormat.default("en-XX", {
localeMatcher: "lookup"
});
expect(rtf.format(-1, "day")).to.equal("1 day ago");
});
it('should throw when "localeMatcher" option is not a valid one', function () {
expect(function () {
return new _RelativeTimeFormat.default("en", {
localeMatcher: "eccentric"
});
}).to.throw('Invalid "localeMatcher" option');
});
it('should throw if no supported locale was found', function () {
_RelativeTimeFormat.default.setDefaultLocale('xx');
expect(function () {
return new _RelativeTimeFormat.default();
}).to.throw("No supported locale was found");
_RelativeTimeFormat.default.setDefaultLocale('en');
});
it('should format relative time', function () {
var rtf = new _RelativeTimeFormat.default("en");
expect(rtf.format(-1, "day")).to.equal("1 day ago");
expect(rtf.format(-2, "day")).to.equal("2 days ago");
expect(rtf.format(2.15, "day")).to.equal("in 2.15 days");
expect(rtf.format(100, "day")).to.equal("in 100 days");
});
it('should fall back to "other" quantifier if others have been removed as an optimization', function () {
var rtf = new _RelativeTimeFormat.default("ru"); // `2` is classified as "few" in Russian.
// The rule for "few" is identical to that for "other"
// so the rule for "few" is omitted from locale data
// to reduce bundle size.
expect(rtf.format(-2, "day")).to.equal("2 дня назад");
});
it('should throw if a time unit is unsupported', function () {
var rtf = new _RelativeTimeFormat.default("en");
expect(function () {
return rtf.format(-1, "decade");
}).to.throw("Unknown time unit: decade.");
});
it('should format yesterday/today/tomorrow', function () {
var rtf = new _RelativeTimeFormat.default("de", {
numeric: "auto"
}); // "today" is useless for relative time labels.
// E.g. for `23:59:00` "today" is too vague.
// And for `00:01:00` "today" is counter-intuitive.
// "yesterday" and "tomorrow" are also useless for relative time.
// E.g. "yesterday" of `00:01` is misleading.
// Same as "tomorrow" of `23:59` which is misleading too.
// Not to mention that both of them are too "vague", same as "today".
// Also there are no rules defining when to use
// "yesterday", "today" and "tomorrow".
// The algorithm should take local time into account.
expect(rtf.format(-2, "day")).to.equal("vorgestern");
expect(rtf.format(-1, "day")).to.equal("gestern");
expect(rtf.format(0, "day")).to.equal("heute");
expect(rtf.format(1, "day")).to.equal("morgen");
expect(rtf.format(2, "day")).to.equal("übermorgen");
expect(rtf.format(0, "second")).to.equal("jetzt");
});
it('should use "Intl.NumberFormat" (when available)', function () {
var rtf = new _RelativeTimeFormat.default("en");
expect(rtf.format(1000, "day")).to.equal("in 1,000 days");
});
it('should fall back when "Intl.NumberFormat" is not available', function () {
var NumberFormat = Intl.NumberFormat; // I imagine `Intl` object getting "frozen" in future.
delete Intl.NumberFormat;
var rtf = new _RelativeTimeFormat.default("en");
expect(rtf.format(1000, "day")).to.equal("in 1000 days");
Intl.NumberFormat = NumberFormat;
});
it('shouldn\'t format yesterday/today/tomorrow when there\'s no locale data', function () {
var enLongDay = _objectSpread({}, _en.default.long.day);
delete _en.default.long.day.previous;
delete _en.default.long.day.current;
delete _en.default.long.day.next;
var rtf = new _RelativeTimeFormat.default("en", {
numeric: "auto"
}); // "today" is useless for relative time labels.
// E.g. for `23:59:00` "today" is too vague.
// And for `00:01:00` "today" is counter-intuitive.
// "yesterday" and "tomorrow" are also useless for relative time.
// E.g. "yesterday" of `00:01` is misleading.
// Same as "tomorrow" of `23:59` which is misleading too.
// Not to mention that both of them are too "vague", same as "today".
// Also there are no rules defining when to use
// "yesterday", "today" and "tomorrow".
// The algorithm should take local time into account.
expect(rtf.format(-1, "day")).to.equal("1 day ago");
expect(rtf.format(0, "day")).to.equal("0 days ago");
expect(rtf.format(1, "day")).to.equal("in 1 day");
_en.default.long.day = enLongDay;
});
it('should accept an array of locales', function () {
var rtf = new _RelativeTimeFormat.default(["en"]);
expect(rtf.format(-2, "day")).to.equal("2 days ago");
});
it('should resolve locales as "best fit"', function () {
var rtf = new _RelativeTimeFormat.default('en-XX');
expect(rtf.format(-2, "day")).to.equal("2 days ago");
});
it('should fallback to default system locale', function () {
var rtf = new _RelativeTimeFormat.default();
expect(rtf.format(-2, "day")).to.equal("2 days ago");
});
it('should format to parts', function () {
var rtf = new _RelativeTimeFormat.default("en");
expect(rtf.formatToParts(100, "day")).to.deep.equal([{
type: "literal",
value: "in "
}, {
type: "integer",
value: "100",
unit: "day"
}, {
type: "literal",
value: " days"
}]);
expect(rtf.formatToParts(-100, "day")).to.deep.equal([{
type: "integer",
value: "100",
unit: "day"
}, {
type: "literal",
value: " days ago"
}]);
});
it('should format to parts with numeric="auto"', function () {
var rtf = new _RelativeTimeFormat.default("en", {
numeric: "auto"
});
expect(rtf.formatToParts(-1, "day")).to.deep.equal([{
type: "literal",
value: "yesterday"
}]);
expect(rtf.formatToParts(100, "day")).to.deep.equal([{
type: "literal",
value: "in "
}, {
type: "integer",
value: "100",
unit: "day"
}, {
type: "literal",
value: " days"
}]);
});
it('should format to parts (non-English)', function () {
// Tonga (Tonga Islands)
var rtf = new _RelativeTimeFormat.default("to");
expect(rtf.formatToParts(100, "day")).to.deep.equal([{
type: "literal",
value: "ʻi he ʻaho ʻe "
}, {
type: "integer",
value: "100",
unit: "day"
}]);
});
it('"supportedLocalesOf" should list supported locales', function () {
expect(_RelativeTimeFormat.default.supportedLocalesOf(['es-ES', 'ru', 'ru-XX', 'en-GB'])).to.deep.equal(['ru', 'ru-XX', 'en-GB']);
expect(_RelativeTimeFormat.default.supportedLocalesOf('ru-XX')).to.deep.equal(['ru-XX']);
});
it('"supportedLocalesOf" should throw when "localeMatcher" option is not a valid one', function () {
expect(function () {
return _RelativeTimeFormat.default.supportedLocalesOf(["en"], {
localeMatcher: "eccentric"
});
}).to.throw('Invalid "localeMatcher" option');
});
it("should quantify as \"other\" when no quantifier function is present for a locale", function () {
new _RelativeTimeFormat.default("ccp").format(1, "minute").should.equal("1 𑄟𑄨𑄚𑄨𑄘𑄬");
});
it('should show resolved options', function () {
expect(new _RelativeTimeFormat.default('ru-XX', {
timeZone: 'UTC'
}).resolvedOptions()).to.deep.equal({
locale: "ru",
style: "long",
numeric: "always"
});
});
});
//# sourceMappingURL=RelativeTimeFormat.test.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = resolveLocale;
exports.resolveLocaleLookup = resolveLocaleLookup;
var _LocaleDataStore = require("./LocaleDataStore");
/**
* Resolves a locale to a supported one (if any).
* @param {string} locale
* @param {Object} [options] - An object that may have the following property:
* @param {string} [options.localeMatcher="lookup"] - The locale matching algorithm to use. Possible values are "lookup" and "best fit". Currently only "lookup" is supported.
* @return {string} [locale]
* @example
* // Returns "sr"
* resolveLocale("sr-Cyrl-BA")
* // Returns `undefined`
* resolveLocale("xx-Latn")
*/
function resolveLocale(locale) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var localeMatcher = options.localeMatcher || 'lookup';
switch (localeMatcher) {
case 'lookup':
return resolveLocaleLookup(locale);
// "best fit" locale matching is not supported.
// https://github.com/catamphetamine/relative-time-format/issues/2
case 'best fit':
// return resolveLocaleBestFit(locale)
return resolveLocaleLookup(locale);
default:
throw new RangeError("Invalid \"localeMatcher\" option: ".concat(localeMatcher));
}
}
/**
* Resolves a locale to a supported one (if any).
* Starts from the most specific locale and gradually
* falls back to less specific ones.
* This is a basic implementation of the "lookup" algorithm.
* https://tools.ietf.org/html/rfc4647#section-3.4
* @param {string} locale
* @return {string} [locale]
* @example
* // Returns "sr"
* resolveLocaleLookup("sr-Cyrl-BA")
* // Returns `undefined`
* resolveLocaleLookup("xx-Latn")
*/
function resolveLocaleLookup(locale) {
if ((0, _LocaleDataStore.getLocaleData)(locale)) {
return locale;
} // `sr-Cyrl-BA` -> `sr-Cyrl` -> `sr`.
var parts = locale.split('-');
while (locale.length > 1) {
parts.pop();
locale = parts.join('-');
if ((0, _LocaleDataStore.getLocaleData)(locale)) {
return locale;
}
}
}
//# sourceMappingURL=resolveLocale.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../source/resolveLocale.js"],"names":["resolveLocale","locale","options","localeMatcher","resolveLocaleLookup","RangeError","parts","split","length","pop","join"],"mappings":";;;;;;;;AAAA;;AAIA;;;;;;;;;;;;AAYe,SAASA,aAAT,CAAuBC,MAAvB,EAA6C;AAAA,MAAdC,OAAc,uEAAJ,EAAI;AAC1D,MAAMC,aAAa,GAAGD,OAAO,CAACC,aAAR,IAAyB,QAA/C;;AACA,UAAQA,aAAR;AACE,SAAK,QAAL;AACE,aAAOC,mBAAmB,CAACH,MAAD,CAA1B;AACF;AACA;;AACA,SAAK,UAAL;AACE;AACA,aAAOG,mBAAmB,CAACH,MAAD,CAA1B;;AACF;AACE,YAAM,IAAII,UAAJ,6CAAkDF,aAAlD,EAAN;AATJ;AAWD;AAED;;;;;;;;;;;;;;;;AAcO,SAASC,mBAAT,CAA6BH,MAA7B,EAAqC;AAC1C,MAAI,oCAAcA,MAAd,CAAJ,EAA2B;AACzB,WAAOA,MAAP;AACD,GAHyC,CAI1C;;;AACA,MAAMK,KAAK,GAAGL,MAAM,CAACM,KAAP,CAAa,GAAb,CAAd;;AACA,SAAON,MAAM,CAACO,MAAP,GAAgB,CAAvB,EAA0B;AACxBF,IAAAA,KAAK,CAACG,GAAN;AACAR,IAAAA,MAAM,GAAGK,KAAK,CAACI,IAAN,CAAW,GAAX,CAAT;;AACA,QAAI,oCAAcT,MAAd,CAAJ,EAA2B;AACzB,aAAOA,MAAP;AACD;AACF;AACF","sourcesContent":["import {\r\n getLocaleData\r\n} from './LocaleDataStore'\r\n\r\n/**\r\n * Resolves a locale to a supported one (if any).\r\n * @param {string} locale\r\n * @param {Object} [options] - An object that may have the following property:\r\n * @param {string} [options.localeMatcher=\"lookup\"] - The locale matching algorithm to use. Possible values are \"lookup\" and \"best fit\". Currently only \"lookup\" is supported.\r\n * @return {string} [locale]\r\n * @example\r\n * // Returns \"sr\"\r\n * resolveLocale(\"sr-Cyrl-BA\")\r\n * // Returns `undefined`\r\n * resolveLocale(\"xx-Latn\")\r\n */\r\nexport default function resolveLocale(locale, options = {}) {\r\n const localeMatcher = options.localeMatcher || 'lookup'\r\n switch (localeMatcher) {\r\n case 'lookup':\r\n return resolveLocaleLookup(locale)\r\n // \"best fit\" locale matching is not supported.\r\n // https://github.com/catamphetamine/relative-time-format/issues/2\r\n case 'best fit':\r\n // return resolveLocaleBestFit(locale)\r\n return resolveLocaleLookup(locale)\r\n default:\r\n throw new RangeError(`Invalid \"localeMatcher\" option: ${localeMatcher}`)\r\n }\r\n}\r\n\r\n/**\r\n * Resolves a locale to a supported one (if any).\r\n * Starts from the most specific locale and gradually\r\n * falls back to less specific ones.\r\n * This is a basic implementation of the \"lookup\" algorithm.\r\n * https://tools.ietf.org/html/rfc4647#section-3.4\r\n * @param {string} locale\r\n * @return {string} [locale]\r\n * @example\r\n * // Returns \"sr\"\r\n * resolveLocaleLookup(\"sr-Cyrl-BA\")\r\n * // Returns `undefined`\r\n * resolveLocaleLookup(\"xx-Latn\")\r\n */\r\nexport function resolveLocaleLookup(locale) {\r\n if (getLocaleData(locale)) {\r\n return locale\r\n }\r\n // `sr-Cyrl-BA` -> `sr-Cyrl` -> `sr`.\r\n const parts = locale.split('-')\r\n while (locale.length > 1) {\r\n parts.pop()\r\n locale = parts.join('-')\r\n if (getLocaleData(locale)) {\r\n return locale\r\n }\r\n }\r\n}\r\n"],"file":"resolveLocale.js"}

View File

@@ -0,0 +1,24 @@
"use strict";
var _resolveLocale = _interopRequireDefault(require("./resolveLocale"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('resolveLocale', function () {
it('should resolve locale', function () {
(0, _resolveLocale.default)("en-XX").should.equal("en");
});
it('should throw when "localeMatcher" option is not a valid one', function () {
expect(function () {
return (0, _resolveLocale.default)("en", {
localeMatcher: "eccentric"
});
}).to.throw('Invalid "localeMatcher" option');
});
it('should fall back to "lookup" when passed "best fit" "localeMatcher" option', function () {
(0, _resolveLocale.default)("en-XX", {
localeMatcher: "best fit"
}).should.equal("en");
});
});
//# sourceMappingURL=resolveLocale.test.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../source/resolveLocale.test.js"],"names":["describe","it","should","equal","expect","localeMatcher","to","throw"],"mappings":";;AAAA;;;;AAEAA,QAAQ,CAAC,eAAD,EAAkB,YAAM;AAC9BC,EAAAA,EAAE,CAAC,uBAAD,EAA0B,YAAM;AAChC,gCAAc,OAAd,EAAuBC,MAAvB,CAA8BC,KAA9B,CAAoC,IAApC;AACD,GAFC,CAAF;AAIAF,EAAAA,EAAE,CAAC,6DAAD,EAAgE,YAAM;AACtEG,IAAAA,MAAM,CAAC;AAAA,aAAM,4BAAc,IAAd,EAAoB;AAAEC,QAAAA,aAAa,EAAE;AAAjB,OAApB,CAAN;AAAA,KAAD,CAAN,CAAkEC,EAAlE,CAAqEC,KAArE,CAA2E,gCAA3E;AACD,GAFC,CAAF;AAIAN,EAAAA,EAAE,CAAC,4EAAD,EAA+E,YAAM;AACrF,gCAAc,OAAd,EAAuB;AAAEI,MAAAA,aAAa,EAAE;AAAjB,KAAvB,EAAsDH,MAAtD,CAA6DC,KAA7D,CAAmE,IAAnE;AACD,GAFC,CAAF;AAGD,CAZO,CAAR","sourcesContent":["import resolveLocale from './resolveLocale'\r\n\r\ndescribe('resolveLocale', () => {\r\n it('should resolve locale', () => {\r\n resolveLocale(\"en-XX\").should.equal(\"en\")\r\n })\r\n\r\n it('should throw when \"localeMatcher\" option is not a valid one', () => {\r\n expect(() => resolveLocale(\"en\", { localeMatcher: \"eccentric\" })).to.throw('Invalid \"localeMatcher\" option')\r\n })\r\n\r\n it('should fall back to \"lookup\" when passed \"best fit\" \"localeMatcher\" option', () => {\r\n resolveLocale(\"en-XX\", { localeMatcher: \"best fit\" }).should.equal(\"en\")\r\n })\r\n})"],"file":"resolveLocale.test.js"}