芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/app.optimyar.com/backend/node_modules/intl-messageformat/lib/formatters.js
import { convertNumberSkeletonToNumberFormatOptions, isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, parseDateTimeSkeleton, isTagElement, } from 'intl-messageformat-parser'; import { MissingValueError, InvalidValueError, 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 || (PART_TYPE = {})); function mergeLiteral(parts) { if (parts.length < 2) { return parts; } return parts.reduce(function (all, part) { var lastPart = all[all.length - 1]; if (!lastPart || lastPart.type !== 0 /* literal */ || part.type !== 0 /* literal */) { all.push(part); } else { lastPart.value += part.value; } return all; }, []); } function isFormatXMLElementFn(el) { return typeof el === 'function'; } // TODO(skeleton): add skeleton support export function formatToParts(els, locales, formatters, formats, values, currentPluralValue, // For debugging originalMessage) { // Hot path for straight simple msg translations if (els.length === 1 && isLiteralElement(els[0])) { return [ { type: 0 /* literal */, value: els[0].value, }, ]; } var result = []; for (var _i = 0, els_1 = els; _i < els_1.length; _i++) { var el = els_1[_i]; // Exit early for string parts. if (isLiteralElement(el)) { result.push({ type: 0 /* literal */, value: el.value, }); continue; } // TODO: should this part be literal type? // Replace `#` in plural rules with the actual numeric value. if (isPoundElement(el)) { if (typeof currentPluralValue === 'number') { result.push({ type: 0 /* literal */, value: formatters.getNumberFormat(locales).format(currentPluralValue), }); } continue; } var varName = el.value; // Enforce that all required values are provided by the caller. if (!(values && varName in values)) { throw new MissingValueError(varName, originalMessage); } var value = values[varName]; if (isArgumentElement(el)) { if (!value || typeof value === 'string' || typeof value === 'number') { value = typeof value === 'string' || typeof value === 'number' ? String(value) : ''; } result.push({ type: typeof value === 'string' ? 0 /* literal */ : 1 /* object */, value: value, }); continue; } // Recursively format plural and select parts' option — which can be a // nested pattern structure. The choosing of the option to use is // abstracted-by and delegated-to the part helper object. if (isDateElement(el)) { var style = typeof el.style === 'string' ? formats.date[el.style] : isDateTimeSkeleton(el.style) ? parseDateTimeSkeleton(el.style.pattern) : undefined; result.push({ type: 0 /* literal */, value: formatters .getDateTimeFormat(locales, style) .format(value), }); continue; } if (isTimeElement(el)) { var style = typeof el.style === 'string' ? formats.time[el.style] : isDateTimeSkeleton(el.style) ? parseDateTimeSkeleton(el.style.pattern) : undefined; result.push({ type: 0 /* literal */, value: formatters .getDateTimeFormat(locales, style) .format(value), }); continue; } if (isNumberElement(el)) { var style = typeof el.style === 'string' ? formats.number[el.style] : isNumberSkeleton(el.style) ? convertNumberSkeletonToNumberFormatOptions(el.style.tokens) : undefined; result.push({ type: 0 /* literal */, value: formatters .getNumberFormat(locales, style) .format(value), }); continue; } if (isTagElement(el)) { var children = el.children, value_1 = el.value; var formatFn = values[value_1]; if (!isFormatXMLElementFn(formatFn)) { throw new InvalidValueTypeError(value_1, 'function', originalMessage); } var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue); var chunks = formatFn.apply(void 0, parts.map(function (p) { return p.value; })); if (!Array.isArray(chunks)) { chunks = [chunks]; } result.push.apply(result, chunks.map(function (c) { return { type: typeof c === 'string' ? 0 /* literal */ : 1 /* object */, value: c, }; })); } if (isSelectElement(el)) { var opt = el.options[value] || el.options.other; if (!opt) { throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); } result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values)); continue; } if (isPluralElement(el)) { var opt = el.options["=" + value]; if (!opt) { if (!Intl.PluralRules) { throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", "MISSING_INTL_API" /* MISSING_INTL_API */, originalMessage); } var rule = formatters .getPluralRules(locales, { type: el.pluralType }) .select(value - (el.offset || 0)); opt = el.options[rule] || el.options.other; } if (!opt) { throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); } result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0))); continue; } } return mergeLiteral(result); } //# sourceMappingURL=formatters.js.map