From b4cfd8808117ab53ceb5d1ee2a615b1fabee6ad2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 14 Jun 2026 23:20:15 +0000 Subject: [PATCH] perf: optimize getSubWords by replacing array spread with push Co-authored-by: lemononmars <26631189+lemononmars@users.noreply.github.com> --- src/lib/utils/thaiwords.ts | 82 +++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/src/lib/utils/thaiwords.ts b/src/lib/utils/thaiwords.ts index 64375e1..a2cd23d 100644 --- a/src/lib/utils/thaiwords.ts +++ b/src/lib/utils/thaiwords.ts @@ -1,77 +1,75 @@ -import dict from '$lib/utils/dict' +import dict from '$lib/utils/dict'; export function splitWord(word: string) { - var alphas = word.split(""); - var out: string[] = []; - alphas.forEach(function (a) { - if (a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/) || a.match(/[\.\*\/]/)) { - out.push(a); - } - else { - out[out.length - 1] += a; - } - }); - return out; + var alphas = word.split(''); + var out: string[] = []; + alphas.forEach(function (a) { + if (a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/) || a.match(/[\.\*\/]/)) { + out.push(a); + } else { + out[out.length - 1] += a; + } + }); + return out; } export function isLegal(a: string) { - return a.length == 1 && a.match(/[\u0E00-\u0E4C]/) !== null + return a.length == 1 && a.match(/[\u0E00-\u0E4C]/) !== null; } export function appendable(a: string, b: string) { - return a.match(consonantRegex) && (b.match(upperLetterRegex) || b.match(lowerLetterRegex)) + return a.match(consonantRegex) && (b.match(upperLetterRegex) || b.match(lowerLetterRegex)); } export function isUpper(a: string) { - return a.match(upperLetterRegex) !== null + return a.match(upperLetterRegex) !== null; } export function isLower(a: string) { - return a.match(lowerLetterRegex) !== null + return a.match(lowerLetterRegex) !== null; } export function isMiddle(a: string) { - return a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/) + return a.match(/[ก-ฮ]/) || a.match(/[ใเแโไาำะๆฯฤา]/); } export function isTall(a: string) { - return a === 'ใ' || a === 'ไ' || a === 'โ' + return a === 'ใ' || a === 'ไ' || a === 'โ'; } export function wordLength(word: string) { - return word.replace(/[\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]/g, "").length + return word.replace(/[\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]/g, '').length; } export function getFirstLetter(word: string) { - for(var l of word.split("")) - if(l.match(/[ก-ฮ]/)) return l - return "" + for (var l of word.split('')) if (l.match(/[ก-ฮ]/)) return l; + return ''; } -export const consonantRegex = /[\u0E01-\u0E2E]/ -export const upperLetterRegex = /[\u0E31\u0E34-\u0E37\u0E47-\u0E4C`]/ -export const lowerLetterRegex = /[\u0E38-\u0E39]/ +export const consonantRegex = /[\u0E01-\u0E2E]/; +export const upperLetterRegex = /[\u0E31\u0E34-\u0E37\u0E47-\u0E4C`]/; +export const lowerLetterRegex = /[\u0E38-\u0E39]/; export function search(word: string) { - return dict.includes(word) + return dict.includes(word); } export function getSubWords(word: string, allowRepeat: boolean = true) { - let subWords: string[] = [] + let subWords: string[] = []; - if(allowRepeat) - for(const w of dict) { - if(w.split("").every(l => word.includes(l))) { - subWords = [...subWords, w] - } - } - else - for(const w of dict) { - const a = w.split("") - if(new Set(a).size == a.length && a.every(l => word.includes(l))) { - subWords = [...subWords, w] - } - } + if (allowRepeat) + for (const w of dict) { + if (w.split('').every((l) => word.includes(l))) { + subWords.push(w); + } + } + else + for (const w of dict) { + const a = w.split(''); + if (new Set(a).size == a.length && a.every((l) => word.includes(l))) { + subWords.push(w); + } + } - return subWords -} \ No newline at end of file + return subWords; +}