lesetid
Version:
A dead simple read time estimation
104 lines (102 loc) • 2.78 kB
JavaScript
//#region src/utils.ts
/**
* Checks if a character code falls within a specified range (inclusive).
*
* @param {number} code - The character code to check.
* @param {number} start - The start of the range (inclusive).
* @param {number} end - The end of the range (inclusive).
* @returns {boolean} - true if the character code is within the range, false otherwise.
*/
function isCharCodeIntersected(code, start, end) {
return code >= start && code <= end;
}
const CJK_CODE_RANGES = [
[12352, 12447],
[19968, 40959],
[44032, 55203],
[131072, 191456]
];
/**
* Checks if a character is a CJK character.
*
* NOTE: That the ranges are maybe not complete.
* You can see the full list of code ranges in `CJK_CODE_RANGES` export.
*
* @param {string | undefined} char - the character to check.
* @returns {boolean} - true if the character is a CJK character, false otherwise.
*/
const isCJK = (char) => {
if (!char) return false;
const charCode = char.charCodeAt(0);
for (const [start, end] of CJK_CODE_RANGES) {
if (!start || !end) return false;
if (isCharCodeIntersected(charCode, start, end)) return true;
}
return false;
};
const PUNCTATION_CODE_RANGES = [
[33, 47],
[58, 64],
[91, 96],
[123, 126],
[12288, 12351],
[65280, 65519]
];
/**
* Checks if a character is a punctuation character.
*
* NOTE: That the ranges are maybe not complete.
* You can see the full list of code ranges in `PUNCTATION_CODE_RANGES` export.
*
* @param {string | undefined} char - the character to check.
* @returns {boolean} - true if the character is a punctuation character, false otherwise.
*/
const isPunctuation = (char) => {
if (!char) return false;
const charCode = char.charCodeAt(0);
for (const [start, end] of PUNCTATION_CODE_RANGES) {
if (!start || !end) return false;
if (isCharCodeIntersected(charCode, start, end)) return true;
}
return false;
};
/**
* Checks if a character is a ansi character.
* @param {string | undefined} char - the character to check.
* @returns {boolean} - true if the character is a word character, false otherwise.
*/
const isAnsi = (char) => {
if (!char) return false;
return " \n\r ".includes(char);
};
//#endregion
Object.defineProperty(exports, 'CJK_CODE_RANGES', {
enumerable: true,
get: function () {
return CJK_CODE_RANGES;
}
});
Object.defineProperty(exports, 'PUNCTATION_CODE_RANGES', {
enumerable: true,
get: function () {
return PUNCTATION_CODE_RANGES;
}
});
Object.defineProperty(exports, 'isAnsi', {
enumerable: true,
get: function () {
return isAnsi;
}
});
Object.defineProperty(exports, 'isCJK', {
enumerable: true,
get: function () {
return isCJK;
}
});
Object.defineProperty(exports, 'isPunctuation', {
enumerable: true,
get: function () {
return isPunctuation;
}
});