@ultraq/string-utils
Advanced tools
| /** | ||
| * Escapes special HTML characters in a string with their entity code | ||
| * equivalents. | ||
| * | ||
| * @param {string} string | ||
| * @return {string} | ||
| * HTML escaped string, safe for use in HTML. | ||
| */ | ||
| export function escapeHtml(string: string): string; | ||
| /** | ||
| * Returns the replacement of each placeholder in a template string with a | ||
| * corresponding replacement value. | ||
| * | ||
| * @param {string} template | ||
| * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`, | ||
| * `{greeting}`, ...) placeholders, but not both. | ||
| * @param {...string|Record<string,string>} values | ||
| * Either an argument list / array of values to replace values in an indexed | ||
| * template string, or an object where the keys are the names in a named | ||
| * template string to replace with their values. | ||
| * @return {string} | ||
| * Replaced template string. | ||
| */ | ||
| export function format(template: string, ...values: (string | Record<string, string>)[]): string; |
+12
-10
| { | ||
| "name": "@ultraq/string-utils", | ||
| "version": "2.1.0", | ||
| "version": "3.0.0", | ||
| "description": "A collection of utilities for JavaScript strings", | ||
@@ -20,2 +20,3 @@ "author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)", | ||
| "main": "string-utils.cjs.js", | ||
| "types": "string-utils.d.ts", | ||
| "sideEffects": false, | ||
@@ -25,19 +26,20 @@ "scripts": { | ||
| "test": "jest", | ||
| "coverage": "cat ./coverage/lcov.info | coveralls", | ||
| "build": "npm run build:cjs && npm run build:es", | ||
| "build": "npm run build:cjs && npm run build:es && npm run build:dts", | ||
| "build:cjs": "BABEL_ENV=cjs babel string-utils.js --out-file string-utils.cjs.js --source-maps", | ||
| "build:es": "BABEL_ENV=es babel string-utils.js --out-file string-utils.es.js --source-maps", | ||
| "build:dts": "tsc --allowJs --declaration --emitDeclarationOnly string-utils.js", | ||
| "prepublish": "npm run build" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.8.4", | ||
| "@babel/preset-env": "^7.9.6", | ||
| "coveralls": "^3.1.0", | ||
| "eslint": "^7.0.0", | ||
| "eslint-config-ultraq": "^2.3.2", | ||
| "jest": "^26.0.1" | ||
| "@babel/cli": "^7.22.15", | ||
| "@babel/preset-env": "^7.22.15", | ||
| "@types/jest": "^29.5.4", | ||
| "eslint": "^8.48.0", | ||
| "eslint-config-ultraq": "^2.4.0", | ||
| "jest": "^29.6.4", | ||
| "typescript": "^5.2.2" | ||
| }, | ||
| "engines": { | ||
| "node": ">=10" | ||
| "node": ">=18" | ||
| } | ||
| } |
+7
-5
@@ -5,4 +5,4 @@ | ||
| [](https://travis-ci.com/ultraq/string-utils) | ||
| [](https://coveralls.io/github/ultraq/string-utils?branch=master) | ||
| [](https://github.com/ultraq/string-utils/actions) | ||
| [](https://codecov.io/gh/ultraq/string-utils) | ||
| [](https://www.npmjs.com/package/@ultraq/string-utils) | ||
@@ -39,5 +39,7 @@ [](https://bundlephobia.com/result?p=@ultraq/string-utils) | ||
| - **template**: template string containing placeholders in the format or `{n}` | ||
| - **template**: String containing indexed (`{0}`, `{1}`, ...) or named | ||
| (`{value}`, `{greeting}`, ...) placeholders, but not both. | ||
| where `n` is the corresponding index value to have filled-in. | ||
| - **...values**: argument list of values to fill in the placeholders in the | ||
| template string with. | ||
| - **...values**: Either an argument list / array of values to replace values in | ||
| an indexed template string, or an object where the keys are the names in a | ||
| named template string to replace with their values. |
+20
-13
@@ -8,3 +8,2 @@ "use strict"; | ||
| exports.format = format; | ||
| /* | ||
@@ -30,4 +29,4 @@ * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * | ||
| * @param {String} string | ||
| * @return {String} | ||
| * @param {string} string | ||
| * @return {string} | ||
| * HTML escaped string, safe for use in HTML. | ||
@@ -38,2 +37,3 @@ */ | ||
| } | ||
| /** | ||
@@ -43,10 +43,12 @@ * Returns the replacement of each placeholder in a template string with a | ||
| * | ||
| * @param {String} template | ||
| * @param {...String} values | ||
| * Argument list of values or a single array of values. | ||
| * @return {String} | ||
| * @param {string} template | ||
| * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`, | ||
| * `{greeting}`, ...) placeholders, but not both. | ||
| * @param {...string|Record<string,string>} values | ||
| * Either an argument list / array of values to replace values in an indexed | ||
| * template string, or an object where the keys are the names in a named | ||
| * template string to replace with their values. | ||
| * @return {string} | ||
| * Replaced template string. | ||
| */ | ||
| function format(template) { | ||
@@ -56,8 +58,13 @@ for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| } | ||
| return template.replace(/\{(\d+)\}/g, function (match, index) { | ||
| return values[+index] + ''; | ||
| }); | ||
| if (values && values.length > 0) { | ||
| let arg = values[0]; | ||
| if (values.length === 1 && typeof arg === 'object' && arg !== null) { | ||
| return template.replace(/\{(.+?)\}/g, (match, token) => arg[token]); | ||
| } else { | ||
| return template.replace(/\{(\d+)\}/g, (match, index) => values[+index]); | ||
| } | ||
| } | ||
| return template; | ||
| } | ||
| //# sourceMappingURL=string-utils.cjs.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["string-utils.js"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;AAQO,SAAS,UAAT,CAAoB,MAApB,EAA4B;AAElC,SAAO,OAAO,MAAP,KAAkB,QAAlB,GAA6B,MAA7B,GAAsC,MAAM,CACjD,OAD2C,CACnC,IADmC,EAC7B,OAD6B,EAE3C,OAF2C,CAEnC,IAFmC,EAE7B,MAF6B,EAG3C,OAH2C,CAGnC,IAHmC,EAG7B,MAH6B,EAI3C,OAJ2C,CAInC,IAJmC,EAI7B,QAJ6B,EAK3C,OAL2C,CAKnC,IALmC,EAK7B,QAL6B,CAA7C;AAMA;AAED;;;;;;;;;;;;AAUO,SAAS,MAAT,CAAgB,QAAhB,EAAqC;AAAA,oCAAR,MAAQ;AAAR,IAAA,MAAQ;AAAA;;AAE3C,SAAO,QAAQ,CAAC,OAAT,CAAiB,YAAjB,EAA+B,UAAC,KAAD,EAAQ,KAAR;AAAA,WAAkB,MAAM,CAAC,CAAC,KAAF,CAAN,GAAiB,EAAnC;AAAA,GAA/B,CAAP;AACA","file":"string-utils.cjs.js","sourcesContent":["/* \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 * Escapes special HTML characters in a string with their entity code\n * equivalents.\n * \n * @param {String} string\n * @return {String}\n * HTML escaped string, safe for use in HTML.\n */\nexport function escapeHtml(string) {\n\n\treturn typeof string !== 'string' ? string : string\n\t\t.replace(/&/g, '&')\n\t\t.replace(/</g, '<')\n\t\t.replace(/>/g, '>')\n\t\t.replace(/\"/g, '"')\n\t\t.replace(/'/g, ''');\n}\n\n/**\n * Returns the replacement of each placeholder in a template string with a\n * corresponding replacement value.\n * \n * @param {String} template\n * @param {...String} values\n * Argument list of values or a single array of values.\n * @return {String}\n * Replaced template string.\n */\nexport function format(template, ...values) {\n\n\treturn template.replace(/\\{(\\d+)\\}/g, (match, index) => values[+index] + '');\n}\n"]} | ||
| {"version":3,"file":"string-utils.cjs.js","names":["escapeHtml","string","replace","format","template","_len","arguments","length","values","Array","_key","arg","match","token","index"],"sources":["string-utils.js"],"sourcesContent":["/* \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 * Escapes special HTML characters in a string with their entity code\n * equivalents.\n * \n * @param {string} string\n * @return {string}\n * HTML escaped string, safe for use in HTML.\n */\nexport function escapeHtml(string) {\n\n\treturn typeof string !== 'string' ? string : string\n\t\t.replace(/&/g, '&')\n\t\t.replace(/</g, '<')\n\t\t.replace(/>/g, '>')\n\t\t.replace(/\"/g, '"')\n\t\t.replace(/'/g, ''');\n}\n\n/**\n * Returns the replacement of each placeholder in a template string with a\n * corresponding replacement value.\n * \n * @param {string} template\n * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`,\n * `{greeting}`, ...) placeholders, but not both.\n * @param {...string|Record<string,string>} values\n * Either an argument list / array of values to replace values in an indexed\n * template string, or an object where the keys are the names in a named\n * template string to replace with their values.\n * @return {string}\n * Replaced template string.\n */\nexport function format(template, ...values) {\n\n\tif (values && values.length > 0) {\n\t\tlet arg = values[0];\n\t\tif (values.length === 1 && typeof arg === 'object' && arg !== null) {\n\t\t\treturn template.replace(/\\{(.+?)\\}/g, (match, token) => arg[token]);\n\t\t}\n\t\telse {\n\t\t\treturn template.replace(/\\{(\\d+)\\}/g, (match, index) => values[+index]);\n\t\t}\n\t}\n\treturn template;\n}\n"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,UAAUA,CAACC,MAAM,EAAE;EAElC,OAAO,OAAOA,MAAM,KAAK,QAAQ,GAAGA,MAAM,GAAGA,MAAM,CACjDC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CACtBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CACrBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CACrBA,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CACvBA,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,MAAMA,CAACC,QAAQ,EAAa;EAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAARC,MAAM,OAAAC,KAAA,CAAAJ,IAAA,OAAAA,IAAA,WAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;IAANF,MAAM,CAAAE,IAAA,QAAAJ,SAAA,CAAAI,IAAA;EAAA;EAEzC,IAAIF,MAAM,IAAIA,MAAM,CAACD,MAAM,GAAG,CAAC,EAAE;IAChC,IAAII,GAAG,GAAGH,MAAM,CAAC,CAAC,CAAC;IACnB,IAAIA,MAAM,CAACD,MAAM,KAAK,CAAC,IAAI,OAAOI,GAAG,KAAK,QAAQ,IAAIA,GAAG,KAAK,IAAI,EAAE;MACnE,OAAOP,QAAQ,CAACF,OAAO,CAAC,YAAY,EAAE,CAACU,KAAK,EAAEC,KAAK,KAAKF,GAAG,CAACE,KAAK,CAAC,CAAC;IACpE,CAAC,MACI;MACJ,OAAOT,QAAQ,CAACF,OAAO,CAAC,YAAY,EAAE,CAACU,KAAK,EAAEE,KAAK,KAAKN,MAAM,CAAC,CAACM,KAAK,CAAC,CAAC;IACxE;EACD;EACA,OAAOV,QAAQ;AAChB"} |
+20
-11
@@ -21,4 +21,4 @@ /* | ||
| * | ||
| * @param {String} string | ||
| * @return {String} | ||
| * @param {string} string | ||
| * @return {string} | ||
| * HTML escaped string, safe for use in HTML. | ||
@@ -29,2 +29,3 @@ */ | ||
| } | ||
| /** | ||
@@ -34,9 +35,12 @@ * Returns the replacement of each placeholder in a template string with a | ||
| * | ||
| * @param {String} template | ||
| * @param {...String} values | ||
| * Argument list of values or a single array of values. | ||
| * @return {String} | ||
| * @param {string} template | ||
| * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`, | ||
| * `{greeting}`, ...) placeholders, but not both. | ||
| * @param {...string|Record<string,string>} values | ||
| * Either an argument list / array of values to replace values in an indexed | ||
| * template string, or an object where the keys are the names in a named | ||
| * template string to replace with their values. | ||
| * @return {string} | ||
| * Replaced template string. | ||
| */ | ||
| export function format(template) { | ||
@@ -46,8 +50,13 @@ for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| } | ||
| return template.replace(/\{(\d+)\}/g, function (match, index) { | ||
| return values[+index] + ''; | ||
| }); | ||
| if (values && values.length > 0) { | ||
| let arg = values[0]; | ||
| if (values.length === 1 && typeof arg === 'object' && arg !== null) { | ||
| return template.replace(/\{(.+?)\}/g, (match, token) => arg[token]); | ||
| } else { | ||
| return template.replace(/\{(\d+)\}/g, (match, index) => values[+index]); | ||
| } | ||
| } | ||
| return template; | ||
| } | ||
| //# sourceMappingURL=string-utils.es.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["string-utils.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;AAQA,OAAO,SAAS,UAAT,CAAoB,MAApB,EAA4B;AAElC,SAAO,OAAO,MAAP,KAAkB,QAAlB,GAA6B,MAA7B,GAAsC,MAAM,CACjD,OAD2C,CACnC,IADmC,EAC7B,OAD6B,EAE3C,OAF2C,CAEnC,IAFmC,EAE7B,MAF6B,EAG3C,OAH2C,CAGnC,IAHmC,EAG7B,MAH6B,EAI3C,OAJ2C,CAInC,IAJmC,EAI7B,QAJ6B,EAK3C,OAL2C,CAKnC,IALmC,EAK7B,QAL6B,CAA7C;AAMA;AAED;;;;;;;;;;;AAUA,OAAO,SAAS,MAAT,CAAgB,QAAhB,EAAqC;AAAA,oCAAR,MAAQ;AAAR,IAAA,MAAQ;AAAA;;AAE3C,SAAO,QAAQ,CAAC,OAAT,CAAiB,YAAjB,EAA+B,UAAC,KAAD,EAAQ,KAAR;AAAA,WAAkB,MAAM,CAAC,CAAC,KAAF,CAAN,GAAiB,EAAnC;AAAA,GAA/B,CAAP;AACA","file":"string-utils.es.js","sourcesContent":["/* \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 * Escapes special HTML characters in a string with their entity code\n * equivalents.\n * \n * @param {String} string\n * @return {String}\n * HTML escaped string, safe for use in HTML.\n */\nexport function escapeHtml(string) {\n\n\treturn typeof string !== 'string' ? string : string\n\t\t.replace(/&/g, '&')\n\t\t.replace(/</g, '<')\n\t\t.replace(/>/g, '>')\n\t\t.replace(/\"/g, '"')\n\t\t.replace(/'/g, ''');\n}\n\n/**\n * Returns the replacement of each placeholder in a template string with a\n * corresponding replacement value.\n * \n * @param {String} template\n * @param {...String} values\n * Argument list of values or a single array of values.\n * @return {String}\n * Replaced template string.\n */\nexport function format(template, ...values) {\n\n\treturn template.replace(/\\{(\\d+)\\}/g, (match, index) => values[+index] + '');\n}\n"]} | ||
| {"version":3,"file":"string-utils.es.js","names":["escapeHtml","string","replace","format","template","_len","arguments","length","values","Array","_key","arg","match","token","index"],"sources":["string-utils.js"],"sourcesContent":["/* \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 * Escapes special HTML characters in a string with their entity code\n * equivalents.\n * \n * @param {string} string\n * @return {string}\n * HTML escaped string, safe for use in HTML.\n */\nexport function escapeHtml(string) {\n\n\treturn typeof string !== 'string' ? string : string\n\t\t.replace(/&/g, '&')\n\t\t.replace(/</g, '<')\n\t\t.replace(/>/g, '>')\n\t\t.replace(/\"/g, '"')\n\t\t.replace(/'/g, ''');\n}\n\n/**\n * Returns the replacement of each placeholder in a template string with a\n * corresponding replacement value.\n * \n * @param {string} template\n * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`,\n * `{greeting}`, ...) placeholders, but not both.\n * @param {...string|Record<string,string>} values\n * Either an argument list / array of values to replace values in an indexed\n * template string, or an object where the keys are the names in a named\n * template string to replace with their values.\n * @return {string}\n * Replaced template string.\n */\nexport function format(template, ...values) {\n\n\tif (values && values.length > 0) {\n\t\tlet arg = values[0];\n\t\tif (values.length === 1 && typeof arg === 'object' && arg !== null) {\n\t\t\treturn template.replace(/\\{(.+?)\\}/g, (match, token) => arg[token]);\n\t\t}\n\t\telse {\n\t\t\treturn template.replace(/\\{(\\d+)\\}/g, (match, index) => values[+index]);\n\t\t}\n\t}\n\treturn template;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,UAAUA,CAACC,MAAM,EAAE;EAElC,OAAO,OAAOA,MAAM,KAAK,QAAQ,GAAGA,MAAM,GAAGA,MAAM,CACjDC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CACtBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CACrBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CACrBA,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CACvBA,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CAACC,QAAQ,EAAa;EAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAARC,MAAM,OAAAC,KAAA,CAAAJ,IAAA,OAAAA,IAAA,WAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;IAANF,MAAM,CAAAE,IAAA,QAAAJ,SAAA,CAAAI,IAAA;EAAA;EAEzC,IAAIF,MAAM,IAAIA,MAAM,CAACD,MAAM,GAAG,CAAC,EAAE;IAChC,IAAII,GAAG,GAAGH,MAAM,CAAC,CAAC,CAAC;IACnB,IAAIA,MAAM,CAACD,MAAM,KAAK,CAAC,IAAI,OAAOI,GAAG,KAAK,QAAQ,IAAIA,GAAG,KAAK,IAAI,EAAE;MACnE,OAAOP,QAAQ,CAACF,OAAO,CAAC,YAAY,EAAE,CAACU,KAAK,EAAEC,KAAK,KAAKF,GAAG,CAACE,KAAK,CAAC,CAAC;IACpE,CAAC,MACI;MACJ,OAAOT,QAAQ,CAACF,OAAO,CAAC,YAAY,EAAE,CAACU,KAAK,EAAEE,KAAK,KAAKN,MAAM,CAAC,CAACM,KAAK,CAAC,CAAC;IACxE;EACD;EACA,OAAOV,QAAQ;AAChB"} |
+20
-7
@@ -21,4 +21,4 @@ /* | ||
| * | ||
| * @param {String} string | ||
| * @return {String} | ||
| * @param {string} string | ||
| * @return {string} | ||
| * HTML escaped string, safe for use in HTML. | ||
@@ -40,6 +40,10 @@ */ | ||
| * | ||
| * @param {String} template | ||
| * @param {...String} values | ||
| * Argument list of values or a single array of values. | ||
| * @return {String} | ||
| * @param {string} template | ||
| * String containing indexed (`{0}`, `{1}`, ...) or named (`{value}`, | ||
| * `{greeting}`, ...) placeholders, but not both. | ||
| * @param {...string|Record<string,string>} values | ||
| * Either an argument list / array of values to replace values in an indexed | ||
| * template string, or an object where the keys are the names in a named | ||
| * template string to replace with their values. | ||
| * @return {string} | ||
| * Replaced template string. | ||
@@ -49,3 +53,12 @@ */ | ||
| return template.replace(/\{(\d+)\}/g, (match, index) => values[+index] + ''); | ||
| if (values && values.length > 0) { | ||
| let arg = values[0]; | ||
| if (values.length === 1 && typeof arg === 'object' && arg !== null) { | ||
| return template.replace(/\{(.+?)\}/g, (match, token) => arg[token]); | ||
| } | ||
| else { | ||
| return template.replace(/\{(\d+)\}/g, (match, index) => values[+index]); | ||
| } | ||
| } | ||
| return template; | ||
| } |
+27
-4
@@ -23,3 +23,3 @@ /* | ||
| */ | ||
| describe('StringUtils', function() { | ||
| describe('string-utils', function() { | ||
@@ -43,5 +43,5 @@ describe('#escapeHtml', function() { | ||
| describe('#format', function() { | ||
| let template = "There's a {0} in my {1}, dear {2}"; | ||
| test('Should replace each token with the corresponding value', function() { | ||
| test('Should replace indexed tokens with the corresponding value', function() { | ||
| let template = "There's a {0} in my {1}, dear {2}"; | ||
| let result = format(template, 'hole', 'bucket', 'Liza'); | ||
@@ -51,7 +51,30 @@ expect(result).toBe("There's a hole in my bucket, dear Liza"); | ||
| test('Non-string values should be coerced to strings before formatting', function() { | ||
| // Not sure if this test ever covered anything now | ||
| test.skip('Non-string values should be coerced to strings before formatting', function() { | ||
| let template = "There's a {0} in my {1}, dear {2}"; | ||
| let result = format(template, 0, 1, 2); | ||
| expect(result).toBe("There's a 0 in my 1, dear 2"); | ||
| }); | ||
| test('Should replace named tokens with the corresponding value', function() { | ||
| let template = "There's a {badThing} in my {posession}, dear {name}"; | ||
| let result = format(template, { | ||
| badThing: 'hole', | ||
| posession: 'bucket', | ||
| name: 'Liza' | ||
| }); | ||
| expect(result).toBe("There's a hole in my bucket, dear Liza"); | ||
| }); | ||
| test('Returns the template string if there are no replacements', function() { | ||
| let template, result; | ||
| template = "There's a {0} in my {1}, dear {2}"; | ||
| result = format(template); | ||
| expect(result).toBe(template); | ||
| template = "There's a {badThing} in my {posession}, dear {name}"; | ||
| result = format(template); | ||
| }); | ||
| }); | ||
| }); |
31166
24.27%11
10%270
39.18%44
4.76%7
16.67%