-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
80 lines (70 loc) · 2.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export type TextData = {
text: string
replaced: ReplaceHistory[]
}
type ReplaceHistory = {
begin: number
end: number
}
export type Dict = {
match: RegExp
replace: string
}[]
export const replace = (origData: TextData, dict: Dict): TextData => {
const data: TextData = { ...origData }
dict.forEach(word => {
const match = data.text.match(word.match) || []
const extra = data.text.split(word.match)
let cursor = extra[0].length
let processed = false
const replaced = match.map((str, i) => {
const cursorEnd = cursor + str.length - 1
// 以前に置換した範囲と被ってるかどうか
if (
data.replaced.some(
(hist: ReplaceHistory) =>
(hist.begin <= cursor && cursor <= hist.end) ||
(hist.begin <= cursorEnd && cursorEnd <= hist.end)
)
) {
cursor += str.length + extra[i + 1].length
return str
} else {
// 変換前後で文字列長が等しいかどうか
if (str.length === word.replace.length) {
data.replaced.push({ begin: cursor, end: cursorEnd })
} else {
// ずらす
const diff = word.replace.length - str.length
data.replaced = data.replaced.map((hist: ReplaceHistory) =>
cursor < hist.begin
? { begin: hist.begin + diff, end: hist.end + diff }
: hist
)
data.replaced.push({ begin: cursor, end: cursorEnd + diff })
}
cursor += word.replace.length + extra[i + 1].length
processed = true
return word.replace
}
})
if (processed) {
const interleave = <T>([x, ...xs]: T[], ys: T[] = []): T[] =>
x === undefined ? ys : [x, ...interleave(ys, xs)]
data.text = interleave(extra, replaced).join("")
}
})
return data
}
export const json2Dict = (obj: { [key: string]: string }): Dict => {
const dict: Dict = []
for (const key in obj) {
try {
dict.push({
match: new RegExp(key, "g"),
replace: obj[key],
})
} catch {}
}
return dict
}