瀏覽代碼

nissaya 语尾切分提取为函数 nissayaBase

visuddhinanda 2 年之前
父節點
當前提交
6e95b87fda
共有 1 個文件被更改,包括 21 次插入17 次删除
  1. 21 17
      dashboard/src/components/template/Nissaya/NissayaMeaning.tsx

+ 21 - 17
dashboard/src/components/template/Nissaya/NissayaMeaning.tsx

@@ -5,11 +5,29 @@ import { getEnding } from "../../../reducers/nissaya-ending-vocabulary";
 import Lookup from "../../dict/Lookup";
 import { NissayaCardPop } from "../../general/NissayaCard";
 
-interface IMeaning {
+export interface IMeaning {
   base: string;
   ending?: string[];
 }
 
+export const nissayaBase = (item: string, endings: string[]): IMeaning => {
+  let word = item.replaceAll("။", "");
+  let end: string[] = [];
+  for (let loop = 0; loop < 3; loop++) {
+    for (let i = 0; i < word.length; i++) {
+      const ending = word.slice(i);
+      if (endings?.includes(ending)) {
+        end.unshift(word.slice(i));
+        word = word.slice(0, i);
+      }
+    }
+  }
+  return {
+    base: word,
+    ending: end,
+  };
+};
+
 interface IWidget {
   text?: string;
   code?: string;
@@ -20,26 +38,12 @@ const NissayaMeaningWidget = ({ text, code = "my" }: IWidget) => {
   const endings = useAppSelector(getEnding);
 
   useEffect(() => {
-    if (typeof text === "undefined") {
+    if (typeof text === "undefined" || typeof endings === "undefined") {
       return;
     }
 
     const mWords: IMeaning[] = text.split(" ").map((item) => {
-      let word = item.replaceAll("။", "");
-      let end: string[] = [];
-      for (let loop = 0; loop < 3; loop++) {
-        for (let i = 0; i < word.length; i++) {
-          const ending = word.slice(i);
-          if (endings?.includes(ending)) {
-            end.unshift(word.slice(i));
-            word = word.slice(0, i);
-          }
-        }
-      }
-      return {
-        base: word,
-        ending: end,
-      };
+      return nissayaBase(item, endings);
     });
     setWords(mWords);
   }, [endings, text]);