@ultraq/icu-message-formatter 0.12.0 → 0.13.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 CHANGED
@@ -2,6 +2,12 @@
2
2
  Changelog
3
3
  =========
4
4
 
5
+ ### 0.13.0
6
+ - Minimum supported version of Node is now 18
7
+ - Browser target in `.browserslistrc` file is now `defaults`, so the explicit
8
+ `ie11` target has been removed and thus IE11 support has been dropped
9
+ - Type definitions are now included in this project
10
+
5
11
  ### 0.12.0
6
12
  - Dropped support for Node 10
7
13
  - Use [`Intl.Pluralrules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules)
package/README.md CHANGED
@@ -98,8 +98,9 @@ register.
98
98
  - the object which matched the key of the block being processed
99
99
  - any format options associated with the block being processed
100
100
  - the locale to use for formatting
101
- - the object of placeholder data given to the original `format` call
102
- - and the `format` function itself (see below) so that sub-messages can be
101
+ - the object of placeholder data given to the original `format`/`process`
102
+ call
103
+ - and the `process` function itself (see below) so that sub-messages can be
103
104
  processed by type handlers
104
105
 
105
106
  #### format(message, values = {})
@@ -1,2 +1,2 @@
1
- function r(r,n){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(r);n&&(e=e.filter((function(n){return Object.getOwnPropertyDescriptor(r,n).enumerable}))),t.push.apply(t,e)}return t}function n(n){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?r(Object(e),!0).forEach((function(r){o(n,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(e)):r(Object(e)).forEach((function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(e,r))}))}return n}function t(r,n){if(!(r instanceof n))throw new TypeError("Cannot call a class as a function")}function e(r,n){for(var t=0;t<n.length;t++){var e=n[t];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(r,e.key,e)}}function o(r,n,t){return n in r?Object.defineProperty(r,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[n]=t,r}function i(r,n){return function(r){if(Array.isArray(r))return r}(r)||function(r,n){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null==t)return;var e,o,i=[],a=!0,u=!1;try{for(t=t.call(r);!(a=(e=t.next()).done)&&(i.push(e.value),!n||i.length!==n);a=!0);}catch(r){u=!0,o=r}finally{try{a||null==t.return||t.return()}finally{if(u)throw o}}return i}(r,n)||function(r,n){if(!r)return;if("string"==typeof r)return a(r,n);var t=Object.prototype.toString.call(r).slice(8,-1);"Object"===t&&r.constructor&&(t=r.constructor.name);if("Map"===t||"Set"===t)return Array.from(r);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return a(r,n)}(r,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(r,n){(null==n||n>r.length)&&(n=r.length);for(var t=0,e=new Array(n);t<n;t++)e[t]=r[t];return e}function u(r){for(var n=function(r){return/\s/.test(r)},t=[],e={},o=0,i=null,a=!1,u=0;u<r.length;){if(a&&(n(r[u])||"{"===r[u]))a=!1,i=r.slice(o,u),"{"===r[u]&&u--;else if(!a&&!n(r[u])){var c="{"===r[u];if(i&&c){var s=l(r,u);if(-1===s)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));e[i]=r.slice(u+1,s),u=s,i=null}else i&&(t.push(i),i=null),a=!0,o=u}u++}return a&&(i=r.slice(o)),i&&t.push(i),{args:t,cases:e}}function l(r,n){for(var t=0,e=n+1;e<r.length;e++){var o=r.charAt(e);if("}"===o){if(0===t)return e;t--}else"{"===o&&t++}return-1}function c(r){return s(r.slice(1,-1),",",3)}function s(r,n,t){var e=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];if(!r)return e;if(1===t)return e.push(r),e;var o=r.indexOf(n);if(-1===o)return e.push(r),e;var i=r.substring(0,o).trim(),a=r.substring(o+n.length+1).trim();return e.push(i),s(a,n,t-1,e)}function f(r){return r.reduce((function(r,n){return r.concat(Array.isArray(n)?f(n):n)}),[])}function h(r){var n={};return function(){for(var t=arguments.length,e=new Array(t),o=0;o<t;o++)e[o]=arguments[o];var i=e.length?e.map((function(r){return null===r?"null":void 0===r?"undefined":"function"==typeof r?r.toString():r instanceof Date?r.toISOString():JSON.stringify(r)})).join("|"):"_(no-args)_";if(Object.prototype.hasOwnProperty.call(n,i))return n[i];var a=r.apply(void 0,e);return n[i]=a,a}}var v,p=function(){function r(n){var e=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(this,r),o(this,"format",h((function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return f(e.process(r,n)).join("")}))),this.locale=n,this.typeHandlers=i}var n,a,u;return n=r,a=[{key:"process",value:function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r)return[];var t=r.indexOf("{");if(-1!==t){var e=l(r,t);if(-1===e)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));var o=r.substring(t,e+1);if(o){var a=[],u=r.substring(0,t);u&&a.push(u);var s=c(o),f=i(s,3),h=f[0],v=f[1],p=f[2],g=n[h];null==g&&(g="");var y=v&&this.typeHandlers[v];a.push(y?y(g,p,this.locale,n,this.process.bind(this)):g);var b=r.substring(e+1);return b&&a.push(this.process(b,n)),a}}return[r]}}],a&&e(n.prototype,a),u&&e(n,u),r}(),g=0;function y(r,n){for(var t=0,e="",o=0,i={};t<r.length;){if("#"!==r[t]||o)e+=r[t];else{var a="__hashToken".concat(g++);e+="{".concat(a,", number}"),i[a]=n}"{"===r[t]?o++:"}"===r[t]&&o--,t++}return{caseBody:e,numberValues:i}}function b(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",e=arguments.length>2?arguments[2]:void 0,o=arguments.length>3?arguments[3]:void 0,i=arguments.length>4?arguments[4]:void 0,a=u(t),l=a.args,c=a.cases,s=parseInt(r);l.forEach((function(r){r.startsWith("offset:")&&(s-=parseInt(r.slice("offset:".length)))}));var f=[];if("PluralRules"in Intl){void 0!==v&&v.resolvedOptions().locale===e||(v=new Intl.PluralRules(e));var h=v.select(s);"other"!==h&&f.push(h)}1===s&&f.push("one"),f.push("=".concat(s),"other");for(var p=0;p<f.length;p++){var g=f[p];if(g in c){var b=y(c[g],s),d=b.caseBody,O=b.numberValues;return i(d,n(n({},o),O))}}return r}function d(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",t=arguments.length>3?arguments[3]:void 0,e=arguments.length>4?arguments[4]:void 0,o=u(n),i=o.cases;return r in i?e(i[r],t):"other"in i?e(i.other,t):r}export{p as MessageFormatter,l as findClosingBracket,u as parseCases,b as pluralTypeHandler,d as selectTypeHandler,c as splitFormattedArgument};
1
+ function t(t,e,n){return(e=function(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function e(t){const e=t=>/\s/.test(t),r=[],i={};let l=0,s=null,o=!1,u=0;for(;u<t.length;){if(o&&(e(t[u])||"{"===t[u]))o=!1,s=t.slice(l,u),"{"===t[u]&&u--;else if(!o&&!e(t[u])){const e="{"===t[u];if(s&&e){const e=n(t,u);if(-1===e)throw new Error(`Unbalanced curly braces in string: "${t}"`);i[s]=t.slice(u+1,e),u=e,s=null}else s&&(r.push(s),s=null),o=!0,l=u}u++}return o&&(s=t.slice(l)),s&&r.push(s),{args:r,cases:i}}function n(t,e){let n=0;for(let r=e+1;r<t.length;r++){let e=t.charAt(r);if("}"===e){if(0===n)return r;n--}else"{"===e&&n++}return-1}function r(t){return i(t.slice(1,-1),",",3)}function i(t,e,n){let r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];if(!t)return r;if(1===n)return r.push(t),r;let l=t.indexOf(e);if(-1===l)return r.push(t),r;let s=t.substring(0,l).trim(),o=t.substring(l+e.length+1).trim();return r.push(s),i(o,e,n-1,r)}function l(t){return t.reduce(((t,e)=>t.concat(Array.isArray(e)?l(e):e)),[])}class s{constructor(e){var n=this;let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};var i,s;t(this,"format",(i=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return l(n.process(t,e)).join("")},s={},function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var r=e.length?e.map((function(t){return null===t?"null":void 0===t?"undefined":"function"==typeof t?t.toString():t instanceof Date?t.toISOString():JSON.stringify(t)})).join("|"):"_(no-args)_";if(Object.prototype.hasOwnProperty.call(s,r))return s[r];var l=i.apply(void 0,e);return s[r]=l,l})),this.locale=e,this.typeHandlers=r}process(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!t)return[];let i=t.indexOf("{");if(-1!==i){let l=n(t,i);if(-1===l)throw new Error(`Unbalanced curly braces in string: "${t}"`);{let n=t.substring(i,l+1);if(n){let s=[],o=t.substring(0,i);o&&s.push(o);let[u,c,a]=r(n),f=e[u];null==f&&(f="");let h=c&&this.typeHandlers[c];s.push(h?h(f,a,this.locale,e,this.process.bind(this)):f);let g=t.substring(l+1);return g&&s.push(this.process(g,e)),s}}}return[t]}}let o,u=0;const c="other";function a(t,e){let n=0,r="",i=0;const l={};for(;n<t.length;){if("#"!==t[n]||i)r+=t[n];else{let t="__hashToken"+u++;r+=`{${t}, number}`,l[t]=e}"{"===t[n]?i++:"}"===t[n]&&i--,n++}return{caseBody:r,numberValues:l}}function f(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>2?arguments[2]:void 0,i=arguments.length>3?arguments[3]:void 0,l=arguments.length>4?arguments[4]:void 0;const{args:s,cases:u}=e(n);let f=parseInt(t);s.forEach((t=>{t.startsWith("offset:")&&(f-=parseInt(t.slice(7)))}));const h=[];if("PluralRules"in Intl){void 0!==o&&o.resolvedOptions().locale===r||(o=new Intl.PluralRules(r));const t=o.select(f);t!==c&&h.push(t)}1===f&&h.push("one"),h.push(`=${f}`,c);for(let t=0;t<h.length;t++){const e=h[t];if(e in u){const{caseBody:t,numberValues:n}=a(u[e],f);return l(t,{...i,...n})}}return t}const h="other";function g(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>3?arguments[3]:void 0,i=arguments.length>4?arguments[4]:void 0;const{cases:l}=e(n);return t in l?i(l[t],r):h in l?i(l[h],r):t}export{s as MessageFormatter,n as findClosingBracket,e as parseCases,f as pluralTypeHandler,g as selectTypeHandler,r as splitFormattedArgument};
2
2
  //# sourceMappingURL=icu-message-formatter.es.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"icu-message-formatter.es.min.js","sources":["../source/utilities.js","../node_modules/@ultraq/array-utils/array-utils.es.js","../node_modules/@ultraq/function-utils/function-utils.es.js","../source/pluralTypeHandler.js","../source/MessageFormatter.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 * Most branch-based type handlers are based around \"cases\".\n * For example, `select` and `plural` compare compare a value\n * to \"case keys\" to choose a subtranslation.\n * \n * This util splits \"matches\" portions provided to the aforementioned\n * handlers into case strings, and extracts any prepended arguments\n * (for example, `plural` supports an `offset:n` argument used for\n * populating the magic `#` variable).\n * \n * @param {String} string\n * @return {Object} The `cases` key points to a map of all cases.\n * The `arguments` key points to a list of prepended arguments.\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} The index of the matching closing bracket, or -1 if no\n * closing bracket 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 {Array}\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 {Array} [accumulator=[]]\n * @return {Array}\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 2017, 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 * Flattens an array of arrays of infinite depth into a single-dimension array.\n * \n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n * \n * @param {Array} array\n * @return {Array} Flattened array.\n */\nexport function flatten(array) {\n return array.reduce(function (accumulator, value) {\n return accumulator.concat(Array.isArray(value) ? flatten(value) : value);\n }, []);\n}\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n * \n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {Array} An array encompassing the given range.\n */\n\nexport function range(start, end) {\n var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;\n return Array.apply(0, Array(Math.ceil((end - start) / step))).map(function (empty, index) {\n return index * step + start;\n });\n}\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n * \n * @param {Array} array\n * @param {Function} predicate\n * Invoked with the array item.\n * @return {Object} The matching item, or `null` if no match was found.\n */\n\nexport function remove(array, predicate) {\n return array.find(function (item, index) {\n if (predicate(item)) {\n array.splice(index, 1);\n return item;\n }\n });\n}\n\n//# sourceMappingURL=array-utils.es.js.map","/**\n * A higher-order function to apply [memoization](https://en.wikipedia.org/wiki/Memoization).\n * \n * If memoizing a recursive function, then memoize and define the function at\n * the same time so you can make a call to the memoized function, eg:\n * \n * ```javascript\n * const myFunction = memoize(() => myFunction());\n * ```\n * \n * @param {Function} func\n * @return {Function} \n */\nexport function memoize(func) {\n var cache = {};\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n var key = args.length ? args.map(function (arg) {\n return arg === null ? 'null' : arg === undefined ? 'undefined' : typeof arg === 'function' ? arg.toString() : arg instanceof Date ? arg.toISOString() : JSON.stringify(arg);\n }).join('|') : '_(no-args)_';\n\n if (Object.prototype.hasOwnProperty.call(cache, key)) {\n return cache[key];\n }\n\n var result = func.apply(void 0, args);\n cache[key] = result;\n return result;\n };\n}\n\n//# sourceMappingURL=function-utils.es.js.map","/* \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 {Object} {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 {String} values\n * @param {Function} format\n * @return {String}\n */\nexport default function pluralTypeHandler(value, matches = '', locale, values, format) {\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 format(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 {findClosingBracket, splitFormattedArgument} from './utilities.js';\n\nimport {flatten} from '@ultraq/array-utils';\nimport {memoize} from '@ultraq/function-utils';\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 {Object} [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 * @param {String} message\n\t * @param {Object} [values={}]\n\t * @return {String}\n\t */\n\tformat = memoize((message, values = {}) => {\n\n\t\treturn flatten(this.process(message, values)).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 {Object} [values={}]\n\t * @return {Array}\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\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 {String} values\n * @param {Function} format\n * @return {String}\n */\nexport default function selectTypeHandler(value, matches = '', locale, values, format) {\n\tconst { cases } = parseCases(matches);\n\n\tif (value in cases) {\n\t\treturn format(cases[value], values);\n\t}\n\telse if (OTHER in cases) {\n\t\treturn format(cases[OTHER], values);\n\t}\n\n\treturn value;\n}\n"],"names":["parseCases","string","isWhitespace","ch","test","args","cases","currTermStart","latestTerm","inTerm","i","length","slice","caseBody","branchEndIndex","findClosingBracket","Error","push","fromIndex","depth","char","charAt","splitFormattedArgument","block","split","separator","limit","accumulator","indexOfDelimiter","indexOf","head","substring","trim","tail","flatten","array","reduce","value","concat","Array","isArray","memoize","func","cache","_len","arguments","_key","key","map","arg","undefined","toString","Date","toISOString","JSON","stringify","join","Object","prototype","hasOwnProperty","call","result","apply","pluralFormatter","MessageFormatter","locale","typeHandlers","message","values","_this","process","blockStartIndex","blockEndIndex","type","format","body","typeHandler","this","bind","keyCounter","replaceNumberSign","output","numBraces","numberValues","keyParam","pluralTypeHandler","matches","intValue","parseInt","forEach","startsWith","keywordPossibilities","Intl","resolvedOptions","PluralRules","pluralKeyword","select","keyword","selectTypeHandler"],"mappings":"24DA8BO,SAASA,EAAWC,WACpBC,EAAe,SAAAC,SAAM,KAAKC,KAAKD,IAE/BE,EAAO,GACPC,EAAQ,GAEVC,EAAgB,EAChBC,EAAa,KACbC,GAAS,EAETC,EAAI,EACDA,EAAIT,EAAOU,QAAQ,IAErBF,IAAWP,EAAaD,EAAOS,KAAqB,MAAdT,EAAOS,IAChDD,GAAS,EACTD,EAAaP,EAAOW,MAAML,EAAeG,GAGvB,MAAdT,EAAOS,IACVA,SAKG,IAAKD,IAAWP,EAAaD,EAAOS,IAAK,KACvCG,EAAyB,MAAdZ,EAAOS,MAIpBF,GAAcK,EAAU,KACrBC,EAAiBC,EAAmBd,EAAQS,OAE1B,IAApBI,QACG,IAAIE,oDAA6Cf,QAGxDK,EAAME,GAAcP,EAAOW,MAAMF,EAAI,EAAGI,GAExCJ,EAAII,EACJN,EAAa,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,EAASH,GACZwE,IAEwB,MAAhBrE,EAASH,IACjBwE,IAGDxE,UAGM,CACNG,SAAUoE,EACVE,aAAAA,GAkBa,SAASE,EAAkBhD,OAAOiD,yDAAU,GAAIrB,yCAAQG,yCAAQM,2CACtD1E,EAAWsF,GAA3BjF,IAAAA,KAAMC,IAAAA,MAEViF,EAAWC,SAASnD,GAExBhC,EAAKoF,SAAQ,SAACxC,GACTA,EAAIyC,WAAW,aAClBH,GAAYC,SAASvC,EAAIrC,MAAM,UAAUD,iBAIrCgF,EAAuB,MAEzB,gBAAiBC,KAAM,MAEF1C,IAApBa,GAAiCA,EAAgB8B,kBAAkB5B,SAAWA,IACjFF,EAAkB,IAAI6B,KAAKE,YAAY7B,QAGlC8B,EAAgBhC,EAAgBiC,OAAOT,GAzEjC,UA4ERQ,GACHJ,EAAqB1E,KAAK8E,GAGX,IAAbR,GACHI,EAAqB1E,KAlFT,OAoFb0E,EAAqB1E,gBAASsE,GAnFjB,aAqFR,IAAI7E,EAAI,EAAGA,EAAIiF,EAAqBhF,OAAQD,IAAK,KAC/CuF,EAAUN,EAAqBjF,MACjCuF,KAAW3F,EAAO,OACc0E,EAAkB1E,EAAM2F,GAAUV,GAA7D1E,IAAAA,SAAUsE,IAAAA,oBACXT,EAAO7D,SACVuD,GACAe,YAKC9C,EEtFO,SAAS6D,EAAkB7D,OAAOiD,yDAAU,GAAYlB,yCAAQM,2CAC5D1E,EAAWsF,GAArBhF,IAAAA,aAEJ+B,KAAS/B,EACLoE,EAAOpE,EAAM+B,GAAQ+B,GApBhB,UAsBK9D,EACVoE,EAAOpE,EAAK,MAAS8D,GAGtB/B"}
1
+ {"version":3,"file":"icu-message-formatter.es.min.js","sources":["../source/utilities.js","../node_modules/@ultraq/array-utils/array-utils.es.js","../source/MessageFormatter.js","../node_modules/@ultraq/function-utils/function-utils.es.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 2017, 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 * Flattens an array of arrays of infinite depth into a single-dimension array.\n *\n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n *\n * @param {Array<any>} array\n * @return {Array<any>} Flattened array.\n */\nexport function flatten(array) {\n return array.reduce((acc, value) => {\n return acc.concat(Array.isArray(value) ? flatten(value) : value);\n }, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n *\n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {number[]} An array encompassing the given range.\n */\nexport function range(start, end) {\n let step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;\n return Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * A function to execute on each item in an array, returning truthy\n * if the item passes whatever test is required for the use of this\n * predicate.\n *\n * @template T\n * @callback Predicate<T>\n * @param {T} item\n * @return {boolean}\n */\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n *\n * @template T\n * @param {T[]} array\n * @param {Predicate<T>} predicate\n * Function to test each item of the array with. If it returns a truthy value\n * for the item, then that item is removed and returned.\n * @return {T | undefined} The matching item, or `undefined` if no match was found.\n */\nexport function remove(array, predicate) {\n return array.find((item, index) => {\n if (predicate(item)) {\n array.splice(index, 1);\n return item;\n }\n return false;\n });\n}\n\n//# sourceMappingURL=array-utils.es.js.map","/* \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 {flatten} from '@ultraq/array-utils';\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 flatten(this.process(message, values)).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 * A higher-order function to apply [memoization](https://en.wikipedia.org/wiki/Memoization).\n * \n * If memoizing a recursive function, then memoize and define the function at\n * the same time so you can make a call to the memoized function, eg:\n * \n * ```javascript\n * const myFunction = memoize(() => myFunction());\n * ```\n * \n * @param {Function} func\n * @return {Function} \n */\nexport function memoize(func) {\n var cache = {};\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n var key = args.length ? args.map(function (arg) {\n return arg === null ? 'null' : arg === undefined ? 'undefined' : typeof arg === 'function' ? arg.toString() : arg instanceof Date ? arg.toISOString() : JSON.stringify(arg);\n }).join('|') : '_(no-args)_';\n if (Object.prototype.hasOwnProperty.call(cache, key)) {\n return cache[key];\n }\n var result = func.apply(void 0, args);\n cache[key] = result;\n return result;\n };\n}\n\n//# sourceMappingURL=function-utils.es.js.map","/* \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 {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} 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 {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} 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":["parseCases","string","isWhitespace","ch","test","args","cases","currTermStart","latestTerm","inTerm","i","length","slice","caseBody","branchEndIndex","findClosingBracket","Error","push","fromIndex","depth","char","charAt","splitFormattedArgument","block","split","separator","limit","accumulator","arguments","undefined","indexOfDelimiter","indexOf","head","substring","trim","tail","flatten","array","reduce","acc","value","concat","Array","isArray","MessageFormatter","constructor","locale","_this","this","typeHandlers","func","cache","_defineProperty","memoize","message","values","process","join","_len","_key","key","map","arg","toString","Date","toISOString","JSON","stringify","Object","prototype","hasOwnProperty","call","result","apply","blockStartIndex","blockEndIndex","type","format","body","typeHandler","bind","pluralFormatter","keyCounter","OTHER","replaceNumberSign","output","numBraces","numberValues","keyParam","pluralTypeHandler","matches","intValue","parseInt","forEach","startsWith","keywordPossibilities","Intl","resolvedOptions","PluralRules","pluralKeyword","select","keyword","selectTypeHandler"],"mappings":"wcAqCO,SAASA,EAAWC,GAC1B,MAAMC,EAAeC,GAAM,KAAKC,KAAKD,GAE/BE,EAAO,GACPC,EAAQ,CAAA,EAEd,IAAIC,EAAgB,EAChBC,EAAa,KACbC,GAAS,EAETC,EAAI,EACR,KAAOA,EAAIT,EAAOU,QAAQ,CAEzB,GAAIF,IAAWP,EAAaD,EAAOS,KAAqB,MAAdT,EAAOS,IAChDD,GAAS,EACTD,EAAaP,EAAOW,MAAML,EAAeG,GAGvB,MAAdT,EAAOS,IACVA,SAKG,IAAKD,IAAWP,EAAaD,EAAOS,IAAK,CAC7C,MAAMG,EAAyB,MAAdZ,EAAOS,GAIxB,GAAIF,GAAcK,EAAU,CAC3B,MAAMC,EAAiBC,EAAmBd,EAAQS,GAElD,IAAwB,IAApBI,EACH,MAAM,IAAIE,MAAO,uCAAsCf,MAGxDK,EAAME,GAAcP,EAAOW,MAAMF,EAAI,EAAGI,GAExCJ,EAAII,EACJN,EAAa,IACd,MAEKA,IACHH,EAAKY,KAAKT,GACVA,EAAa,MAGdC,GAAS,EACTF,EAAgBG,CAElB,CACAA,GACD,CAUA,OARID,IACHD,EAAaP,EAAOW,MAAML,IAGvB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7B,GAAI,gBAAiBC,KAAM,MAEFnE,IAApBoD,GAAiCA,EAAgBgB,kBAAkBnD,SAAWA,IACjFmC,EAAkB,IAAIe,KAAKE,YAAYpD,IAGxC,MAAMqD,EAAgBlB,EAAgBmB,OAAOT,GAGzCQ,IAAkBhB,GACrBY,EAAqB9E,KAAKkF,EAE5B,CACiB,IAAbR,GACHI,EAAqB9E,KAlFT,OAoFb8E,EAAqB9E,KAAM,IAAG0E,IAAYR,GAE1C,IAAK,IAAIzE,EAAI,EAAGA,EAAIqF,EAAqBpF,OAAQD,IAAK,CACrD,MAAM2F,EAAUN,EAAqBrF,GACrC,GAAI2F,KAAW/F,EAAO,CACrB,MAAMO,SAACA,EAAQ0E,aAAEA,GAAgBH,EAAkB9E,EAAM+F,GAAUV,GACnE,OAAOnC,EAAQ3C,EAAU,IACrB0C,KACAgC,GAEL,CACD,CAEA,OAAO/C,CACR,CCvGA,MAAM2C,EAAQ,QAgBC,SAASmB,EAAkB9D,GAA8C,IAAvCkD,EAAO9D,UAAAjB,OAAA,QAAAkB,IAAAD,UAAA,GAAAA,UAAA,GAAG,GAAY2B,EAAM3B,UAAAjB,OAAAiB,EAAAA,kBAAAC,EAAE2B,EAAO5B,UAAAjB,OAAAiB,EAAAA,kBAAAC,EACrF,MAAMvB,MAACA,GAASN,EAAW0F,GAE3B,OAAIlD,KAASlC,EACLkD,EAAQlD,EAAMkC,GAAQe,GAErB4B,KAAS7E,EACVkD,EAAQlD,EAAM6E,GAAQ5B,GAGvBf,CACR","x_google_ignoreList":[1,3]}
@@ -1,2 +1,2 @@
1
- var IcuMessageFormatter=function(r){"use strict";function e(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function t(r){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?e(Object(n),!0).forEach((function(e){i(r,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(n)):e(Object(n)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(n,e))}))}return r}function n(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}function o(r,e){for(var t=0;t<e.length;t++){var n=e[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,n.key,n)}}function i(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function a(r,e){return function(r){if(Array.isArray(r))return r}(r)||function(r,e){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null==t)return;var n,o,i=[],a=!0,u=!1;try{for(t=t.call(r);!(a=(n=t.next()).done)&&(i.push(n.value),!e||i.length!==e);a=!0);}catch(r){u=!0,o=r}finally{try{a||null==t.return||t.return()}finally{if(u)throw o}}return i}(r,e)||function(r,e){if(!r)return;if("string"==typeof r)return u(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);"Object"===t&&r.constructor&&(t=r.constructor.name);if("Map"===t||"Set"===t)return Array.from(r);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return u(r,e)}(r,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function l(r){for(var e=function(r){return/\s/.test(r)},t=[],n={},o=0,i=null,a=!1,u=0;u<r.length;){if(a&&(e(r[u])||"{"===r[u]))a=!1,i=r.slice(o,u),"{"===r[u]&&u--;else if(!a&&!e(r[u])){var l="{"===r[u];if(i&&l){var c=s(r,u);if(-1===c)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));n[i]=r.slice(u+1,c),u=c,i=null}else i&&(t.push(i),i=null),a=!0,o=u}u++}return a&&(i=r.slice(o)),i&&t.push(i),{args:t,cases:n}}function s(r,e){for(var t=0,n=e+1;n<r.length;n++){var o=r.charAt(n);if("}"===o){if(0===t)return n;t--}else"{"===o&&t++}return-1}function c(r){return f(r.slice(1,-1),",",3)}function f(r,e,t){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];if(!r)return n;if(1===t)return n.push(r),n;var o=r.indexOf(e);if(-1===o)return n.push(r),n;var i=r.substring(0,o).trim(),a=r.substring(o+e.length+1).trim();return n.push(i),f(a,e,t-1,n)}function v(r){return r.reduce((function(r,e){return r.concat(Array.isArray(e)?v(e):e)}),[])}function h(r){var e={};return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];var i=n.length?n.map((function(r){return null===r?"null":void 0===r?"undefined":"function"==typeof r?r.toString():r instanceof Date?r.toISOString():JSON.stringify(r)})).join("|"):"_(no-args)_";if(Object.prototype.hasOwnProperty.call(e,i))return e[i];var a=r.apply(void 0,n);return e[i]=a,a}}var p,g=function(){function r(e){var t=this,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};n(this,r),i(this,"format",h((function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return v(t.process(r,e)).join("")}))),this.locale=e,this.typeHandlers=o}var e,t,u;return e=r,t=[{key:"process",value:function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r)return[];var t=r.indexOf("{");if(-1!==t){var n=s(r,t);if(-1===n)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));var o=r.substring(t,n+1);if(o){var i=[],u=r.substring(0,t);u&&i.push(u);var l=c(o),f=a(l,3),v=f[0],h=f[1],p=f[2],g=e[v];null==g&&(g="");var y=h&&this.typeHandlers[h];i.push(y?y(g,p,this.locale,e,this.process.bind(this)):g);var b=r.substring(n+1);return b&&i.push(this.process(b,e)),i}}return[r]}}],t&&o(e.prototype,t),u&&o(e,u),r}(),y=0,b="other";function d(r,e){for(var t=0,n="",o=0,i={};t<r.length;){if("#"!==r[t]||o)n+=r[t];else{var a="__hashToken".concat(y++);n+="{".concat(a,", number}"),i[a]=e}"{"===r[t]?o++:"}"===r[t]&&o--,t++}return{caseBody:n,numberValues:i}}var O="other";return r.MessageFormatter=g,r.findClosingBracket=s,r.parseCases=l,r.pluralTypeHandler=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",n=arguments.length>2?arguments[2]:void 0,o=arguments.length>3?arguments[3]:void 0,i=arguments.length>4?arguments[4]:void 0,a=l(e),u=a.args,s=a.cases,c=parseInt(r);u.forEach((function(r){r.startsWith("offset:")&&(c-=parseInt(r.slice("offset:".length)))}));var f=[];if("PluralRules"in Intl){void 0!==p&&p.resolvedOptions().locale===n||(p=new Intl.PluralRules(n));var v=p.select(c);v!==b&&f.push(v)}1===c&&f.push("one"),f.push("=".concat(c),b);for(var h=0;h<f.length;h++){var g=f[h];if(g in s){var y=d(s[g],c),O=y.caseBody,m=y.numberValues;return i(O,t(t({},o),m))}}return r},r.selectTypeHandler=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",t=arguments.length>3?arguments[3]:void 0,n=arguments.length>4?arguments[4]:void 0,o=l(e),i=o.cases;return r in i?n(i[r],t):O in i?n(i.other,t):r},r.splitFormattedArgument=c,Object.defineProperty(r,"__esModule",{value:!0}),r}({});
1
+ var IcuMessageFormatter=function(t){"use strict";function e(t,e,n){return(e=function(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function n(t){const e=t=>/\s/.test(t),n=[],i={};let s=0,l=null,o=!1,u=0;for(;u<t.length;){if(o&&(e(t[u])||"{"===t[u]))o=!1,l=t.slice(s,u),"{"===t[u]&&u--;else if(!o&&!e(t[u])){const e="{"===t[u];if(l&&e){const e=r(t,u);if(-1===e)throw new Error(`Unbalanced curly braces in string: "${t}"`);i[l]=t.slice(u+1,e),u=e,l=null}else l&&(n.push(l),l=null),o=!0,s=u}u++}return o&&(l=t.slice(s)),l&&n.push(l),{args:n,cases:i}}function r(t,e){let n=0;for(let r=e+1;r<t.length;r++){let e=t.charAt(r);if("}"===e){if(0===n)return r;n--}else"{"===e&&n++}return-1}function i(t){return s(t.slice(1,-1),",",3)}function s(t,e,n){let r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];if(!t)return r;if(1===n)return r.push(t),r;let i=t.indexOf(e);if(-1===i)return r.push(t),r;let l=t.substring(0,i).trim(),o=t.substring(i+e.length+1).trim();return r.push(l),s(o,e,n-1,r)}function l(t){return t.reduce(((t,e)=>t.concat(Array.isArray(e)?l(e):e)),[])}let o,u=0;const c="other";function a(t,e){let n=0,r="",i=0;const s={};for(;n<t.length;){if("#"!==t[n]||i)r+=t[n];else{let t="__hashToken"+u++;r+=`{${t}, number}`,s[t]=e}"{"===t[n]?i++:"}"===t[n]&&i--,n++}return{caseBody:r,numberValues:s}}const f="other";return t.MessageFormatter=class{constructor(t){var n=this;let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};var i,s;e(this,"format",(i=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return l(n.process(t,e)).join("")},s={},function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var r=e.length?e.map((function(t){return null===t?"null":void 0===t?"undefined":"function"==typeof t?t.toString():t instanceof Date?t.toISOString():JSON.stringify(t)})).join("|"):"_(no-args)_";if(Object.prototype.hasOwnProperty.call(s,r))return s[r];var l=i.apply(void 0,e);return s[r]=l,l})),this.locale=t,this.typeHandlers=r}process(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!t)return[];let n=t.indexOf("{");if(-1!==n){let s=r(t,n);if(-1===s)throw new Error(`Unbalanced curly braces in string: "${t}"`);{let r=t.substring(n,s+1);if(r){let l=[],o=t.substring(0,n);o&&l.push(o);let[u,c,a]=i(r),f=e[u];null==f&&(f="");let h=c&&this.typeHandlers[c];l.push(h?h(f,a,this.locale,e,this.process.bind(this)):f);let g=t.substring(s+1);return g&&l.push(this.process(g,e)),l}}}return[t]}},t.findClosingBracket=r,t.parseCases=n,t.pluralTypeHandler=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>2?arguments[2]:void 0,i=arguments.length>3?arguments[3]:void 0,s=arguments.length>4?arguments[4]:void 0;const{args:l,cases:u}=n(e);let f=parseInt(t);l.forEach((t=>{t.startsWith("offset:")&&(f-=parseInt(t.slice(7)))}));const h=[];if("PluralRules"in Intl){void 0!==o&&o.resolvedOptions().locale===r||(o=new Intl.PluralRules(r));const t=o.select(f);t!==c&&h.push(t)}1===f&&h.push("one"),h.push(`=${f}`,c);for(let t=0;t<h.length;t++){const e=h[t];if(e in u){const{caseBody:t,numberValues:n}=a(u[e],f);return s(t,{...i,...n})}}return t},t.selectTypeHandler=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>3?arguments[3]:void 0,i=arguments.length>4?arguments[4]:void 0;const{cases:s}=n(e);return t in s?i(s[t],r):f in s?i(s[f],r):t},t.splitFormattedArgument=i,t}({});
2
2
  //# sourceMappingURL=icu-message-formatter.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"icu-message-formatter.min.js","sources":["../source/utilities.js","../node_modules/@ultraq/array-utils/array-utils.es.js","../node_modules/@ultraq/function-utils/function-utils.es.js","../source/pluralTypeHandler.js","../source/MessageFormatter.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 * Most branch-based type handlers are based around \"cases\".\n * For example, `select` and `plural` compare compare a value\n * to \"case keys\" to choose a subtranslation.\n * \n * This util splits \"matches\" portions provided to the aforementioned\n * handlers into case strings, and extracts any prepended arguments\n * (for example, `plural` supports an `offset:n` argument used for\n * populating the magic `#` variable).\n * \n * @param {String} string\n * @return {Object} The `cases` key points to a map of all cases.\n * The `arguments` key points to a list of prepended arguments.\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} The index of the matching closing bracket, or -1 if no\n * closing bracket 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 {Array}\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 {Array} [accumulator=[]]\n * @return {Array}\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 2017, 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 * Flattens an array of arrays of infinite depth into a single-dimension array.\n * \n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n * \n * @param {Array} array\n * @return {Array} Flattened array.\n */\nexport function flatten(array) {\n return array.reduce(function (accumulator, value) {\n return accumulator.concat(Array.isArray(value) ? flatten(value) : value);\n }, []);\n}\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n * \n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {Array} An array encompassing the given range.\n */\n\nexport function range(start, end) {\n var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;\n return Array.apply(0, Array(Math.ceil((end - start) / step))).map(function (empty, index) {\n return index * step + start;\n });\n}\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n * \n * @param {Array} array\n * @param {Function} predicate\n * Invoked with the array item.\n * @return {Object} The matching item, or `null` if no match was found.\n */\n\nexport function remove(array, predicate) {\n return array.find(function (item, index) {\n if (predicate(item)) {\n array.splice(index, 1);\n return item;\n }\n });\n}\n\n//# sourceMappingURL=array-utils.es.js.map","/**\n * A higher-order function to apply [memoization](https://en.wikipedia.org/wiki/Memoization).\n * \n * If memoizing a recursive function, then memoize and define the function at\n * the same time so you can make a call to the memoized function, eg:\n * \n * ```javascript\n * const myFunction = memoize(() => myFunction());\n * ```\n * \n * @param {Function} func\n * @return {Function} \n */\nexport function memoize(func) {\n var cache = {};\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n var key = args.length ? args.map(function (arg) {\n return arg === null ? 'null' : arg === undefined ? 'undefined' : typeof arg === 'function' ? arg.toString() : arg instanceof Date ? arg.toISOString() : JSON.stringify(arg);\n }).join('|') : '_(no-args)_';\n\n if (Object.prototype.hasOwnProperty.call(cache, key)) {\n return cache[key];\n }\n\n var result = func.apply(void 0, args);\n cache[key] = result;\n return result;\n };\n}\n\n//# sourceMappingURL=function-utils.es.js.map","/* \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 {Object} {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 {String} values\n * @param {Function} format\n * @return {String}\n */\nexport default function pluralTypeHandler(value, matches = '', locale, values, format) {\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 format(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 {findClosingBracket, splitFormattedArgument} from './utilities.js';\n\nimport {flatten} from '@ultraq/array-utils';\nimport {memoize} from '@ultraq/function-utils';\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 {Object} [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 * @param {String} message\n\t * @param {Object} [values={}]\n\t * @return {String}\n\t */\n\tformat = memoize((message, values = {}) => {\n\n\t\treturn flatten(this.process(message, values)).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 {Object} [values={}]\n\t * @return {Array}\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\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 {String} values\n * @param {Function} format\n * @return {String}\n */\nexport default function selectTypeHandler(value, matches = '', locale, values, format) {\n\tconst { cases } = parseCases(matches);\n\n\tif (value in cases) {\n\t\treturn format(cases[value], values);\n\t}\n\telse if (OTHER in cases) {\n\t\treturn format(cases[OTHER], values);\n\t}\n\n\treturn value;\n}\n"],"names":["parseCases","string","isWhitespace","ch","test","args","cases","currTermStart","latestTerm","inTerm","i","length","slice","caseBody","branchEndIndex","findClosingBracket","Error","push","fromIndex","depth","char","charAt","splitFormattedArgument","block","split","separator","limit","accumulator","indexOfDelimiter","indexOf","head","substring","trim","tail","flatten","array","reduce","value","concat","Array","isArray","memoize","func","cache","_len","arguments","_key","key","map","arg","undefined","toString","Date","toISOString","JSON","stringify","join","Object","prototype","hasOwnProperty","call","result","apply","pluralFormatter","MessageFormatter","locale","typeHandlers","message","values","_this","process","blockStartIndex","blockEndIndex","type","format","body","typeHandler","this","bind","keyCounter","OTHER","replaceNumberSign","output","numBraces","numberValues","keyParam","matches","intValue","parseInt","forEach","startsWith","keywordPossibilities","Intl","resolvedOptions","PluralRules","pluralKeyword","select","keyword"],"mappings":"47DA8BO,SAASA,EAAWC,WACpBC,EAAe,SAAAC,SAAM,KAAKC,KAAKD,IAE/BE,EAAO,GACPC,EAAQ,GAEVC,EAAgB,EAChBC,EAAa,KACbC,GAAS,EAETC,EAAI,EACDA,EAAIT,EAAOU,QAAQ,IAErBF,IAAWP,EAAaD,EAAOS,KAAqB,MAAdT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ArBhF,IAAAA,aAEJ+B,KAAS/B,EACLoE,EAAOpE,EAAM+B,GAAQ+B,GAEpBY,KAAS1E,EACVoE,EAAOpE,EAAK,MAAS8D,GAGtB/B"}
