switch-chinese 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/stcasc.lib.js +79 -73
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "switch-chinese",
3
- "version": "1.0.8",
4
- "description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for bidirectional conversion between Simplified and Traditional Chinese with intelligent word segmentation, custom dictionary support, character detection, and multiple output formats. Zero dependencies. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
3
+ "version": "1.0.10",
4
+ "description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for conversion between Simplified and Traditional Chinese. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
5
5
  "main": "stcasc.lib.js",
6
6
  "types": "stcasc.d.ts",
7
7
  "type": "module",
package/stcasc.lib.js CHANGED
@@ -837,84 +837,90 @@ function detect(text) {
837
837
  }
838
838
 
839
839
  function stcasc(cache, custom, disableTerms) {
840
- if (!cache) cache = {};
841
- if (cache.sc2tcCombTree && cache.tc2scCombTree) {
842
- sc2tcCombTree = cache.sc2tcCombTree;
840
+ if (!cache) cache = {};
841
+ if (cache.sc2tcCombTree && cache.tc2scCombTree) {
842
+ sc2tcCombTree = cache.sc2tcCombTree;
843
843
  tc2scCombTree = cache.tc2scCombTree;
844
- } else {
844
+ } else {
845
845
  if (disableTerms) sc2tcComb = {};
846
- if (custom && custom.length) {
847
- for (let sc in custom) {
848
- sc2tcComb[sc] = custom[sc];
849
- }
850
- }
851
- function makeCombTree(key, value) {
852
- let curTree = sc2tcCombTree;
853
- for (let i = 0; i < key.length; i++) {
854
- let newTree = {};
855
- if (i == key.length - 1) {
856
- newTree = {"end": value};
857
- }
858
- let curKey = key.charAt(i);
859
- let branch = curTree[curKey];
860
- if (!branch) {
861
- curTree[curKey] = newTree;
862
- curTree = newTree;
863
- } else {
864
- curTree = branch;
865
- }
866
- }
867
- curTree = tc2scCombTree;
868
- for (let i = 0; i < value.length; i++) {
869
- let newTree = {};
870
- if (i == value.length - 1) {
871
- newTree = {"end": key};
872
- }
873
- let curKey = value.charAt(i);
874
- let branch = curTree[curKey];
875
- if (!branch) {
876
- curTree[curKey] = newTree;
877
- curTree = newTree;
878
- } else {
879
- curTree = branch;
880
- }
881
- }
882
- }
883
- for (let key in sc2tcComb) {
884
- let value = sc2tcComb[key];
885
- if (Array.isArray(value)) {
886
- value.forEach(v => {
887
- makeCombTree(key, v);
888
- });
889
- } else {
890
- makeCombTree(key, value);
891
- }
892
- }
893
- cache.sc2tcCombTree = sc2tcCombTree;
846
+ if (custom && custom.length) {
847
+ for (let sc in custom) {
848
+ sc2tcComb[sc] = custom[sc];
849
+ }
850
+ }
851
+ function makeCombTree(key, value) {
852
+ let curTree = sc2tcCombTree;
853
+ for (let i = 0; i < key.length; i++) {
854
+ let curKey = key.charAt(i);
855
+ let branch = curTree[curKey];
856
+ let newTree = {};
857
+ if (i == key.length - 1) {
858
+ newTree = {"end": value};
859
+ if (branch) {
860
+ branch.end = value;
861
+ }
862
+ }
863
+ if (branch) {
864
+ curTree = branch;
865
+ } else {
866
+ curTree[curKey] = newTree;
867
+ curTree = newTree;
868
+ }
869
+ }
870
+ curTree = tc2scCombTree;
871
+ for (let i = 0; i < value.length; i++) {
872
+ let curKey = value.charAt(i);
873
+ let branch = curTree[curKey];
874
+ let newTree = {};
875
+ if (i == value.length - 1) {
876
+ newTree = {"end": key};
877
+ if (branch) {
878
+ branch.end = value;
879
+ }
880
+ }
881
+ if (branch) {
882
+ curTree = branch;
883
+ } else {
884
+ curTree[curKey] = newTree;
885
+ curTree = newTree;
886
+ }
887
+ }
888
+ }
889
+ for (let key in sc2tcComb) {
890
+ let value = sc2tcComb[key];
891
+ if (Array.isArray(value)) {
892
+ value.forEach(v => {
893
+ makeCombTree(key, v);
894
+ });
895
+ } else {
896
+ makeCombTree(key, value);
897
+ }
898
+ }
899
+ cache.sc2tcCombTree = sc2tcCombTree;
894
900
  cache.tc2scCombTree = tc2scCombTree;
895
- }
896
- if (cache.stDict && cache.tsDict) {
897
- stDict = cache.stDict;
901
+ }
902
+ if (cache.stDict && cache.tsDict) {
903
+ stDict = cache.stDict;
898
904
  tsDict = cache.tsDict;
899
- } else {
900
- for (let i = 0; i < scStr.length; i++) {
901
- let _sc = scStr[i];
902
- let _tc = tcStr[i];
903
- if (!stDict[_sc]) stDict[_sc] = _tc;
904
- if (!tsDict[_tc]) tsDict[_tc] = _sc;
905
- }
906
- Object.keys(oc2tc).forEach(key => {
907
- let ocList = oc2tc[key];
908
- for (let i = 0; i < ocList.length; i++) {
909
- let oc = ocList[i];
910
- stDict[oc] = key;
911
- tsDict[oc] = tsDict[key] || key;
912
- }
913
- })
914
- cache.stDict = stDict;
905
+ } else {
906
+ for (let i = 0; i < scStr.length; i++) {
907
+ let _sc = scStr[i];
908
+ let _tc = tcStr[i];
909
+ if (!stDict[_sc]) stDict[_sc] = _tc;
910
+ if (!tsDict[_tc]) tsDict[_tc] = _sc;
911
+ }
912
+ Object.keys(oc2tc).forEach(key => {
913
+ let ocList = oc2tc[key];
914
+ for (let i = 0; i < ocList.length; i++) {
915
+ let oc = ocList[i];
916
+ stDict[oc] = key;
917
+ tsDict[oc] = tsDict[key] || key;
918
+ }
919
+ })
920
+ cache.stDict = stDict;
915
921
  cache.tsDict = tsDict;
916
- }
917
- return {simplized, traditionalized, detect, cache};
922
+ }
923
+ return {simplized, traditionalized, detect, cache};
918
924
  }
919
925
 
920
926
  export default stcasc;