@ultraq/string-utils
Advanced tools
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.escapeHtml = escapeHtml; | ||
| exports.format = format; | ||
| /* | ||
| * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| /** | ||
| * 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. | ||
| */ | ||
| function escapeHtml(string) { | ||
| return typeof string !== 'string' ? string : string.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '''); | ||
| } | ||
| /** | ||
| * 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. | ||
| */ | ||
| function format(template, ...values) { | ||
| 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; | ||
| } |
| export const __esModule: boolean; | ||
| /** | ||
| * 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; |
+28
-10
| { | ||
| "name": "@ultraq/string-utils", | ||
| "version": "3.0.0", | ||
| "version": "3.1.0", | ||
| "description": "A collection of utilities for JavaScript strings", | ||
@@ -18,5 +18,22 @@ "author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)", | ||
| ], | ||
| "module": "string-utils.es.js", | ||
| "main": "string-utils.cjs.js", | ||
| "type": "module", | ||
| "module": "string-utils.js", | ||
| "main": "string-utils.cjs", | ||
| "types": "string-utils.d.ts", | ||
| "exports": { | ||
| "import": { | ||
| "default": "./string-utils.js", | ||
| "types": "./string-utils.d.ts" | ||
| }, | ||
| "require": { | ||
| "default": "./string-utils.cjs", | ||
| "types": "./string-utils.d.cts" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "string-utils.js", | ||
| "string-utils.cjs", | ||
| "string-utils.d.ts", | ||
| "string-utils.d.cts" | ||
| ], | ||
| "sideEffects": false, | ||
@@ -26,14 +43,15 @@ "scripts": { | ||
| "test": "jest", | ||
| "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" | ||
| "build": "npm run build:cjs && npm run build:dts", | ||
| "build:cjs": "babel string-utils.js --out-file string-utils.cjs", | ||
| "build:dts": "tsc --allowJs --declaration --emitDeclarationOnly string-utils.js string-utils.cjs", | ||
| "prepublishOnly": "npm run build" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.22.15", | ||
| "@babel/preset-env": "^7.22.15", | ||
| "@babel/plugin-transform-modules-commonjs": "^7.23.3", | ||
| "@types/jest": "^29.5.4", | ||
| "eslint": "^8.48.0", | ||
| "eslint-config-ultraq": "^2.4.0", | ||
| "eslint-config-ultraq": "^3.1.1", | ||
| "eslint-plugin-import": "^2.29.1", | ||
| "eslint-plugin-jsdoc": "^46.8.2", | ||
| "jest": "^29.6.4", | ||
@@ -40,0 +58,0 @@ "typescript": "^5.2.2" |
| /* eslint-env node */ | ||
| 'use strict'; // eslint-disable-line | ||
| module.exports = { | ||
| collectCoverage: true, | ||
| coverageReporters: [ | ||
| 'html', | ||
| 'lcov', | ||
| 'text-summary' | ||
| ] | ||
| }; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.escapeHtml = escapeHtml; | ||
| exports.format = format; | ||
| /* | ||
| * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| /** | ||
| * 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. | ||
| */ | ||
| function escapeHtml(string) { | ||
| return typeof string !== 'string' ? string : string.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '''); | ||
| } | ||
| /** | ||
| * 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. | ||
| */ | ||
| function format(template) { | ||
| for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| values[_key - 1] = arguments[_key]; | ||
| } | ||
| 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 |
| {"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"} |
| /* | ||
| * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| /** | ||
| * 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) { | ||
| return typeof string !== 'string' ? string : string.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '''); | ||
| } | ||
| /** | ||
| * 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) { | ||
| for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| values[_key - 1] = arguments[_key]; | ||
| } | ||
| 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 |
| {"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"} |
| /* | ||
| * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| /* eslint-env jest */ | ||
| import {escapeHtml, format} from './string-utils'; | ||
| /** | ||
| * Tests for the string utilities. | ||
| */ | ||
| describe('string-utils', function() { | ||
| describe('#escapeHtml', function() { | ||
| test('Escape special characters', function() { | ||
| let string = `This & that < thing > sing "ring" 'king'`; | ||
| let result = escapeHtml(string); | ||
| expect(result).toBe(`This & that < thing > sing "ring" 'king'`); | ||
| }); | ||
| test('Return the value back to the user if the type is not a string', function() { | ||
| [null, undefined, 123, Date.now, []].forEach(value => { | ||
| let result = escapeHtml(value); | ||
| expect(value).toBe(result); | ||
| }); | ||
| }); | ||
| }); | ||
| describe('#format', 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'); | ||
| expect(result).toBe("There's a hole in my bucket, dear Liza"); | ||
| }); | ||
| // 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); | ||
| }); | ||
| }); | ||
| }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Yes
NaN20343
-34.73%9
28.57%7
-36.36%138
-48.89%1
Infinity%