1
+ {"version":3,"file":"icu-message-formatter.min.js","sources":["../source/utilities.js","../node_modules/@ultraq/array-utils/array-utils.es.js","../source/pluralTypeHandler.js","../source/selectTypeHandler.js","../source/MessageFormatter.js","../node_modules/@ultraq/function-utils/function-utils.es.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 2017, 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 * Flattens an array of arrays of infinite depth into a single-dimension array.\n *\n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n *\n * @param {Array<any>} array\n * @return {Array<any>} Flattened array.\n */\nexport function flatten(array) {\n return array.reduce((acc, value) => {\n return acc.concat(Array.isArray(value) ? flatten(value) : value);\n }, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n *\n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {number[]} An array encompassing the given range.\n */\nexport function range(start, end) {\n let step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;\n return Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * A function to execute on each item in an array, returning truthy\n * if the item passes whatever test is required for the use of this\n * predicate.\n *\n * @template T\n * @callback Predicate<T>\n * @param {T} item\n * @return {boolean}\n */\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n *\n * @template T\n * @param {T[]} array\n * @param {Predicate<T>} predicate\n * Function to test each item of the array with. If it returns a truthy value\n * for the item, then that item is removed and returned.\n * @return {T | undefined} The matching item, or `undefined` if no match was found.\n */\nexport function remove(array, predicate) {\n return array.find((item, index) => {\n if (predicate(item)) {\n array.splice(index, 1);\n return item;\n }\n return false;\n });\n}\n\n//# sourceMappingURL=array-utils.es.js.map","/* \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 {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} 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 {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} 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","/* \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 {flatten} from '@ultraq/array-utils';\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 flatten(this.process(message, values)).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 * A higher-order function to apply [memoization](https://en.wikipedia.org/wiki/Memoization).\n * \n * If memoizing a recursive function, then memoize and define the function at\n * the same time so you can make a call to the memoized function, eg:\n * \n * ```javascript\n * const myFunction = memoize(() => myFunction());\n * ```\n * \n * @param {Function} func\n * @return {Function} \n */\nexport function memoize(func) {\n var cache = {};\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n var key = args.length ? args.map(function (arg) {\n return arg === null ? 'null' : arg === undefined ? 'undefined' : typeof arg === 'function' ? arg.toString() : arg instanceof Date ? arg.toISOString() : JSON.stringify(arg);\n }).join('|') : '_(no-args)_';\n if (Object.prototype.hasOwnProperty.call(cache, key)) {\n return cache[key];\n }\n var result = func.apply(void 0, args);\n cache[key] = result;\n return result;\n };\n}\n\n//# sourceMappingURL=function-utils.es.js.map"],"names":["parseCases","string","isWhitespace","ch","test","args","cases","currTermStart","latestTerm","inTerm","i","length","slice","caseBody","branchEndIndex","findClosingBracket","Error","push","fromIndex","depth","char","charAt","splitFormattedArgument","block","split","separator","limit","accumulator","arguments","undefined","indexOfDelimiter","indexOf","head","substring","trim","tail","flatten","array","reduce","acc","value","concat","Array","isArray","pluralFormatter","keyCounter","OTHER","replaceNumberSign","output","numBraces","numberValues","keyParam","constructor","locale","_this","this","typeHandlers","func","cache","_defineProperty","memoize","message","values","process","join","_len","_key","key","map","arg","toString","Date","toISOString","JSON","stringify","Object","prototype","hasOwnProperty","call","result","apply","blockStartIndex","blockEndIndex","type","format","body","typeHandler","bind","matches","intValue","parseInt","forEach","startsWith","keywordPossibilities","Intl","resolvedOptions","PluralRules","pluralKeyword","select","keyword"],"mappings":"yfAqCO,SAASA,EAAWC,GAC1B,MAAMC,EAAeC,GAAM,KAAKC,KAAKD,GAE/BE,EAAO,GACPC,EAAQ,CAAA,EAEd,IAAIC,EAAgB,EAChBC,EAAa,KACbC,GAAS,EAETC,EAAI,EACR,KAAOA,EAAIT,EAAOU,QAAQ,CAEzB,GAAIF,IAAWP,EAAaD,EAAOS,KAAqB,MAAdT,EAAOS,IAChDD,GAAS,EACTD,EAAaP,EAAOW,MAAML,EAAeG,GAGvB,MAAdT,EAAOS,IACVA,SAKG,IAAKD,IAAWP,EAAaD,EAAOS,IAAK,CAC7C,MAAMG,EAAyB,MAAdZ,EAAOS,GAIxB,GAAIF,GAAcK,E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,5]}
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './types/IcuMessageFormatter';
2
+ export * from './types/MessageFormatter';
3
+ export * from './types/pluralTypeHandler';
4
+ export * from './types/selectTypeHandler';
5
+ export * from './types/utilities';