UNPKG

intl-relativeformat

Version:

Formats JavaScript dates to relative time strings.

126 lines 4.59 kB
/* Copyright (c) 2014, Yahoo! Inc. All rights reserved. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ import diff from './diff'; // ----------------------------------------------------------------------------- var SUPPORTED_FIELDS = [ "second" /* second */, "second-short" /* secondShort */, "minute" /* minute */, "minute-short" /* minuteShort */, "hour" /* hour */, "hour-short" /* hourShort */, "day" /* day */, "day-short" /* dayShort */, "month" /* month */, "month-short" /* monthShort */, "year" /* year */, "year-short" /* yearShort */ ]; function isValidUnits(units) { if (!units || ~SUPPORTED_FIELDS.indexOf(units)) { return true; } if (typeof units === 'string') { var suggestion = /s$/.test(units) && units.substr(0, units.length - 1); if (suggestion && ~SUPPORTED_FIELDS.indexOf(suggestion)) { throw new Error("\"" + units + "\" is not a valid IntlRelativeFormat 'units' value, did you mean: " + suggestion); } } throw new Error("\"" + units + "\" is not a valid IntlRelativeFormat 'units' value, it must be one of: " + SUPPORTED_FIELDS.join('", "')); } function resolveStyle(style) { // Default to "best fit" style. if (!style) { return "best fit" /* bestFit */; } if (style === 'best fit' || style === 'numeric') { return style; } throw new Error("\"" + style + "\" is not a valid IntlRelativeFormat 'style' value, it must be one of: 'best fit' or 'numeric'"); } function selectUnits(diffReport) { var fields = SUPPORTED_FIELDS.filter(function (field) { return field.indexOf('-short') < 1; }); var units = fields[0]; for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) { units = fields_1[_i]; if (Math.abs(diffReport[units]) < RelativeFormat.thresholds[units]) { break; } } return units; } var RelativeFormat = (function (locales, options) { if (options === void 0) { options = {}; } var resolvedOptions = { style: resolveStyle(options.style), units: isValidUnits(options.units) && options.units }; var numeric = resolvedOptions.style === 'best fit' ? 'auto' : 'always'; var rtf = new Intl.RelativeTimeFormat(locales, { numeric: numeric }); return { format: function (date, options) { var now = options && options.now !== undefined ? options.now === null ? 0 : options.now : Date.now(); if (date === undefined) { date = now; } // Determine if the `date` and optional `now` values are valid, and throw a // similar error to what `Intl.DateTimeFormat#format()` would throw. if (!isFinite(now)) { throw new RangeError('The `now` option provided to IntlRelativeFormat#format() is not ' + 'in valid range.'); } if (!isFinite(date)) { throw new RangeError('The date value provided to IntlRelativeFormat#format() is not ' + 'in valid range.'); } var diffReport = diff(now, date); var units = resolvedOptions.units || selectUnits(diffReport); var diffInUnits = diffReport[units]; var style = units.substring(units.length - 6, units.length) === '-short' ? 'narrow' : 'long'; var rtfUnit = units.replace('-short', ''); return new Intl.RelativeTimeFormat(locales, { numeric: numeric, style: style }).format(diffInUnits, rtfUnit); }, resolvedOptions: function () { return { locale: rtf.resolvedOptions().locale, style: resolvedOptions.style, units: resolvedOptions.units }; } }; }); // Define public `defaultLocale` property which can be set by the developer, or // it will be set when the first RelativeFormat instance is created by // leveraging the resolved locale from `Intl`. RelativeFormat.defaultLocale = 'en'; RelativeFormat.thresholds = { second: 45, 'second-short': 45, minute: 45, 'minute-short': 45, hour: 22, 'hour-short': 22, day: 26, 'day-short': 26, month: 11, 'month-short': 11 // months to year }; export default RelativeFormat; //# sourceMappingURL=index.js.map