escape-unicode
Version:
Library to escape Unicode characters
111 lines • 4.75 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.isNotLatin1 = exports.isLatin1 = exports.isNotBmp = exports.isBmp = exports.isNotAscii = exports.isAscii = exports.composeFilter = void 0;
/**
* Returns a {@link Filter} composed of the specified `filters` that returns `true` only if any of the `filters`
* provided return a truthy value.
*
* @example
* const filter = composeFilter(isNotAscii, (code) => code === 0x0020);
* escapeUnicode("I ♥ Unicode!", { filter });
* //=> "I\\u0020\\u2665\\u0020Unicode!"
* @param filters The {@link Filter} functions to be composed.
* @return A {@link Filter} composed of multiple `filters`.
*/
const composeFilter = (...filters) => (code, char) => {
for (const filter of filters) {
if (filter(code, char)) {
return true;
}
}
return false;
};
exports.composeFilter = composeFilter;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is valid in ASCII encoding.
*
* ASCII covers code points 0x00-0x7F (0-127).
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is valid in ASCII encoding and should be converted to a Unicode escape.
*/
const isAscii = (code) => code <= 0x007f;
exports.isAscii = isAscii;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is **not** valid in ASCII encoding.
*
* ASCII covers code points 0x00-0x7F (0-127).
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is **not** valid in ASCII encoding and should be converted to a Unicode
* escape.
*/
const isNotAscii = (code) => code > 0x007f;
exports.isNotAscii = isNotAscii;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is in the Basic Multilingual Plane (BMP).
*
* BMP covers code points 0x0000-0xFFFF (0-65535) and represents characters that can be encoded in a single UTF-16 code
* unit.
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is in the BMP and should be converted to a Unicode escape.
*/
const isBmp = (code) => code <= 0xffff;
exports.isBmp = isBmp;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is **not** in the Basic Multilingual Plane
* (BMP).
*
* BMP covers code points 0x0000-0xFFFF (0-65535) and represents characters that can be encoded in a single UTF-16 code
* unit.
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is **not** in the BMP and should be converted to a Unicode escape.
*/
const isNotBmp = (code) => code > 0xffff;
exports.isNotBmp = isNotBmp;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is valid in Latin-1 (ISO 8859-1) encoding.
*
* Latin-1 covers code points 0x00-0xFF (0-255).
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is valid in Latin-1 encoding and should be converted to a Unicode escape.
*/
const isLatin1 = (code) => code <= 0x00ff;
exports.isLatin1 = isLatin1;
/**
* A {@link Filter} that returns whether the specified Unicode `code` point is **not** valid in Latin-1 (ISO 8859-1)
* encoding.
*
* Latin-1 covers code points 0x00-0xFF (0-255).
*
* @param code The Unicode code unit to be checked.
* @return A truthy value if the code point is **not** valid in Latin-1 encoding and should be converted to a Unicode
* escape.
*/
const isNotLatin1 = (code) => code > 0x00ff;
exports.isNotLatin1 = isNotLatin1;
//# sourceMappingURL=filter.js.map