Files
ingglish-hyperdope-gematria/node_modules/@ingglish/normalize/dist/index.js
T

223 lines
6.1 KiB
JavaScript

// src/case.ts
function applyCasePattern(word, pattern, original) {
if (pattern === "lower") {
const firstChar = word.codePointAt(0);
if (firstChar >= 97 && firstChar <= 122 && word === word.toLowerCase()) {
return word;
}
return word.toLowerCase();
}
switch (pattern) {
case "capitalized": {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
case "mixed": {
if (original !== void 0 && original.length > 0) {
return applyMixedCase(word, original);
}
return word.toLowerCase();
}
case "upper": {
return word.toUpperCase();
}
default: {
return word.toLowerCase();
}
}
}
function detectCasePattern(word) {
if (word.length === 0) {
return "lower";
}
const firstCode = word.codePointAt(0);
const firstChar = word[0];
const isFirstUpper = firstCode >= 65 && firstCode <= 90 || // A-Z (fast ASCII path)
firstCode > 127 && firstChar !== firstChar.toLowerCase();
if (!isFirstUpper && word === word.toLowerCase()) {
return "lower";
}
if (word.length === 1) {
if (word === "I") {
return "lower";
}
return isFirstUpper ? "capitalized" : "lower";
}
if (word === word.toUpperCase()) {
if (word.length <= 2) {
return "capitalized";
}
return "upper";
}
if (isFirstUpper) {
let restIsLower = true;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90 || // A-Z
c > 127 && word[i] !== word[i].toLowerCase()) {
restIsLower = false;
break;
}
}
if (restIsLower) {
return "capitalized";
}
}
return "mixed";
}
function splitCamelCase(word) {
if (word.length < 2) {
return null;
}
let hasInternalUpper = false;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90) {
hasInternalUpper = true;
break;
}
}
if (!hasInternalUpper) {
return null;
}
const parts = [];
let start = 0;
for (let i = 1; i < word.length; i++) {
const prevCode = word.codePointAt(i - 1);
const currCode = word.codePointAt(i);
if (prevCode >= 97 && prevCode <= 122 && currCode >= 65 && currCode <= 90) {
parts.push(word.slice(start, i));
start = i;
}
}
if (parts.length > 0) {
parts.push(word.slice(start));
return parts;
}
return null;
}
function applyMixedCase(translated, original) {
const lowerTranslated = translated.toLowerCase();
let result = "";
let i = 0;
for (const char of lowerTranslated) {
if (i < original.length) {
const origChar = original[i];
result += origChar === origChar.toUpperCase() ? char.toUpperCase() : char;
} else {
result += char;
}
i++;
}
return result;
}
// src/text.ts
var URL_REGEX = /(?:https?|ftp|file):\/\/[^\s<>"')\]]+/gi;
var EMAIL_REGEX = /[\w.%+-]{1,64}@(?:[a-z0-9-]{1,63}\.){1,10}[a-z]{2,63}/gi;
var COMMON_TLDS = "com|org|net|edu|gov|io|co|uk|de|fr|jp|au|ca|ru|ch|it|nl|se|no|es|mil|info|biz|tv|me|app|dev|ai|xyz|rs|site|tech|blog|news|club|lol|new|world|online|space|fun|live|shop|store|gg|fm|im|is|to|cc|ws|ly|gl|be|us|in|eu|asia|pro|cz|pl|fi|dk|pt|ie|nz|za|br|mx|ar|cl|kr|tw|hk|sg|id|th|vn|ph";
var BARE_DOMAIN_REGEX = new RegExp(
String.raw`\b(?:[a-z0-9][-a-z0-9]*\.)+(?:${COMMON_TLDS})\b(?:\/[^\s<>"')\]]*)?`,
"gi"
);
function extractPreservedPatterns(text) {
const preserved = /* @__PURE__ */ new Map();
let counter = 0;
let result = text;
if (text.includes("://")) {
result = result.replaceAll(URL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes("@")) {
result = result.replaceAll(EMAIL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes(".")) {
result = result.replaceAll(BARE_DOMAIN_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
return { preserved, text: result };
}
var FANCY_APOSTROPHE = /[\u2018\u2019\u02BC]/g;
var COMBINING_MARKS = /[\u0300-\u036F]/g;
function normalizeApostrophes(text) {
return text.replaceAll(FANCY_APOSTROPHE, "'");
}
function stripDiacritics(text) {
return text.normalize("NFD").replaceAll(COMBINING_MARKS, "");
}
// src/tokenize.ts
var WORD_SPLIT_REGEX = /((?<!\d)[a-zA-Z\u00C0-\u024F']+(?!\d))/;
var WORD_TEST_REGEX = /^[a-zA-Z\u00C0-\u024F']+$/;
var IPA_SYMBOLS_SET = new Set("\u0259\u025D\u025A\u028C\xE6\u0251\u0254\u025B\u026A\u028A\xF0\u03B8\u0283\u0292\u014B\u0279\u0261");
function isIPAChar(char) {
const code = char.codePointAt(0);
if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
return true;
}
if (code === 8288) {
return true;
}
if (code === 712 || code === 716) {
return true;
}
if (IPA_SYMBOLS_SET.has(char)) {
return true;
}
return false;
}
function tokenizeIPA(text) {
const tokens = [];
let i = 0;
while (i < text.length) {
if (isIPAChar(text[i])) {
let wordEnd = i + 1;
while (wordEnd < text.length && isIPAChar(text[wordEnd])) {
wordEnd++;
}
tokens.push({ isWord: true, text: text.slice(i, wordEnd) });
i = wordEnd;
} else {
let nonWordEnd = i + 1;
while (nonWordEnd < text.length && !isIPAChar(text[nonWordEnd])) {
nonWordEnd++;
}
tokens.push({ isWord: false, text: text.slice(i, nonWordEnd) });
i = nonWordEnd;
}
}
return tokens;
}
function tokenizeText(text) {
const normalized = normalizeApostrophes(text);
const parts = normalized.split(WORD_SPLIT_REGEX);
const tokens = [];
for (const part of parts) {
if (part.length > 0) {
tokens.push({ isWord: WORD_TEST_REGEX.test(part), text: part });
}
}
return tokens;
}
export {
WORD_SPLIT_REGEX,
WORD_TEST_REGEX,
applyCasePattern,
detectCasePattern,
extractPreservedPatterns,
normalizeApostrophes,
splitCamelCase,
stripDiacritics,
tokenizeIPA,
tokenizeText
};