-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strtable.ts
91 lines (72 loc) · 1.41 KB
/
strtable.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
81
82
83
84
85
86
87
88
89
90
91
/**
* Created by user on 2018/7/29/029.
*/
import { table_tw2cn, table_cn2tw } from '../zh/convert/index';
import UString from 'uni-string';
export interface IOptions
{
coreJs?: boolean,
ignore?: boolean,
}
export function toStrTableArray(table: {
[k: string]: string,
}, options: IOptions = {})
{
if (typeof table !== 'object' || Array.isArray(table))
{
throw new TypeError(`table '${typeof table}' ${table}`);
}
let from = [];
let to = [];
const ks = Object.keys(table);
ks.sort();
let split = options.coreJs ? jsSplit : utf8Split;
for (let k of ks)
{
let k2 = table[k];
let s1 = split(k);
let s2 = split(k2);
if (s1.length !== 1 || s2.length !== 1)
{
let msg = `'${k}' s1: ${s1.length} ${s1} ; s2: ${s2.length} ${s2}`;
if (options.ignore)
{
console.error(msg);
continue;
}
else
{
//console.dir(s1);
//console.dir(s2);
throw new TypeError(msg);
break;
}
}
from.push(k);
to.push(k2);
}
return {
from,
to,
}
}
export function toStrTable(table: {
[k: string]: string,
}, options?: IOptions)
{
let { from, to, } = toStrTableArray(table, options);
return {
from: from.join(''),
to: to.join(''),
}
}
export function jsSplit(s: string)
{
return s.split('');
}
export function utf8Split(s: string)
{
return UString.split(s, '');
}
export default exports as typeof import('./strtable');
//console.log(__filename, require.extensions);