intl-messageformat
Advanced tools
Comparing version
import { IntlMessageFormat } from './src/core'; | ||
export * from './src/formatters'; | ||
export * from './src/core'; | ||
export * from './src/error'; | ||
export * from './src/formatters'; | ||
export { IntlMessageFormat }; | ||
export default IntlMessageFormat; |
@@ -8,7 +8,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IntlMessageFormat = void 0; | ||
var tslib_1 = require("tslib"); | ||
var core_1 = require("./src/core"); | ||
tslib_1.__exportStar(require("./src/formatters"), exports); | ||
Object.defineProperty(exports, "IntlMessageFormat", { enumerable: true, get: function () { return core_1.IntlMessageFormat; } }); | ||
tslib_1.__exportStar(require("./src/core"), exports); | ||
tslib_1.__exportStar(require("./src/error"), exports); | ||
tslib_1.__exportStar(require("./src/formatters"), exports); | ||
exports.default = core_1.IntlMessageFormat; |
import { IntlMessageFormat } from './src/core'; | ||
export * from './src/formatters'; | ||
export * from './src/core'; | ||
export * from './src/error'; | ||
export * from './src/formatters'; | ||
export { IntlMessageFormat }; | ||
export default IntlMessageFormat; |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
/* | ||
@@ -7,8 +6,7 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var core_1 = require("./src/core"); | ||
tslib_1.__exportStar(require("./src/formatters"), exports); | ||
tslib_1.__exportStar(require("./src/core"), exports); | ||
tslib_1.__exportStar(require("./src/error"), exports); | ||
exports.default = core_1.IntlMessageFormat; | ||
import { IntlMessageFormat } from './src/core'; | ||
export * from './src/core'; | ||
export * from './src/error'; | ||
export * from './src/formatters'; | ||
export { IntlMessageFormat }; | ||
export default IntlMessageFormat; |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
/* | ||
@@ -7,8 +6,6 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IntlMessageFormat = void 0; | ||
var tslib_1 = require("tslib"); | ||
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser"); | ||
var fast_memoize_1 = require("@formatjs/fast-memoize"); | ||
var formatters_1 = require("./formatters"); | ||
import { __assign, __rest, __spreadArray } from "tslib"; | ||
import { parse, } from '@formatjs/icu-messageformat-parser'; | ||
import { memoize, strategies } from '@formatjs/fast-memoize'; | ||
import { formatToParts, PART_TYPE, } from './formatters'; | ||
// -- MessageFormat -------------------------------------------------------- | ||
@@ -19,4 +16,4 @@ function mergeConfig(c1, c2) { | ||
} | ||
return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) { | ||
all[k] = tslib_1.__assign(tslib_1.__assign({}, c1[k]), (c2[k] || {})); | ||
return __assign(__assign(__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) { | ||
all[k] = __assign(__assign({}, c1[k]), (c2[k] || {})); | ||
return all; | ||
@@ -32,3 +29,3 @@ }, {})); | ||
return all; | ||
}, tslib_1.__assign({}, defaultConfig)); | ||
}, __assign({}, defaultConfig)); | ||
} | ||
@@ -56,3 +53,3 @@ function createFastMemoizeCache(store) { | ||
return { | ||
getNumberFormat: (0, fast_memoize_1.memoize)(function () { | ||
getNumberFormat: memoize(function () { | ||
var _a; | ||
@@ -63,8 +60,8 @@ var args = []; | ||
} | ||
return new ((_a = Intl.NumberFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.number), | ||
strategy: fast_memoize_1.strategies.variadic, | ||
strategy: strategies.variadic, | ||
}), | ||
getDateTimeFormat: (0, fast_memoize_1.memoize)(function () { | ||
getDateTimeFormat: memoize(function () { | ||
var _a; | ||
@@ -75,8 +72,8 @@ var args = []; | ||
} | ||
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.dateTime), | ||
strategy: fast_memoize_1.strategies.variadic, | ||
strategy: strategies.variadic, | ||
}), | ||
getPluralRules: (0, fast_memoize_1.memoize)(function () { | ||
getPluralRules: memoize(function () { | ||
var _a; | ||
@@ -87,6 +84,6 @@ var args = []; | ||
} | ||
return new ((_a = Intl.PluralRules).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.pluralRules), | ||
strategy: fast_memoize_1.strategies.variadic, | ||
strategy: strategies.variadic, | ||
}), | ||
@@ -112,3 +109,3 @@ }; | ||
if (!all.length || | ||
part.type !== formatters_1.PART_TYPE.literal || | ||
part.type !== PART_TYPE.literal || | ||
typeof all[all.length - 1] !== 'string') { | ||
@@ -128,3 +125,3 @@ all.push(part.value); | ||
this.formatToParts = function (values) { | ||
return (0, formatters_1.formatToParts)(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message); | ||
return formatToParts(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message); | ||
}; | ||
@@ -147,5 +144,5 @@ this.resolvedOptions = function () { | ||
} | ||
var _a = opts || {}, formatters = _a.formatters, parseOpts = tslib_1.__rest(_a, ["formatters"]); | ||
var _a = opts || {}, formatters = _a.formatters, parseOpts = __rest(_a, ["formatters"]); | ||
// Parse string messages into an AST. | ||
this.ast = IntlMessageFormat.__parse(message, tslib_1.__assign(tslib_1.__assign({}, parseOpts), { locale: this.resolvedLocale })); | ||
this.ast = IntlMessageFormat.__parse(message, __assign(__assign({}, parseOpts), { locale: this.resolvedLocale })); | ||
} | ||
@@ -186,3 +183,3 @@ else { | ||
}; | ||
IntlMessageFormat.__parse = icu_messageformat_parser_1.parse; | ||
IntlMessageFormat.__parse = parse; | ||
// Default format options used as the prototype of the `formats` provided to the | ||
@@ -252,2 +249,2 @@ // constructor. These are used when constructing the internal Intl.NumberFormat | ||
}()); | ||
exports.IntlMessageFormat = IntlMessageFormat; | ||
export { IntlMessageFormat }; |
@@ -1,6 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MissingValueError = exports.InvalidValueTypeError = exports.InvalidValueError = exports.FormatError = exports.ErrorCode = void 0; | ||
var tslib_1 = require("tslib"); | ||
var ErrorCode; | ||
import { __extends } from "tslib"; | ||
export var ErrorCode; | ||
(function (ErrorCode) { | ||
@@ -13,5 +10,5 @@ // When we have a placeholder but no value to format | ||
ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API"; | ||
})(ErrorCode || (exports.ErrorCode = ErrorCode = {})); | ||
})(ErrorCode || (ErrorCode = {})); | ||
var FormatError = /** @class */ (function (_super) { | ||
tslib_1.__extends(FormatError, _super); | ||
__extends(FormatError, _super); | ||
function FormatError(msg, code, originalMessage) { | ||
@@ -28,5 +25,5 @@ var _this = _super.call(this, msg) || this; | ||
}(Error)); | ||
exports.FormatError = FormatError; | ||
export { FormatError }; | ||
var InvalidValueError = /** @class */ (function (_super) { | ||
tslib_1.__extends(InvalidValueError, _super); | ||
__extends(InvalidValueError, _super); | ||
function InvalidValueError(variableId, value, options, originalMessage) { | ||
@@ -37,5 +34,5 @@ return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
exports.InvalidValueError = InvalidValueError; | ||
export { InvalidValueError }; | ||
var InvalidValueTypeError = /** @class */ (function (_super) { | ||
tslib_1.__extends(InvalidValueTypeError, _super); | ||
__extends(InvalidValueTypeError, _super); | ||
function InvalidValueTypeError(value, type, originalMessage) { | ||
@@ -46,5 +43,5 @@ return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
exports.InvalidValueTypeError = InvalidValueTypeError; | ||
export { InvalidValueTypeError }; | ||
var MissingValueError = /** @class */ (function (_super) { | ||
tslib_1.__extends(MissingValueError, _super); | ||
__extends(MissingValueError, _super); | ||
function MissingValueError(variableId, originalMessage) { | ||
@@ -55,2 +52,2 @@ return _super.call(this, "The intl string context variable \"".concat(variableId, "\" was not provided to the string \"").concat(originalMessage, "\""), ErrorCode.MISSING_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
exports.MissingValueError = MissingValueError; | ||
export { MissingValueError }; |
@@ -1,11 +0,8 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatToParts = exports.isFormatXMLElementFn = exports.PART_TYPE = void 0; | ||
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser"); | ||
var error_1 = require("./error"); | ||
var PART_TYPE; | ||
import { isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, isTagElement, } from '@formatjs/icu-messageformat-parser'; | ||
import { MissingValueError, InvalidValueError, ErrorCode, FormatError, InvalidValueTypeError, } from './error'; | ||
export var PART_TYPE; | ||
(function (PART_TYPE) { | ||
PART_TYPE[PART_TYPE["literal"] = 0] = "literal"; | ||
PART_TYPE[PART_TYPE["object"] = 1] = "object"; | ||
})(PART_TYPE || (exports.PART_TYPE = PART_TYPE = {})); | ||
})(PART_TYPE || (PART_TYPE = {})); | ||
function mergeLiteral(parts) { | ||
@@ -28,12 +25,11 @@ if (parts.length < 2) { | ||
} | ||
function isFormatXMLElementFn(el) { | ||
export function isFormatXMLElementFn(el) { | ||
return typeof el === 'function'; | ||
} | ||
exports.isFormatXMLElementFn = isFormatXMLElementFn; | ||
// TODO(skeleton): add skeleton support | ||
function formatToParts(els, locales, formatters, formats, values, currentPluralValue, | ||
export function formatToParts(els, locales, formatters, formats, values, currentPluralValue, | ||
// For debugging | ||
originalMessage) { | ||
// Hot path for straight simple msg translations | ||
if (els.length === 1 && (0, icu_messageformat_parser_1.isLiteralElement)(els[0])) { | ||
if (els.length === 1 && isLiteralElement(els[0])) { | ||
return [ | ||
@@ -50,3 +46,3 @@ { | ||
// Exit early for string parts. | ||
if ((0, icu_messageformat_parser_1.isLiteralElement)(el)) { | ||
if (isLiteralElement(el)) { | ||
result.push({ | ||
@@ -60,3 +56,3 @@ type: PART_TYPE.literal, | ||
// Replace `#` in plural rules with the actual numeric value. | ||
if ((0, icu_messageformat_parser_1.isPoundElement)(el)) { | ||
if (isPoundElement(el)) { | ||
if (typeof currentPluralValue === 'number') { | ||
@@ -73,6 +69,6 @@ result.push({ | ||
if (!(values && varName in values)) { | ||
throw new error_1.MissingValueError(varName, originalMessage); | ||
throw new MissingValueError(varName, originalMessage); | ||
} | ||
var value = values[varName]; | ||
if ((0, icu_messageformat_parser_1.isArgumentElement)(el)) { | ||
if (isArgumentElement(el)) { | ||
if (!value || typeof value === 'string' || typeof value === 'number') { | ||
@@ -93,6 +89,6 @@ value = | ||
// abstracted-by and delegated-to the part helper object. | ||
if ((0, icu_messageformat_parser_1.isDateElement)(el)) { | ||
if (isDateElement(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.date[el.style] | ||
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style) | ||
: isDateTimeSkeleton(el.style) | ||
? el.style.parsedOptions | ||
@@ -108,6 +104,6 @@ : undefined; | ||
} | ||
if ((0, icu_messageformat_parser_1.isTimeElement)(el)) { | ||
if (isTimeElement(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.time[el.style] | ||
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style) | ||
: isDateTimeSkeleton(el.style) | ||
? el.style.parsedOptions | ||
@@ -123,6 +119,6 @@ : formats.time.medium; | ||
} | ||
if ((0, icu_messageformat_parser_1.isNumberElement)(el)) { | ||
if (isNumberElement(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.number[el.style] | ||
: (0, icu_messageformat_parser_1.isNumberSkeleton)(el.style) | ||
: isNumberSkeleton(el.style) | ||
? el.style.parsedOptions | ||
@@ -143,7 +139,7 @@ : undefined; | ||
} | ||
if ((0, icu_messageformat_parser_1.isTagElement)(el)) { | ||
if (isTagElement(el)) { | ||
var children = el.children, value_1 = el.value; | ||
var formatFn = values[value_1]; | ||
if (!isFormatXMLElementFn(formatFn)) { | ||
throw new error_1.InvalidValueTypeError(value_1, 'function', originalMessage); | ||
throw new InvalidValueTypeError(value_1, 'function', originalMessage); | ||
} | ||
@@ -162,6 +158,6 @@ var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue); | ||
} | ||
if ((0, icu_messageformat_parser_1.isSelectElement)(el)) { | ||
if (isSelectElement(el)) { | ||
var opt = el.options[value] || el.options.other; | ||
if (!opt) { | ||
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
} | ||
@@ -171,7 +167,7 @@ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values)); | ||
} | ||
if ((0, icu_messageformat_parser_1.isPluralElement)(el)) { | ||
if (isPluralElement(el)) { | ||
var opt = el.options["=".concat(value)]; | ||
if (!opt) { | ||
if (!Intl.PluralRules) { | ||
throw new error_1.FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", error_1.ErrorCode.MISSING_INTL_API, originalMessage); | ||
throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API, originalMessage); | ||
} | ||
@@ -184,3 +180,3 @@ var rule = formatters | ||
if (!opt) { | ||
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
} | ||
@@ -193,2 +189,1 @@ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0))); | ||
} | ||
exports.formatToParts = formatToParts; |
{ | ||
"name": "intl-messageformat", | ||
"version": "10.5.6", | ||
"version": "10.5.7", | ||
"description": "Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
224328
-25.1%7771
-12.45%