forked from chaklim/hkscs_unicode_converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (212 loc) · 5.55 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
const fs = require('fs');
const isHexadecimal = require('is-hexadecimal');
let unicodeMapping = {};
const files = [
{
name: 'gccs',
type: 'tsv',
header: [ 'Big5', 'Unicode', 'Big5Alternate', 'UnicodeName' ],
config: {
columnFromKeys: ['Big5Alternate'],
columnKeyTo: 'Unicode',
},
},
{
name: 'hkscs1999',
type: 'tsv',
header: [ 'Big5', 'Unicode', 'UnicodeAlternate', 'UnicodeName' ],
config: {
columnFromKeys: ['UnicodeAlternate'],
columnKeyTo: 'Unicode',
},
},
{
name: 'hkscs2001',
type: 'tsv',
header: [ 'Big5', 'Unicode', 'UnicodeAlternate', 'UnicodeName' ],
config: {
columnFromKeys: ['UnicodeAlternate'],
columnKeyTo: 'Unicode',
},
},
{
name: 'hkscs2001_2',
type: 'tsv',
header: [
'BIG-5',
'ISO/IEC_10646-1:1993',
'ISO/IEC_10646-1:2000',
'ISO/IEC_10646-2:2001',
],
config: {
columnFromKeys: ['ISO/IEC_10646-1:1993', 'ISO/IEC_10646-1:2000'],
columnKeyTo: 'ISO/IEC_10646-2:2001',
}
},
{
name: 'hkscs2004',
type: 'tsv',
header: [
'BIG-5',
'ISO/IEC_10646-1:1993',
'ISO/IEC_10646-1:2000',
'ISO/IEC_10646:2003_Amendment',
],
config: {
columnFromKeys: ['ISO/IEC_10646-1:1993', 'ISO/IEC_10646-1:2000'],
columnKeyTo: 'ISO/IEC_10646:2003_Amendment',
},
},
{
name: 'hkscs2008',
type: 'tsv',
header: [
'BIG-5',
'ISO/IEC_10646-1:1993',
'ISO/IEC_10646-1:2000',
'ISO/IEC_10646:2003_Amendment',
],
config: {
columnFromKeys: ['ISO/IEC_10646-1:1993', 'ISO/IEC_10646-1:2000'],
columnKeyTo: 'ISO/IEC_10646:2003_Amendment',
},
},
{
name: 'hkscs2016',
type: 'json',
config: {
columnFromKeys: ['codepoint'],
columnKeyTo: 'char',
},
},
];
const tsvFile2json = (filePath) => {
const content = fs.readFileSync(filePath, 'utf8');
const rows = content.split('\n');
const header = rows[0].split('\t');
const contentRows = rows.slice(1);
let json = [];
contentRows.forEach(row => {
const columns = row.split('\t');
let data = {};
columns.forEach((column, index) => {
data[header[index]] = column;
});
json.push(data);
});
return json;
}
const jsonFile2json = (filePath) => {
const content = fs.readFileSync(filePath, 'utf8');
const json = JSON.parse(content);
return json;
}
const getFilePath = (fromFile) => {
return `${__dirname}/hkscs/${fromFile}`;
};
files.forEach(file => {
const filePath = getFilePath(`${file.name}.${file.type}`);
if (filePath.endsWith('.tsv')) {
unicodeMapping[file.name] = {
array: tsvFile2json(filePath)
};
}
if (filePath.endsWith('.json')) {
unicodeMapping[file.name] = {
array: jsonFile2json(filePath)
};
}
});
const formattedKeyValuePairFrom = (key, value) => {
if (
typeof key !== 'string' || key.length <= 0 ||
typeof value !== 'string' || value.length <= 0 ||
key === value
) {
return {};
}
const isRemoveFirstTwoCharacters = key.startsWith('U+');
const newKey = isRemoveFirstTwoCharacters ? key.substr(2) : key;
const newValue = isRemoveFirstTwoCharacters ? value.substr(2) : value;
// handle Ê̄, Ê̌, ê̄, ê̌ (<00CA,0304>, <00CA,030C>, <00EA,0304>, <00EA,030C>)
if (newValue.startsWith('<') && newValue.endsWith('>')) {
const parsedValue = String.fromCodePoint.apply(
null,
value.slice(1, -1).split(',').map(stringValue => parseInt(stringValue, 16))
);
return {
key: newKey,
value: parsedValue
};
}
return {
key: newKey,
value: newValue,
}
}
const mappingFrom = ({
unicodeMappingArray,
columnFromKeys,
columnKeyTo,
}) => {
const mapping = {};
unicodeMappingArray.forEach(data => {
columnFromKeys.forEach(columnKeyFrom => {
const { key, value } = formattedKeyValuePairFrom(data[columnKeyFrom], data[columnKeyTo]);
if (!!key && !!value) {
mapping[key] = value;
}
});
});
return mapping;
};
// create mapping
files.forEach(file => {
const unicodeMappingArray = unicodeMapping[file.name].array;
const { columnFromKeys, columnKeyTo } = file.config;
unicodeMapping[file.name].map = mappingFrom({
unicodeMappingArray,
columnFromKeys,
columnKeyTo,
});
});
// - unicode to char
// 0. gccs Big5Alternate to Unicode
// 1. hkscs1999 UnicodeAlternate to Unicode
// 2. hkscs2001 UnicodeAlternate to Unicode
// 3. hkscs2004 'ISO/IEC_10646-1:1993'/'ISO/IEC_10646-1:2000' to 'ISO/IEC_10646:2003_Amendment'
// 4. hkscs2008 'ISO/IEC_10646-1:1993'/'ISO/IEC_10646-1:2000' to 'ISO/IEC_10646:2003_Amendment'
// 5. hkscs2016 codepoint to char
const mappings = Object.keys(unicodeMapping).map(key => unicodeMapping[key].map);
// char is a string character
const convertCharacter = (char) => {
if (typeof char !== 'string') {
return char;
}
const charLength = Array.from(char).length;
if (charLength <= 0 || charLength > 1) {
return char;
}
let charKey = char.codePointAt(0).toString(16).toUpperCase();
let newChar = mappings.reduce((value, mapping) => {
return mapping[value] || value;
}, charKey);
if (typeof newChar === 'string' && Array.from(newChar).length === 1) {
return newChar;
}
if (isHexadecimal(newChar)) {
return String.fromCodePoint(parseInt(newChar, 16));
}
return newChar;
};
const convertString = (str) => {
if (typeof str !== 'string') {
return str;
}
return Array.from(str).map(char => convertCharacter(char)).join('')
};
module.exports = Object.freeze({
convertCharacter,
convertString
});