escape-unicode
Version:
Library to escape Unicode characters
151 lines • 6.38 kB
JavaScript
;
/*
* Copyright (C) 2025 neocotic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceCodes = exports.replaceCode = exports.replaceChars = exports.replaceChar = exports.composeReplacer = void 0;
/**
* Returns a {@link Replacer} composed of the specified `replacers` that returns the replacement string returned from
* the first {@link Replacer} to return a string, where possible.
*
* @example
* const replacer = composeReplacer(
* replaceChars({ "\f": "\\f", "\n": "\\n", "\r": "\\r" }),
* replaceCodes({ 0x009: "\\t" }),
* );
* escapeUnicode("I ♥ Unicode!", { replacer });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @param replacers The {@link Replacer} functions to be composed.
* @return A {@link Replacer} composed of multiple `replacers`.
*/
const composeReplacer = (...replacers) => (code, char) => {
for (const replacer of replacers) {
const replacement = replacer(code, char);
if (typeof replacement === "string") {
return replacement;
}
}
};
exports.composeReplacer = composeReplacer;
/**
* Returns a {@link Replacer} that returns the specified `replacement` string for the individual Unicode character
* provided.
*
* @example
* escapeUnicode("I ♥ Unicode!", { replacer: replaceChar("\t", "\\t") });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @param char The individual Unicode character.
* @param replacement The replacement string to be returned for `char`.
* @return A {@link Replacer} that returns `replacement` string only for `char`.
*/
const replaceChar = (char, replacement) => (_code, otherChar) => {
if (otherChar === char) {
return replacement;
}
};
exports.replaceChar = replaceChar;
/**
* Returns a {@link Replacer} that returns replacement strings looked up from the specified `replacements`, where
* possible.
*
* The keys within `replacements` are expected to be the individual Unicode character.
*
* @example Providing replacements in a map
* const replacements = new Map([
* ["\f", "\\f"],
* ["\n", "\\n"],
* ["\r", "\\r"],
* ["\t", "\\t"],
* ]);
* escapeUnicode("I ♥ Unicode!", { replacer: replaceChars(replacements) });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @example Providing replacements in an object
* const replacements = {
* "\f": "\\f",
* "\n": "\\n",
* "\r": "\\r",
* "\t": "\\t",
* };
* escapeUnicode("I ♥ Unicode!", { replacer: replaceChars(replacements) });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @param replacements A map/object containing individual Unicode characters mapped to their replacement strings.
* @return A {@link Replacer} that returns replacement strings looked up from `replacements`.
*/
const replaceChars = (replacements) => {
if (replacements instanceof Map) {
return (_code, char) => replacements.get(char);
}
return (_code, char) => replacements[char];
};
exports.replaceChars = replaceChars;
/**
* Returns a {@link Replacer} that returns the specified `replacement` string for the Unicode code point representing
* the individual Unicode character provided.
*
* @example
* escapeUnicode("I ♥ Unicode!", { replacer: replaceCode(0x0009, "\\t") });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @param code The Unicode code point representing the individual Unicode character.
* @param replacement The replacement string to be returned for `char`.
* @return A {@link Replacer} that returns `replacement` string only for `code`.
*/
const replaceCode = (code, replacement) => (otherCode) => {
if (otherCode === code) {
return replacement;
}
};
exports.replaceCode = replaceCode;
/**
* Returns a {@link Replacer} that returns replacement strings looked up from the specified `replacements`, where
* possible.
*
* The keys within `replacements` are expected to be Unicode code points representing the Unicode characters.
*
* @example Providing replacements in a map
* const replacements = new Map([
* [0x000c, "\\f"],
* [0x000a, "\\n"],
* [0x000d, "\\r"],
* [0x0009, "\\t"],
* ]);
* escapeUnicode("I ♥ Unicode!", { replacer: replaceCodes(replacements) });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @example Providing replacements in an object
* const replacements = {
* 0x000c: "\\f",
* 0x000a: "\\n",
* 0x000d: "\\r",
* 0x0009: "\\t",
* };
* escapeUnicode("I ♥ Unicode!", { replacer: replaceCodes(replacements) });
* //=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
* @param replacements A map/object containing Unicode code points representing individual Unicode characters mapped to
* their replacement strings.
* @return A {@link Replacer} that returns replacement strings looked up from `replacements`.
*/
const replaceCodes = (replacements) => {
if (replacements instanceof Map) {
return (code) => replacements.get(code);
}
return (code) => replacements[code];
};
exports.replaceCodes = replaceCodes;
//# sourceMappingURL=replacer.js.map