UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TableDict = void 0;
4const segment_1 = require("segment-dict/lib/loader/segment");
5const cjk_1 = require("../util/cjk");
6const core_1 = require("./core");
7const util_1 = require("../util");
8/**
9 * @todo 掛接其他 dict
10 */
11class TableDict extends core_1.default {
12 constructor() {
13 super(...arguments);
14 this.TABLE = {};
15 this.TABLE2 = {};
16 }
17 exists(data) {
18 let w, p, f;
19 if (typeof data == 'string') {
20 w = data;
21 }
22 else if (Array.isArray(data)) {
23 [w, p, f] = data;
24 }
25 else {
26 ({ w, p, f } = data);
27 }
28 return this.TABLE[w] || null;
29 }
30 __handleInput(data) {
31 let w, p, f;
32 let plus;
33 if (typeof data == 'string') {
34 w = data;
35 }
36 else if (Array.isArray(data)) {
37 [w, p, f, ...plus] = data;
38 }
39 else {
40 ({ w, p, f } = data);
41 }
42 if (typeof w !== 'string' || w === '') {
43 throw new TypeError(JSON.stringify(data));
44 }
45 p = (typeof p != 'number' || Number.isNaN(p)) ? 0 : p;
46 f = (typeof f != 'number' || Number.isNaN(f)) ? 0 : f;
47 return {
48 data: {
49 w, p, f,
50 },
51 plus,
52 };
53 }
54 add(data, skipExists) {
55 let w, p, f;
56 let plus;
57 {
58 let ret = this.__handleInput(data);
59 ({ w, p, f } = ret.data);
60 plus = ret.plus;
61 }
62 if (skipExists && this.exists(w)) {
63 return this;
64 }
65 if (plus && plus.length) {
66 // @todo do something
67 }
68 this._add({ w, p, f, s: true });
69 let self = this;
70 /**
71 * @todo 需要更聰明的作法 目前的做法實在太蠢
72 * @BUG 在不明原因下 似乎不會正確的添加每個項目 如果遇到這種情形請手動添加簡繁項目
73 */
74 if (1 && this.options.autoCjk) {
75 let wa = cjk_1.text_list(w);
76 wa.forEach(function (w2) {
77 if (w2 != w && !self.exists(w2)) {
78 self._add({ w: w2, p, f });
79 }
80 });
81 /*
82 let w2: string;
83 w2 = CjkConv.zh2jp(w);
84
85 if (w2 != w && !this.exists(w2))
86 {
87 this._add({w: w2, p, f});
88 //console.log(w2);
89 }
90
91 w2 = CjkConv.cjk2zht(w);
92
93 if (w2 !== w && !this.exists(w2))
94 {
95 this._add({w: w2, p, f});
96 //console.log(w2);
97 }
98
99 w2 = CjkConv.cjk2zhs(w);
100
101 if (w2 !== w && !this.exists(w2))
102 {
103 this._add({w: w2, p, f});
104 //console.log(w2);
105 }
106 */
107 }
108 return this;
109 }
110 _add({ w, p, f, s }) {
111 let len = w.length;
112 this.TABLE[w] = {
113 p,
114 f,
115 s,
116 };
117 if (!this.TABLE2[len])
118 this.TABLE2[len] = {};
119 this.TABLE2[len][w] = this.TABLE[w];
120 }
121 remove(target) {
122 let { data, plus } = this.__handleInput(target);
123 this._remove(data);
124 return this;
125 }
126 _remove({ w, p, f, s }) {
127 let len = w.length;
128 delete this.TABLE[w];
129 if (this.TABLE2[len]) {
130 delete this.TABLE2[len][w];
131 }
132 return this;
133 }
134 json() {
135 return util_1.cloneDeep(this.TABLE);
136 }
137 /**
138 * 將目前的 表格 匯出
139 */
140 stringify(LF = "\n") {
141 let self = this;
142 return Object.entries(self.TABLE)
143 .reduce(function (a, [w, { p, f }]) {
144 let line = segment_1.stringifyLine([w, p, f]);
145 a.push(line);
146 return a;
147 }, [])
148 .join(typeof LF === 'string' ? LF : "\n");
149 }
150}
151exports.TableDict = TableDict;
152exports.default = TableDict;
153//# sourceMappingURL=dict.js.map
\No newline at end of file