@ultraq/icu-message-formatter 0.15.0-beta.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/README.md +6 -6
- package/dist/icu-message-formatter.browser.es.min.js +1 -180
- package/dist/icu-message-formatter.browser.es.min.js.map +1 -1
- package/dist/icu-message-formatter.browser.min.js +1 -1
- package/dist/icu-message-formatter.browser.min.js.map +1 -1
- package/dist/icu-message-formatter.cjs +462 -219
- package/dist/icu-message-formatter.cjs.map +1 -1
- package/dist/icu-message-formatter.d.cts +153 -0
- package/dist/icu-message-formatter.d.ts +49 -54
- package/dist/icu-message-formatter.js +461 -225
- package/dist/icu-message-formatter.js.map +1 -1
- package/package.json +27 -18
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"icu-message-formatter.cjs","sources":["../source/utilities.js","../source/MessageFormatter.js","../source/pluralTypeHandler.js","../source/selectTypeHandler.js"],"sourcesContent":["/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * @typedef ParseCasesResult\n * @property {string[]} args\n * A list of prepended arguments.\n * @property {Record<string,string>} cases\n * A map of all cases.\n */\n\n/**\n * Most branch-based type handlers are based around \"cases\". For example,\n * `select` and `plural` compare compare a value to \"case keys\" to choose a\n * subtranslation.\n *\n * This util splits \"matches\" portions provided to the aforementioned handlers\n * into case strings, and extracts any prepended arguments (for example,\n * `plural` supports an `offset:n` argument used for populating the magic `#`\n * variable).\n *\n * @param {string} string\n * @return {ParseCasesResult}\n */\nexport function parseCases(string = '') {\n\tconst isWhitespace = ch => /\\s/.test(ch);\n\n\tconst args = [];\n\tconst cases = {};\n\n\tlet currTermStart = 0;\n\tlet latestTerm = null;\n\tlet inTerm = false;\n\n\tlet i = 0;\n\twhile (i < string.length) {\n\t\t// Term ended\n\t\tif (inTerm && (isWhitespace(string[i]) || string[i] === '{')) {\n\t\t\tinTerm = false;\n\t\t\tlatestTerm = string.slice(currTermStart, i);\n\n\t\t\t// We want to process the opening char again so the case will be properly registered.\n\t\t\tif (string[i] === '{') {\n\t\t\t\ti--;\n\t\t\t}\n\t\t}\n\n\t\t// New term\n\t\telse if (!inTerm && !isWhitespace(string[i])) {\n\t\t\tconst caseBody = string[i] === '{';\n\n\t\t\t// If there's a previous term, we can either handle a whole\n\t\t\t// case, or add that as an argument.\n\t\t\tif (latestTerm && caseBody) {\n\t\t\t\tconst branchEndIndex = findClosingBracket(string, i);\n\n\t\t\t\tif (branchEndIndex === -1) {\n\t\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${string}\"`);\n\t\t\t\t}\n\n\t\t\t\tcases[latestTerm] = string.slice(i + 1, branchEndIndex); // Don't include the braces\n\n\t\t\t\ti = branchEndIndex; // Will be moved up where needed at end of loop.\n\t\t\t\tlatestTerm = null;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (latestTerm) {\n\t\t\t\t\targs.push(latestTerm);\n\t\t\t\t\tlatestTerm = null;\n\t\t\t\t}\n\n\t\t\t\tinTerm = true;\n\t\t\t\tcurrTermStart = i;\n\t\t\t}\n\t\t}\n\t\ti++;\n\t}\n\n\tif (inTerm) {\n\t\tlatestTerm = string.slice(currTermStart);\n\t}\n\n\tif (latestTerm) {\n\t\targs.push(latestTerm);\n\t}\n\n\treturn {\n\t\targs,\n\t\tcases\n\t};\n}\n\n/**\n * Finds the index of the matching closing curly bracket, including through\n * strings that could have nested brackets.\n *\n * @param {string} string\n * @param {number} fromIndex\n * @return {number}\n * The index of the matching closing bracket, or -1 if no closing bracket\n * could be found.\n */\nexport function findClosingBracket(string, fromIndex) {\n\tlet depth = 0;\n\tfor (let i = fromIndex + 1; i < string.length; i++) {\n\t\tlet char = string.charAt(i);\n\t\tif (char === '}') {\n\t\t\tif (depth === 0) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t\tdepth--;\n\t\t}\n\t\telse if (char === '{') {\n\t\t\tdepth++;\n\t\t}\n\t}\n\treturn -1;\n}\n\n/**\n * Split a `{key, type, format}` block into those 3 parts, taking into account\n * nested message syntax that can exist in the `format` part.\n *\n * @param {string} block\n * @return {string[]}\n * An array with `key`, `type`, and `format` items in that order, if present\n * in the formatted argument block.\n */\nexport function splitFormattedArgument(block) {\n\treturn split(block.slice(1, -1), ',', 3);\n}\n\n/**\n * Like `String.prototype.split()` but where the limit parameter causes the\n * remainder of the string to be grouped together in a final entry.\n *\n * @private\n * @param {string} string\n * @param {string} separator\n * @param {number} limit\n * @param {string[]} accumulator\n * @return {string[]}\n */\nfunction split(string, separator, limit, accumulator = []) {\n\tif (!string) {\n\t\treturn accumulator;\n\t}\n\tif (limit === 1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet indexOfDelimiter = string.indexOf(separator);\n\tif (indexOfDelimiter === -1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet head = string.substring(0, indexOfDelimiter).trim();\n\tlet tail = string.substring(indexOfDelimiter + separator.length + 1).trim();\n\taccumulator.push(head);\n\treturn split(tail, separator, limit - 1, accumulator);\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {findClosingBracket, splitFormattedArgument} from './utilities.js';\n\nimport {memoize} from '@ultraq/function-utils';\n\n/**\n * @typedef {Record<string,any>} FormatValues\n */\n\n/**\n * @callback ProcessFunction\n * @param {string} message\n * @param {FormatValues} [values={}]\n * @return {any[]}\n */\n\n/**\n * @callback TypeHandler\n * @param {any} value\n * The object which matched the key of the block being processed.\n * @param {string} matches\n * Any format options associated with the block being processed.\n * @param {string} locale\n * The locale to use for formatting.\n * @param {FormatValues} values\n * The object of placeholder data given to the original `format`/`process`\n * call.\n * @param {ProcessFunction} process\n * The `process` function itself so that sub-messages can be processed by type\n * handlers.\n * @return {any | any[]}\n */\n\n/**\n * The main class for formatting messages.\n *\n * @author Emanuel Rabina\n */\nexport default class MessageFormatter {\n\n\t/**\n\t * Creates a new formatter that can work using any of the custom type handlers\n\t * you register.\n\t *\n\t * @param {string} locale\n\t * @param {Record<string,TypeHandler>} [typeHandlers]\n\t * Optional object where the keys are the names of the types to register,\n\t * their values being the functions that will return a nicely formatted\n\t * string for the data and locale they are given.\n\t */\n\tconstructor(locale, typeHandlers = {}) {\n\n\t\tthis.locale = locale;\n\t\tthis.typeHandlers = typeHandlers;\n\t}\n\n\t/**\n\t * Formats an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers.\n\t *\n\t * @type {(message: string, values?: FormatValues) => string}\n\t */\n\tformat = memoize((message, values = {}) => {\n\n\t\treturn this.process(message, values).flat(Infinity).join('');\n\t});\n\n\t/**\n\t * Process an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers. The result of this method is\n\t * an array of the component parts after they have been processed in turn by\n\t * their own type handlers. This raw output is useful for other renderers,\n\t * eg: React where components can be used instead of being forced to return\n\t * raw strings.\n\t *\n\t * This method is used by {@link MessageFormatter#format} where it acts as a\n\t * string renderer.\n\t *\n\t * @param {string} message\n\t * @param {FormatValues} [values]\n\t * @return {any[]}\n\t */\n\tprocess(message, values = {}) {\n\n\t\tif (!message) {\n\t\t\treturn [];\n\t\t}\n\n\t\tlet blockStartIndex = message.indexOf('{');\n\t\tif (blockStartIndex !== -1) {\n\t\t\tlet blockEndIndex = findClosingBracket(message, blockStartIndex);\n\t\t\tif (blockEndIndex !== -1) {\n\t\t\t\tlet block = message.substring(blockStartIndex, blockEndIndex + 1);\n\t\t\t\tif (block) {\n\t\t\t\t\tlet result = [];\n\t\t\t\t\tlet head = message.substring(0, blockStartIndex);\n\t\t\t\t\tif (head) {\n\t\t\t\t\t\tresult.push(head);\n\t\t\t\t\t}\n\t\t\t\t\tlet [key, type, format] = splitFormattedArgument(block);\n\t\t\t\t\tlet body = values[key];\n\t\t\t\t\tif (body === null || body === undefined) {\n\t\t\t\t\t\tbody = '';\n\t\t\t\t\t}\n\t\t\t\t\tlet typeHandler = type && this.typeHandlers[type];\n\t\t\t\t\tresult.push(typeHandler ?\n\t\t\t\t\t\ttypeHandler(body, format, this.locale, values, this.process.bind(this)) :\n\t\t\t\t\t\tbody);\n\t\t\t\t\tlet tail = message.substring(blockEndIndex + 1);\n\t\t\t\t\tif (tail) {\n\t\t\t\t\t\tresult.push(this.process(tail, values));\n\t\t\t\t\t}\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${message}\"`);\n\t\t\t}\n\t\t}\n\t\treturn [message];\n\t}\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nlet pluralFormatter;\n\nlet keyCounter = 0;\n\n// All the special keywords that can be used in `plural` blocks for the various branches\nconst ONE = 'one';\nconst OTHER = 'other';\n\n/**\n * @private\n * @param {string} caseBody\n * @param {number} value\n * @return {{caseBody: string, numberValues: object}}\n */\nfunction replaceNumberSign(caseBody, value) {\n\tlet i = 0;\n\tlet output = '';\n\tlet numBraces = 0;\n\tconst numberValues = {};\n\n\twhile (i < caseBody.length) {\n\t\tif (caseBody[i] === '#' && !numBraces) {\n\t\t\tlet keyParam = `__hashToken${keyCounter++}`;\n\t\t\toutput += `{${keyParam}, number}`;\n\t\t\tnumberValues[keyParam] = value;\n\t\t}\n\t\telse {\n\t\t\toutput += caseBody[i];\n\t\t}\n\n\t\tif (caseBody[i] === '{') {\n\t\t\tnumBraces++;\n\t\t}\n\t\telse if (caseBody[i] === '}') {\n\t\t\tnumBraces--;\n\t\t}\n\n\t\ti++;\n\t}\n\n\treturn {\n\t\tcaseBody: output,\n\t\tnumberValues\n\t};\n}\n\n/**\n * Handler for `plural` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n *\n * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more\n * details on how the `plural` statement works.\n *\n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {import('./MessageFormatter.js').FormatValues} values\n * @param {import('./MessageFormatter.js').ProcessFunction} process\n * @return {any | any[]}\n */\nexport default function pluralTypeHandler(value, matches, locale, values, process) {\n\tconst {args, cases} = parseCases(matches);\n\n\tlet intValue = parseInt(value);\n\n\targs.forEach((arg) => {\n\t\tif (arg.startsWith('offset:')) {\n\t\t\tintValue -= parseInt(arg.slice('offset:'.length));\n\t\t}\n\t});\n\n\tconst keywordPossibilities = [];\n\n\tif ('PluralRules' in Intl) {\n\t\t// Effectively memoize because instantiation of `Int.*` objects is expensive.\n\t\tif (pluralFormatter === undefined || pluralFormatter.resolvedOptions().locale !== locale) {\n\t\t\tpluralFormatter = new Intl.PluralRules(locale);\n\t\t}\n\n\t\tconst pluralKeyword = pluralFormatter.select(intValue);\n\n\t\t// Other is always added last with least priority, so we don't want to add it here.\n\t\tif (pluralKeyword !== OTHER) {\n\t\t\tkeywordPossibilities.push(pluralKeyword);\n\t\t}\n\t}\n\tif (intValue === 1) {\n\t\tkeywordPossibilities.push(ONE);\n\t}\n\tkeywordPossibilities.push(`=${intValue}`, OTHER);\n\n\tfor (let i = 0; i < keywordPossibilities.length; i++) {\n\t\tconst keyword = keywordPossibilities[i];\n\t\tif (keyword in cases) {\n\t\t\tconst {caseBody, numberValues} = replaceNumberSign(cases[keyword], intValue);\n\t\t\treturn process(caseBody, {\n\t\t\t\t...values,\n\t\t\t\t...numberValues\n\t\t\t});\n\t\t}\n\t}\n\n\treturn value;\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nconst OTHER = 'other';\n\n/**\n * Handler for `select` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n *\n * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more\n * details on how the `select` statement works.\n *\n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {import('./MessageFormatter.js').FormatValues} values\n * @param {import('./MessageFormatter.js').ProcessFunction} process\n * @return {any | any[]}\n */\nexport default function selectTypeHandler(value, matches, locale, values, process) {\n\tconst {cases} = parseCases(matches);\n\n\tif (value in cases) {\n\t\treturn process(cases[value], values);\n\t}\n\telse if (OTHER in cases) {\n\t\treturn process(cases[OTHER], values);\n\t}\n\n\treturn value;\n}\n"],"names":["memoize","OTHER"],"mapp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C,SAAO;AACR;ACvGA,MAAM,QAAQ;AAgBC,SAAS,kBAAkB,OAAO,SAAS,QAAQ,QAAQ,SAAS;AAClF,QAAM,EAAC,MAAK,IAAI,WAAW,OAAO;AAElC,MAAI,SAAS,OAAO;AACnB,WAAO,QAAQ,MAAM,KAAK,GAAG,MAAM;AAAA,EACrC,WACU,SAAS,OAAO;AACxB,WAAO,QAAQ,MAAM,KAAK,GAAG,MAAM;AAAA,EACrC;AAEC,SAAO;AACR;;;;;;;"}
|
1
|
+
{"version":3,"file":"icu-message-formatter.cjs","sources":["../source/utilities.js","../source/MessageFormatter.js","../source/pluralTypeHandler.js","../source/selectTypeHandler.js"],"sourcesContent":["/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * @typedef ParseCasesResult\n * @property {string[]} args\n * A list of prepended arguments.\n * @property {Record<string,string>} cases\n * A map of all cases.\n */\n\n/**\n * Most branch-based type handlers are based around \"cases\". For example,\n * `select` and `plural` compare compare a value to \"case keys\" to choose a\n * subtranslation.\n *\n * This util splits \"matches\" portions provided to the aforementioned handlers\n * into case strings, and extracts any prepended arguments (for example,\n * `plural` supports an `offset:n` argument used for populating the magic `#`\n * variable).\n *\n * @param {string} string\n * @return {ParseCasesResult}\n */\nexport function parseCases(string = '') {\n\tconst isWhitespace = ch => /\\s/.test(ch);\n\n\tconst args = [];\n\tconst cases = {};\n\n\tlet currTermStart = 0;\n\tlet latestTerm = null;\n\tlet inTerm = false;\n\n\tlet i = 0;\n\twhile (i < string.length) {\n\t\t// Term ended\n\t\tif (inTerm && (isWhitespace(string[i]) || string[i] === '{')) {\n\t\t\tinTerm = false;\n\t\t\tlatestTerm = string.slice(currTermStart, i);\n\n\t\t\t// We want to process the opening char again so the case will be properly registered.\n\t\t\tif (string[i] === '{') {\n\t\t\t\ti--;\n\t\t\t}\n\t\t}\n\n\t\t// New term\n\t\telse if (!inTerm && !isWhitespace(string[i])) {\n\t\t\tconst caseBody = string[i] === '{';\n\n\t\t\t// If there's a previous term, we can either handle a whole\n\t\t\t// case, or add that as an argument.\n\t\t\tif (latestTerm && caseBody) {\n\t\t\t\tconst branchEndIndex = findClosingBracket(string, i);\n\n\t\t\t\tif (branchEndIndex === -1) {\n\t\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${string}\"`);\n\t\t\t\t}\n\n\t\t\t\tcases[latestTerm] = string.slice(i + 1, branchEndIndex); // Don't include the braces\n\n\t\t\t\ti = branchEndIndex; // Will be moved up where needed at end of loop.\n\t\t\t\tlatestTerm = null;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (latestTerm) {\n\t\t\t\t\targs.push(latestTerm);\n\t\t\t\t\tlatestTerm = null;\n\t\t\t\t}\n\n\t\t\t\tinTerm = true;\n\t\t\t\tcurrTermStart = i;\n\t\t\t}\n\t\t}\n\t\ti++;\n\t}\n\n\tif (inTerm) {\n\t\tlatestTerm = string.slice(currTermStart);\n\t}\n\n\tif (latestTerm) {\n\t\targs.push(latestTerm);\n\t}\n\n\treturn {\n\t\targs,\n\t\tcases\n\t};\n}\n\n/**\n * Finds the index of the matching closing curly bracket, including through\n * strings that could have nested brackets.\n *\n * @param {string} string\n * @param {number} fromIndex\n * @return {number}\n * The index of the matching closing bracket, or -1 if no closing bracket\n * could be found.\n */\nexport function findClosingBracket(string, fromIndex) {\n\tlet depth = 0;\n\tfor (let i = fromIndex + 1; i < string.length; i++) {\n\t\tlet char = string.charAt(i);\n\t\tif (char === '}') {\n\t\t\tif (depth === 0) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t\tdepth--;\n\t\t}\n\t\telse if (char === '{') {\n\t\t\tdepth++;\n\t\t}\n\t}\n\treturn -1;\n}\n\n/**\n * Split a `{key, type, format}` block into those 3 parts, taking into account\n * nested message syntax that can exist in the `format` part.\n *\n * @param {string} block\n * @return {string[]}\n * An array with `key`, `type`, and `format` items in that order, if present\n * in the formatted argument block.\n */\nexport function splitFormattedArgument(block) {\n\treturn split(block.slice(1, -1), ',', 3);\n}\n\n/**\n * Like `String.prototype.split()` but where the limit parameter causes the\n * remainder of the string to be grouped together in a final entry.\n *\n * @private\n * @param {string} string\n * @param {string} separator\n * @param {number} limit\n * @param {string[]} accumulator\n * @return {string[]}\n */\nfunction split(string, separator, limit, accumulator = []) {\n\tif (!string) {\n\t\treturn accumulator;\n\t}\n\tif (limit === 1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet indexOfDelimiter = string.indexOf(separator);\n\tif (indexOfDelimiter === -1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet head = string.substring(0, indexOfDelimiter).trim();\n\tlet tail = string.substring(indexOfDelimiter + separator.length + 1).trim();\n\taccumulator.push(head);\n\treturn split(tail, separator, limit - 1, accumulator);\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {findClosingBracket, splitFormattedArgument} from './utilities.js';\n\nimport {memoize} from '@ultraq/function-utils';\n\n/**\n * @typedef {Record<string,any>} FormatValues\n */\n\n/**\n * @callback ProcessFunction\n * @param {string} message\n * @param {FormatValues} [values={}]\n * @return {any[]}\n */\n\n/**\n * @callback TypeHandler\n * @param {any} value\n * The object which matched the key of the block being processed.\n * @param {string} matches\n * Any format options associated with the block being processed.\n * @param {string} locale\n * The locale to use for formatting.\n * @param {FormatValues} values\n * The object of placeholder data given to the original `format`/`process`\n * call.\n * @param {ProcessFunction} process\n * The `process` function itself so that sub-messages can be processed by type\n * handlers.\n * @return {any | any[]}\n */\n\n/**\n * The main class for formatting messages.\n *\n * @author Emanuel Rabina\n */\nexport default class MessageFormatter {\n\n\t/**\n\t * Creates a new formatter that can work using any of the custom type handlers\n\t * you register.\n\t *\n\t * @param {string} locale\n\t * @param {Record<string,TypeHandler>} [typeHandlers]\n\t * Optional object where the keys are the names of the types to register,\n\t * their values being the functions that will return a nicely formatted\n\t * string for the data and locale they are given.\n\t */\n\tconstructor(locale, typeHandlers = {}) {\n\n\t\tthis.locale = locale;\n\t\tthis.typeHandlers = typeHandlers;\n\t}\n\n\t/**\n\t * Formats an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers.\n\t *\n\t * @type {(message: string, values?: FormatValues) => string}\n\t */\n\tformat = memoize((message, values = {}) => {\n\n\t\treturn this.process(message, values).flat(Infinity).join('');\n\t});\n\n\t/**\n\t * Process an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers. The result of this method is\n\t * an array of the component parts after they have been processed in turn by\n\t * their own type handlers. This raw output is useful for other renderers,\n\t * eg: React where components can be used instead of being forced to return\n\t * raw strings.\n\t *\n\t * This method is used by {@link MessageFormatter#format} where it acts as a\n\t * string renderer.\n\t *\n\t * @param {string} message\n\t * @param {FormatValues} [values]\n\t * @return {any[]}\n\t */\n\tprocess(message, values = {}) {\n\n\t\tif (!message) {\n\t\t\treturn [];\n\t\t}\n\n\t\tlet blockStartIndex = message.indexOf('{');\n\t\tif (blockStartIndex !== -1) {\n\t\t\tlet blockEndIndex = findClosingBracket(message, blockStartIndex);\n\t\t\tif (blockEndIndex !== -1) {\n\t\t\t\tlet block = message.substring(blockStartIndex, blockEndIndex + 1);\n\t\t\t\tif (block) {\n\t\t\t\t\tlet result = [];\n\t\t\t\t\tlet head = message.substring(0, blockStartIndex);\n\t\t\t\t\tif (head) {\n\t\t\t\t\t\tresult.push(head);\n\t\t\t\t\t}\n\t\t\t\t\tlet [key, type, format] = splitFormattedArgument(block);\n\t\t\t\t\tlet body = values[key];\n\t\t\t\t\tlet typeHandler = type && this.typeHandlers[type];\n\t\t\t\t\tresult.push(typeHandler ?\n\t\t\t\t\t\ttypeHandler(body, format, this.locale, values, this.process.bind(this)) :\n\t\t\t\t\t\tbody);\n\t\t\t\t\tlet tail = message.substring(blockEndIndex + 1);\n\t\t\t\t\tif (tail) {\n\t\t\t\t\t\tresult.push(this.process(tail, values));\n\t\t\t\t\t}\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${message}\"`);\n\t\t\t}\n\t\t}\n\t\treturn [message];\n\t}\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nlet pluralFormatter;\n\nlet keyCounter = 0;\n\n// All the special keywords that can be used in `plural` blocks for the various branches\nconst ONE = 'one';\nconst OTHER = 'other';\n\n/**\n * @private\n * @param {string} caseBody\n * @param {number} value\n * @return {{caseBody: string, numberValues: object}}\n */\nfunction replaceNumberSign(caseBody, value) {\n\tlet i = 0;\n\tlet output = '';\n\tlet numBraces = 0;\n\tconst numberValues = {};\n\n\twhile (i < caseBody.length) {\n\t\tif (caseBody[i] === '#' && !numBraces) {\n\t\t\tlet keyParam = `__hashToken${keyCounter++}`;\n\t\t\toutput += `{${keyParam}, number}`;\n\t\t\tnumberValues[keyParam] = value;\n\t\t}\n\t\telse {\n\t\t\toutput += caseBody[i];\n\t\t}\n\n\t\tif (caseBody[i] === '{') {\n\t\t\tnumBraces++;\n\t\t}\n\t\telse if (caseBody[i] === '}') {\n\t\t\tnumBraces--;\n\t\t}\n\n\t\ti++;\n\t}\n\n\treturn {\n\t\tcaseBody: output,\n\t\tnumberValues\n\t};\n}\n\n/**\n * Handler for `plural` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n *\n * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more\n * details on how the `plural` statement works.\n *\n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {import('./MessageFormatter.js').FormatValues} values\n * @param {import('./MessageFormatter.js').ProcessFunction} process\n * @return {any | any[]}\n */\nexport default function pluralTypeHandler(value, matches, locale, values, process) {\n\tconst {args, cases} = parseCases(matches);\n\n\tlet intValue = parseInt(value);\n\n\targs.forEach((arg) => {\n\t\tif (arg.startsWith('offset:')) {\n\t\t\tintValue -= parseInt(arg.slice('offset:'.length));\n\t\t}\n\t});\n\n\tconst keywordPossibilities = [];\n\n\tif ('PluralRules' in Intl) {\n\t\t// Effectively memoize because instantiation of `Int.*` objects is expensive.\n\t\tif (pluralFormatter === undefined || pluralFormatter.resolvedOptions().locale !== locale) {\n\t\t\tpluralFormatter = new Intl.PluralRules(locale);\n\t\t}\n\n\t\tconst pluralKeyword = pluralFormatter.select(intValue);\n\n\t\t// Other is always added last with least priority, so we don't want to add it here.\n\t\tif (pluralKeyword !== OTHER) {\n\t\t\tkeywordPossibilities.push(pluralKeyword);\n\t\t}\n\t}\n\tif (intValue === 1) {\n\t\tkeywordPossibilities.push(ONE);\n\t}\n\tkeywordPossibilities.push(`=${intValue}`, OTHER);\n\n\tfor (let i = 0; i < keywordPossibilities.length; i++) {\n\t\tconst keyword = keywordPossibilities[i];\n\t\tif (keyword in cases) {\n\t\t\tconst {caseBody, numberValues} = replaceNumberSign(cases[keyword], intValue);\n\t\t\treturn process(caseBody, {\n\t\t\t\t...values,\n\t\t\t\t...numberValues\n\t\t\t});\n\t\t}\n\t}\n\n\treturn value;\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nconst OTHER = 'other';\n\n/**\n * Handler for `select` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n *\n * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more\n * details on how the `select` statement works.\n *\n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {import('./MessageFormatter.js').FormatValues} values\n * @param {import('./MessageFormatter.js').ProcessFunction} process\n * @return {any | any[]}\n */\nexport default function selectTypeHandler(value, matches, locale, values, process) {\n\tconst {cases} = parseCases(matches);\n\n\tif (value in cases) {\n\t\treturn process(cases[value], values);\n\t}\n\telse if (OTHER in cases) {\n\t\treturn process(cases[OTHER], values);\n\t}\n\n\treturn value;\n}\n"],"names":["memoize","OTHER"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,UAAU,CAAC,MAAM,GAAG,EAAE,EAAE;AACxC,CAAC,MAAM,YAAY,GAAG,EAAE,I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;AACb;;ACzHA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAIA,MAAM,KAAK,GAAG,OAAO;;AAErB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAAS,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;AACnF,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC;;AAEpC,CAAC,IAAI,KAAK,IAAI,KAAK,EAAE;AACrB,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;AACtC,CAAC;AACD,MAAM,IAAI,KAAK,IAAI,KAAK,EAAE;AAC1B,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;AACtC,CAAC;;AAED,CAAC,OAAO,KAAK;AACb;;;;;;;;;"}
|
@@ -0,0 +1,153 @@
|
|
1
|
+
export type ParseCasesResult = {
|
2
|
+
/**
|
3
|
+
* A list of prepended arguments.
|
4
|
+
*/
|
5
|
+
args: string[];
|
6
|
+
/**
|
7
|
+
* A map of all cases.
|
8
|
+
*/
|
9
|
+
cases: Record<string, string>;
|
10
|
+
};
|
11
|
+
export type FormatValues = Record<string, any>;
|
12
|
+
export type ProcessFunction = (message: string, values?: FormatValues) => any[];
|
13
|
+
export type TypeHandler = (value: any, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => any | any[];
|
14
|
+
/**
|
15
|
+
* @typedef {Record<string,any>} FormatValues
|
16
|
+
*/
|
17
|
+
/**
|
18
|
+
* @callback ProcessFunction
|
19
|
+
* @param {string} message
|
20
|
+
* @param {FormatValues} [values={}]
|
21
|
+
* @return {any[]}
|
22
|
+
*/
|
23
|
+
/**
|
24
|
+
* @callback TypeHandler
|
25
|
+
* @param {any} value
|
26
|
+
* The object which matched the key of the block being processed.
|
27
|
+
* @param {string} matches
|
28
|
+
* Any format options associated with the block being processed.
|
29
|
+
* @param {string} locale
|
30
|
+
* The locale to use for formatting.
|
31
|
+
* @param {FormatValues} values
|
32
|
+
* The object of placeholder data given to the original `format`/`process`
|
33
|
+
* call.
|
34
|
+
* @param {ProcessFunction} process
|
35
|
+
* The `process` function itself so that sub-messages can be processed by type
|
36
|
+
* handlers.
|
37
|
+
* @return {any | any[]}
|
38
|
+
*/
|
39
|
+
/**
|
40
|
+
* The main class for formatting messages.
|
41
|
+
*
|
42
|
+
* @author Emanuel Rabina
|
43
|
+
*/
|
44
|
+
export class MessageFormatter {
|
45
|
+
/**
|
46
|
+
* Creates a new formatter that can work using any of the custom type handlers
|
47
|
+
* you register.
|
48
|
+
*
|
49
|
+
* @param {string} locale
|
50
|
+
* @param {Record<string,TypeHandler>} [typeHandlers]
|
51
|
+
* Optional object where the keys are the names of the types to register,
|
52
|
+
* their values being the functions that will return a nicely formatted
|
53
|
+
* string for the data and locale they are given.
|
54
|
+
*/
|
55
|
+
constructor(locale: string, typeHandlers?: Record<string, TypeHandler>);
|
56
|
+
locale: string;
|
57
|
+
typeHandlers: Record<string, TypeHandler>;
|
58
|
+
/**
|
59
|
+
* Formats an ICU message syntax string using `values` for placeholder data
|
60
|
+
* and any currently-registered type handlers.
|
61
|
+
*
|
62
|
+
* @type {(message: string, values?: FormatValues) => string}
|
63
|
+
*/
|
64
|
+
format: (message: string, values?: FormatValues) => string;
|
65
|
+
/**
|
66
|
+
* Process an ICU message syntax string using `values` for placeholder data
|
67
|
+
* and any currently-registered type handlers. The result of this method is
|
68
|
+
* an array of the component parts after they have been processed in turn by
|
69
|
+
* their own type handlers. This raw output is useful for other renderers,
|
70
|
+
* eg: React where components can be used instead of being forced to return
|
71
|
+
* raw strings.
|
72
|
+
*
|
73
|
+
* This method is used by {@link MessageFormatter#format} where it acts as a
|
74
|
+
* string renderer.
|
75
|
+
*
|
76
|
+
* @param {string} message
|
77
|
+
* @param {FormatValues} [values]
|
78
|
+
* @return {any[]}
|
79
|
+
*/
|
80
|
+
process(message: string, values?: FormatValues): any[];
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* Finds the index of the matching closing curly bracket, including through
|
84
|
+
* strings that could have nested brackets.
|
85
|
+
*
|
86
|
+
* @param {string} string
|
87
|
+
* @param {number} fromIndex
|
88
|
+
* @return {number}
|
89
|
+
* The index of the matching closing bracket, or -1 if no closing bracket
|
90
|
+
* could be found.
|
91
|
+
*/
|
92
|
+
export function findClosingBracket(string: string, fromIndex: number): number;
|
93
|
+
/**
|
94
|
+
* @typedef ParseCasesResult
|
95
|
+
* @property {string[]} args
|
96
|
+
* A list of prepended arguments.
|
97
|
+
* @property {Record<string,string>} cases
|
98
|
+
* A map of all cases.
|
99
|
+
*/
|
100
|
+
/**
|
101
|
+
* Most branch-based type handlers are based around "cases". For example,
|
102
|
+
* `select` and `plural` compare compare a value to "case keys" to choose a
|
103
|
+
* subtranslation.
|
104
|
+
*
|
105
|
+
* This util splits "matches" portions provided to the aforementioned handlers
|
106
|
+
* into case strings, and extracts any prepended arguments (for example,
|
107
|
+
* `plural` supports an `offset:n` argument used for populating the magic `#`
|
108
|
+
* variable).
|
109
|
+
*
|
110
|
+
* @param {string} string
|
111
|
+
* @return {ParseCasesResult}
|
112
|
+
*/
|
113
|
+
export function parseCases(string?: string): ParseCasesResult;
|
114
|
+
/**
|
115
|
+
* Handler for `plural` statements within ICU message syntax strings. Returns
|
116
|
+
* a formatted string for the branch that closely matches the current value.
|
117
|
+
*
|
118
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
|
119
|
+
* details on how the `plural` statement works.
|
120
|
+
*
|
121
|
+
* @param {string} value
|
122
|
+
* @param {string} matches
|
123
|
+
* @param {string} locale
|
124
|
+
* @param {import('./MessageFormatter.js').FormatValues} values
|
125
|
+
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
126
|
+
* @return {any | any[]}
|
127
|
+
*/
|
128
|
+
export function pluralTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
129
|
+
/**
|
130
|
+
* Handler for `select` statements within ICU message syntax strings. Returns
|
131
|
+
* a formatted string for the branch that closely matches the current value.
|
132
|
+
*
|
133
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
|
134
|
+
* details on how the `select` statement works.
|
135
|
+
*
|
136
|
+
* @param {string} value
|
137
|
+
* @param {string} matches
|
138
|
+
* @param {string} locale
|
139
|
+
* @param {import('./MessageFormatter.js').FormatValues} values
|
140
|
+
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
141
|
+
* @return {any | any[]}
|
142
|
+
*/
|
143
|
+
export function selectTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
144
|
+
/**
|
145
|
+
* Split a `{key, type, format}` block into those 3 parts, taking into account
|
146
|
+
* nested message syntax that can exist in the `format` part.
|
147
|
+
*
|
148
|
+
* @param {string} block
|
149
|
+
* @return {string[]}
|
150
|
+
* An array with `key`, `type`, and `format` items in that order, if present
|
151
|
+
* in the formatted argument block.
|
152
|
+
*/
|
153
|
+
export function splitFormattedArgument(block: string): string[];
|
@@ -1,3 +1,16 @@
|
|
1
|
+
export type ParseCasesResult = {
|
2
|
+
/**
|
3
|
+
* A list of prepended arguments.
|
4
|
+
*/
|
5
|
+
args: string[];
|
6
|
+
/**
|
7
|
+
* A map of all cases.
|
8
|
+
*/
|
9
|
+
cases: Record<string, string>;
|
10
|
+
};
|
11
|
+
export type FormatValues = Record<string, any>;
|
12
|
+
export type ProcessFunction = (message: string, values?: FormatValues) => any[];
|
13
|
+
export type TypeHandler = (value: any, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => any | any[];
|
1
14
|
/**
|
2
15
|
* @typedef {Record<string,any>} FormatValues
|
3
16
|
*/
|
@@ -28,7 +41,7 @@
|
|
28
41
|
*
|
29
42
|
* @author Emanuel Rabina
|
30
43
|
*/
|
31
|
-
|
44
|
+
export class MessageFormatter {
|
32
45
|
/**
|
33
46
|
* Creates a new formatter that can work using any of the custom type handlers
|
34
47
|
* you register.
|
@@ -66,10 +79,38 @@ declare class MessageFormatter {
|
|
66
79
|
*/
|
67
80
|
process(message: string, values?: FormatValues): any[];
|
68
81
|
}
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
82
|
+
/**
|
83
|
+
* Finds the index of the matching closing curly bracket, including through
|
84
|
+
* strings that could have nested brackets.
|
85
|
+
*
|
86
|
+
* @param {string} string
|
87
|
+
* @param {number} fromIndex
|
88
|
+
* @return {number}
|
89
|
+
* The index of the matching closing bracket, or -1 if no closing bracket
|
90
|
+
* could be found.
|
91
|
+
*/
|
92
|
+
export function findClosingBracket(string: string, fromIndex: number): number;
|
93
|
+
/**
|
94
|
+
* @typedef ParseCasesResult
|
95
|
+
* @property {string[]} args
|
96
|
+
* A list of prepended arguments.
|
97
|
+
* @property {Record<string,string>} cases
|
98
|
+
* A map of all cases.
|
99
|
+
*/
|
100
|
+
/**
|
101
|
+
* Most branch-based type handlers are based around "cases". For example,
|
102
|
+
* `select` and `plural` compare compare a value to "case keys" to choose a
|
103
|
+
* subtranslation.
|
104
|
+
*
|
105
|
+
* This util splits "matches" portions provided to the aforementioned handlers
|
106
|
+
* into case strings, and extracts any prepended arguments (for example,
|
107
|
+
* `plural` supports an `offset:n` argument used for populating the magic `#`
|
108
|
+
* variable).
|
109
|
+
*
|
110
|
+
* @param {string} string
|
111
|
+
* @return {ParseCasesResult}
|
112
|
+
*/
|
113
|
+
export function parseCases(string?: string): ParseCasesResult;
|
73
114
|
/**
|
74
115
|
* Handler for `plural` statements within ICU message syntax strings. Returns
|
75
116
|
* a formatted string for the branch that closely matches the current value.
|
@@ -84,8 +125,7 @@ type TypeHandler = (value: any, matches: string, locale: string, values: FormatV
|
|
84
125
|
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
85
126
|
* @return {any | any[]}
|
86
127
|
*/
|
87
|
-
|
88
|
-
|
128
|
+
export function pluralTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
89
129
|
/**
|
90
130
|
* Handler for `select` statements within ICU message syntax strings. Returns
|
91
131
|
* a formatted string for the branch that closely matches the current value.
|
@@ -100,40 +140,7 @@ declare function pluralTypeHandler(value: string, matches: string, locale: strin
|
|
100
140
|
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
101
141
|
* @return {any | any[]}
|
102
142
|
*/
|
103
|
-
|
104
|
-
|
105
|
-
/**
|
106
|
-
* @typedef ParseCasesResult
|
107
|
-
* @property {string[]} args
|
108
|
-
* A list of prepended arguments.
|
109
|
-
* @property {Record<string,string>} cases
|
110
|
-
* A map of all cases.
|
111
|
-
*/
|
112
|
-
/**
|
113
|
-
* Most branch-based type handlers are based around "cases". For example,
|
114
|
-
* `select` and `plural` compare compare a value to "case keys" to choose a
|
115
|
-
* subtranslation.
|
116
|
-
*
|
117
|
-
* This util splits "matches" portions provided to the aforementioned handlers
|
118
|
-
* into case strings, and extracts any prepended arguments (for example,
|
119
|
-
* `plural` supports an `offset:n` argument used for populating the magic `#`
|
120
|
-
* variable).
|
121
|
-
*
|
122
|
-
* @param {string} string
|
123
|
-
* @return {ParseCasesResult}
|
124
|
-
*/
|
125
|
-
declare function parseCases(string?: string): ParseCasesResult;
|
126
|
-
/**
|
127
|
-
* Finds the index of the matching closing curly bracket, including through
|
128
|
-
* strings that could have nested brackets.
|
129
|
-
*
|
130
|
-
* @param {string} string
|
131
|
-
* @param {number} fromIndex
|
132
|
-
* @return {number}
|
133
|
-
* The index of the matching closing bracket, or -1 if no closing bracket
|
134
|
-
* could be found.
|
135
|
-
*/
|
136
|
-
declare function findClosingBracket(string: string, fromIndex: number): number;
|
143
|
+
export function selectTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
137
144
|
/**
|
138
145
|
* Split a `{key, type, format}` block into those 3 parts, taking into account
|
139
146
|
* nested message syntax that can exist in the `format` part.
|
@@ -143,16 +150,4 @@ declare function findClosingBracket(string: string, fromIndex: number): number;
|
|
143
150
|
* An array with `key`, `type`, and `format` items in that order, if present
|
144
151
|
* in the formatted argument block.
|
145
152
|
*/
|
146
|
-
|
147
|
-
type ParseCasesResult = {
|
148
|
-
/**
|
149
|
-
* A list of prepended arguments.
|
150
|
-
*/
|
151
|
-
args: string[];
|
152
|
-
/**
|
153
|
-
* A map of all cases.
|
154
|
-
*/
|
155
|
-
cases: Record<string, string>;
|
156
|
-
};
|
157
|
-
|
158
|
-
export { MessageFormatter, findClosingBracket, parseCases, pluralTypeHandler, selectTypeHandler, splitFormattedArgument };
|
153
|
+
export function splitFormattedArgument(block: string): string[];
|