dsh-cait 1.0.3 → 1.0.4

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.
package/lib/glossary.js CHANGED
@@ -1,178 +1,178 @@
1
- import { CatError } from './errors.js'
2
- import { DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, MAX_SEGMENTS, canonicalLanguage, cleanLang, cleanText, escapeRegExp, makeId, now, parseGlossaryCsv } from './store-utils.js'
3
- import { normalizeText } from './similarity.js'
4
-
5
- export const glossaryMethods = {
6
- findGlossaryHits(text, sourceLang) {
7
- const haystack = String(text ?? '')
8
- if (!haystack) return []
9
- const lower = haystack.toLowerCase()
10
- const lang = sourceLang ? canonicalLanguage(sourceLang) : ''
11
- const hits = []
12
- const mountedGlossary = new Set(this.mounts.glossaryIds)
13
-
14
- for (const entry of this.glossary) {
15
- if (!mountedGlossary.has(entry.glossaryId || DEFAULT_GLOSSARY_ID)) continue
16
- if (entry.sourceLang && lang && entry.sourceLang !== lang) continue
17
- const needle = entry.source
18
- if (!needle) continue
19
- const index = entry.caseSensitive ? haystack.indexOf(needle) : lower.indexOf(needle.toLowerCase())
20
- if (index < 0) continue
21
-
22
- let count = 0
23
- const pattern = new RegExp(escapeRegExp(needle), entry.caseSensitive ? 'g' : 'gi')
24
- while (pattern.exec(haystack) !== null && count < 99) count += 1
25
-
26
- hits.push({
27
- id: entry.id,
28
- glossaryId: entry.glossaryId || DEFAULT_GLOSSARY_ID,
29
- term: entry.source,
30
- target: entry.target,
31
- note: entry.note || '',
32
- count,
33
- })
34
- }
35
-
36
- return hits.sort((a, b) => b.term.length - a.term.length)
37
- }
38
- ,
39
- addGlossary({ source, target, note, sourceLang, caseSensitive, glossaryId } = {}) {
40
- const term = cleanText(source, '术语')
41
- const translation = cleanText(target, '术语译文')
42
- if (!term || !translation) throw new CatError('EMPTY_TERM', '术语和译文都不能为空。')
43
- const lang = sourceLang ? cleanLang(sourceLang, '') : ''
44
- const libraryId = this.requireGlossaryLibrary(glossaryId)
45
- const existing = this.glossary.find(
46
- (entry) =>
47
- (entry.glossaryId || DEFAULT_GLOSSARY_ID) === libraryId &&
48
- normalizeText(entry.source) === normalizeText(term) &&
49
- entry.sourceLang === lang,
50
- )
51
- if (existing) {
52
- existing.source = term
53
- existing.target = translation
54
- existing.note = String(note ?? '').trim()
55
- existing.caseSensitive = Boolean(caseSensitive)
56
- existing.updatedAt = now()
57
- this.touch()
58
- this.persist()
59
- return { entry: { ...existing }, created: false }
60
- }
61
- const entry = {
62
- id: makeId('gl'),
63
- glossaryId: libraryId,
64
- source: term,
65
- target: translation,
66
- note: String(note ?? '').trim(),
67
- sourceLang: lang,
68
- caseSensitive: Boolean(caseSensitive),
69
- createdAt: now(),
70
- updatedAt: now(),
71
- }
72
- this.glossary.push(entry)
73
- this.touch()
74
- this.persist()
75
- return { entry: { ...entry }, created: true }
76
- }
77
- ,
78
- updateGlossary({ id, source, target, note, caseSensitive } = {}) {
79
- const entry = this.glossary.find((item) => item.id === id)
80
- if (!entry) throw new CatError('TERM_NOT_FOUND', '术语不存在。', 404)
81
- this.requireMountedGlossaryLibrary(entry.glossaryId || DEFAULT_GLOSSARY_ID)
82
- if (source !== undefined) {
83
- const value = cleanText(source, '术语')
84
- if (!value) throw new CatError('EMPTY_TERM', '术语不能为空。')
85
- entry.source = value
86
- }
87
- if (target !== undefined) {
88
- const value = cleanText(target, '术语译文')
89
- if (!value) throw new CatError('EMPTY_TERM', '术语译文不能为空。')
90
- entry.target = value
91
- }
92
- if (note !== undefined) entry.note = String(note ?? '').trim()
93
- if (caseSensitive !== undefined) entry.caseSensitive = Boolean(caseSensitive)
94
- entry.updatedAt = now()
95
- this.touch()
96
- this.persist()
97
- return { entry: { ...entry } }
98
- }
99
- ,
100
- deleteGlossary({ id } = {}) {
101
- const index = this.glossary.findIndex((item) => item.id === id)
102
- if (index < 0) throw new CatError('TERM_NOT_FOUND', '术语不存在。', 404)
103
- this.requireMountedGlossaryLibrary(
104
- this.glossary[index].glossaryId || DEFAULT_GLOSSARY_ID,
105
- )
106
- const [removed] = this.glossary.splice(index, 1)
107
- this.touch()
108
- this.persist()
109
- return { removed: removed.id }
110
- }
111
- ,
112
- importGlossary({ text, entries, glossaryId } = {}) {
113
- const list = Array.isArray(entries) ? entries : parseGlossaryCsv(text)
114
- if (!list.length) throw new CatError('EMPTY_GLOSSARY_IMPORT', '没有解析到有效术语。')
115
- if (list.length > MAX_SEGMENTS) throw new CatError('TOO_MANY_TERMS', `一次最多导入 ${MAX_SEGMENTS} 条术语。`)
116
- const libraryId = this.requireGlossaryLibrary(glossaryId)
117
-
118
- let added = 0
119
- let updated = 0
120
- for (const item of list) {
121
- if (!item || !String(item.source ?? '').trim() || !String(item.target ?? '').trim()) continue
122
- const lang = item.sourceLang ? cleanLang(item.sourceLang, '') : ''
123
- const existing = this.glossary.find(
124
- (entry) =>
125
- (entry.glossaryId || DEFAULT_GLOSSARY_ID) === libraryId &&
126
- normalizeText(entry.source) === normalizeText(item.source) &&
127
- entry.sourceLang === lang,
128
- )
129
- if (existing) {
130
- existing.target = String(item.target).trim()
131
- existing.note = String(item.note ?? '').trim()
132
- existing.updatedAt = now()
133
- updated += 1
134
- } else {
135
- this.glossary.push({
136
- id: makeId('gl'),
137
- glossaryId: libraryId,
138
- source: String(item.source).trim(),
139
- target: String(item.target).trim(),
140
- note: String(item.note ?? '').trim(),
141
- sourceLang: lang,
142
- caseSensitive: Boolean(item.caseSensitive),
143
- createdAt: now(),
144
- updatedAt: now(),
145
- })
146
- added += 1
147
- }
148
- }
149
- if (added || updated) this.touch()
150
- this.persist()
151
- return { added, updated, total: this.glossary.length }
152
- }
153
- ,
154
- clearGlossary({ glossaryIds, all = false } = {}) {
155
- let targets
156
- let scope
157
- if (all === true) {
158
- targets = new Set(this.glossaryLibraries.map((library) => library.glossaryId))
159
- scope = 'all'
160
- } else if (Array.isArray(glossaryIds) && glossaryIds.length) {
161
- targets = new Set(glossaryIds.map((id) => this.requireMountedGlossaryLibrary(id)))
162
- scope = 'explicit'
163
- } else {
164
- targets = new Set(this.mounts.glossaryIds)
165
- scope = 'mounted'
166
- }
167
-
168
- const before = this.glossary.length
169
- this.glossary = this.glossary.filter(
170
- (entry) => !targets.has(entry.glossaryId || DEFAULT_GLOSSARY_ID),
171
- )
172
- const removed = before - this.glossary.length
173
- this.touch()
174
- this.persist()
175
- return { removed, scope, glossaryIds: [...targets] }
176
- }
177
-
178
- }
1
+ import { CatError } from './errors.js'
2
+ import { DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, MAX_SEGMENTS, canonicalLanguage, cleanLang, cleanText, escapeRegExp, makeId, now, parseGlossaryCsv } from './store-utils.js'
3
+ import { normalizeText } from './similarity.js'
4
+
5
+ export const glossaryMethods = {
6
+ findGlossaryHits(text, sourceLang) {
7
+ const haystack = String(text ?? '')
8
+ if (!haystack) return []
9
+ const lower = haystack.toLowerCase()
10
+ const lang = sourceLang ? canonicalLanguage(sourceLang) : ''
11
+ const hits = []
12
+ const mountedGlossary = new Set(this.mounts.glossaryIds)
13
+
14
+ for (const entry of this.glossary) {
15
+ if (!mountedGlossary.has(entry.glossaryId || DEFAULT_GLOSSARY_ID)) continue
16
+ if (entry.sourceLang && lang && entry.sourceLang !== lang) continue
17
+ const needle = entry.source
18
+ if (!needle) continue
19
+ const index = entry.caseSensitive ? haystack.indexOf(needle) : lower.indexOf(needle.toLowerCase())
20
+ if (index < 0) continue
21
+
22
+ let count = 0
23
+ const pattern = new RegExp(escapeRegExp(needle), entry.caseSensitive ? 'g' : 'gi')
24
+ while (pattern.exec(haystack) !== null && count < 99) count += 1
25
+
26
+ hits.push({
27
+ id: entry.id,
28
+ glossaryId: entry.glossaryId || DEFAULT_GLOSSARY_ID,
29
+ term: entry.source,
30
+ target: entry.target,
31
+ note: entry.note || '',
32
+ count,
33
+ })
34
+ }
35
+
36
+ return hits.sort((a, b) => b.term.length - a.term.length)
37
+ }
38
+ ,
39
+ addGlossary({ source, target, note, sourceLang, caseSensitive, glossaryId } = {}) {
40
+ const term = cleanText(source, '术语')
41
+ const translation = cleanText(target, '术语译文')
42
+ if (!term || !translation) throw new CatError('EMPTY_TERM', '术语和译文都不能为空。')
43
+ const lang = sourceLang ? cleanLang(sourceLang, '') : ''
44
+ const libraryId = this.requireGlossaryLibrary(glossaryId)
45
+ const existing = this.glossary.find(
46
+ (entry) =>
47
+ (entry.glossaryId || DEFAULT_GLOSSARY_ID) === libraryId &&
48
+ normalizeText(entry.source) === normalizeText(term) &&
49
+ entry.sourceLang === lang,
50
+ )
51
+ if (existing) {
52
+ existing.source = term
53
+ existing.target = translation
54
+ existing.note = String(note ?? '').trim()
55
+ existing.caseSensitive = Boolean(caseSensitive)
56
+ existing.updatedAt = now()
57
+ this.touch()
58
+ this.persist()
59
+ return { entry: { ...existing }, created: false }
60
+ }
61
+ const entry = {
62
+ id: makeId('gl'),
63
+ glossaryId: libraryId,
64
+ source: term,
65
+ target: translation,
66
+ note: String(note ?? '').trim(),
67
+ sourceLang: lang,
68
+ caseSensitive: Boolean(caseSensitive),
69
+ createdAt: now(),
70
+ updatedAt: now(),
71
+ }
72
+ this.glossary.push(entry)
73
+ this.touch()
74
+ this.persist()
75
+ return { entry: { ...entry }, created: true }
76
+ }
77
+ ,
78
+ updateGlossary({ id, source, target, note, caseSensitive } = {}) {
79
+ const entry = this.glossary.find((item) => item.id === id)
80
+ if (!entry) throw new CatError('TERM_NOT_FOUND', '术语不存在。', 404)
81
+ this.requireMountedGlossaryLibrary(entry.glossaryId || DEFAULT_GLOSSARY_ID)
82
+ if (source !== undefined) {
83
+ const value = cleanText(source, '术语')
84
+ if (!value) throw new CatError('EMPTY_TERM', '术语不能为空。')
85
+ entry.source = value
86
+ }
87
+ if (target !== undefined) {
88
+ const value = cleanText(target, '术语译文')
89
+ if (!value) throw new CatError('EMPTY_TERM', '术语译文不能为空。')
90
+ entry.target = value
91
+ }
92
+ if (note !== undefined) entry.note = String(note ?? '').trim()
93
+ if (caseSensitive !== undefined) entry.caseSensitive = Boolean(caseSensitive)
94
+ entry.updatedAt = now()
95
+ this.touch()
96
+ this.persist()
97
+ return { entry: { ...entry } }
98
+ }
99
+ ,
100
+ deleteGlossary({ id } = {}) {
101
+ const index = this.glossary.findIndex((item) => item.id === id)
102
+ if (index < 0) throw new CatError('TERM_NOT_FOUND', '术语不存在。', 404)
103
+ this.requireMountedGlossaryLibrary(
104
+ this.glossary[index].glossaryId || DEFAULT_GLOSSARY_ID,
105
+ )
106
+ const [removed] = this.glossary.splice(index, 1)
107
+ this.touch()
108
+ this.persist()
109
+ return { removed: removed.id }
110
+ }
111
+ ,
112
+ importGlossary({ text, entries, glossaryId } = {}) {
113
+ const list = Array.isArray(entries) ? entries : parseGlossaryCsv(text)
114
+ if (!list.length) throw new CatError('EMPTY_GLOSSARY_IMPORT', '没有解析到有效术语。')
115
+ if (list.length > MAX_SEGMENTS) throw new CatError('TOO_MANY_TERMS', `一次最多导入 ${MAX_SEGMENTS} 条术语。`)
116
+ const libraryId = this.requireGlossaryLibrary(glossaryId)
117
+
118
+ let added = 0
119
+ let updated = 0
120
+ for (const item of list) {
121
+ if (!item || !String(item.source ?? '').trim() || !String(item.target ?? '').trim()) continue
122
+ const lang = item.sourceLang ? cleanLang(item.sourceLang, '') : ''
123
+ const existing = this.glossary.find(
124
+ (entry) =>
125
+ (entry.glossaryId || DEFAULT_GLOSSARY_ID) === libraryId &&
126
+ normalizeText(entry.source) === normalizeText(item.source) &&
127
+ entry.sourceLang === lang,
128
+ )
129
+ if (existing) {
130
+ existing.target = String(item.target).trim()
131
+ existing.note = String(item.note ?? '').trim()
132
+ existing.updatedAt = now()
133
+ updated += 1
134
+ } else {
135
+ this.glossary.push({
136
+ id: makeId('gl'),
137
+ glossaryId: libraryId,
138
+ source: String(item.source).trim(),
139
+ target: String(item.target).trim(),
140
+ note: String(item.note ?? '').trim(),
141
+ sourceLang: lang,
142
+ caseSensitive: Boolean(item.caseSensitive),
143
+ createdAt: now(),
144
+ updatedAt: now(),
145
+ })
146
+ added += 1
147
+ }
148
+ }
149
+ if (added || updated) this.touch()
150
+ this.persist()
151
+ return { added, updated, total: this.glossary.length }
152
+ }
153
+ ,
154
+ clearGlossary({ glossaryIds, all = false } = {}) {
155
+ let targets
156
+ let scope
157
+ if (all === true) {
158
+ targets = new Set(this.glossaryLibraries.map((library) => library.glossaryId))
159
+ scope = 'all'
160
+ } else if (Array.isArray(glossaryIds) && glossaryIds.length) {
161
+ targets = new Set(glossaryIds.map((id) => this.requireMountedGlossaryLibrary(id)))
162
+ scope = 'explicit'
163
+ } else {
164
+ targets = new Set(this.mounts.glossaryIds)
165
+ scope = 'mounted'
166
+ }
167
+
168
+ const before = this.glossary.length
169
+ this.glossary = this.glossary.filter(
170
+ (entry) => !targets.has(entry.glossaryId || DEFAULT_GLOSSARY_ID),
171
+ )
172
+ const removed = before - this.glossary.length
173
+ this.touch()
174
+ this.persist()
175
+ return { removed, scope, glossaryIds: [...targets] }
176
+ }
177
+
178
+ }