-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
357 lines (299 loc) · 8.87 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var http = require('http');
var https = require('https');
var url = require('url');
var fs = require('fs');
if (typeof __dirname === 'undefined') {
if (document) {
var scripts = document.getElementsByTagName('script');
var scriptPath = scripts[scripts.length - 1].src.split('?')[0];
var __dirname = scriptPath.split('/').slice(0, -1).join('/');
} else {
throw new Error('Cannot get current directory');
}
}
var IVS_REGEX = new RegExp('\uDB40[\uDD00-\uDDEF]');
var KANJI_REGEX = new RegExp('[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]');
var KANJI_SURROGATE_REGEX = new RegExp('[\uD840-\uD86E][\uDC00-\uDFFF]');
// http://youmightnotneedjquery.com/#deep_extend
var deepExtend = function(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i];
if (!obj)
continue;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object')
deepExtend(out[key], obj[key]);
else
out[key] = obj[key];
}
}
}
return out;
};
var map = function (value, mapObject) {
for (var key in mapObject) {
if (mapObject.hasOwnProperty(key) && value === key) {
return mapObject[key];
}
}
return null;
};
var parseKanji = function (string) {
if (string.length <= 2) {
return {
kanji: string,
ivs: ''
};
} else {
return {
kanji: string.slice(0, -2),
ivs: string.slice(-2)
};
}
}
function IVS() {
var options, callback;
for (var i = 0; i < arguments.length; i++) {
var argument = arguments[i];
if (typeof argument === 'function') {
callback = argument;
} else if (typeof argument === 'object') {
options = argument;
}
}
options = options || {};
callback = callback || function () {};
this.initialize(options, callback);
}
IVS.prototype.initialize = function (options, callback) {
var ivs = this;
if (ivs.IVD === undefined) {
var dataJSON = '';
function onJSONReady() {
try {
IVS.prototype.IVD = JSON.parse(dataJSON);
} catch (error) {
return callback(new Error('ivd.json is broken'));
}
callback.call(ivs, null);
}
if (typeof location !== 'undefined') { // in browser
var get = location.protocol === 'https:' ? https.get : http.get;
var dataURL = options.ivd ? url.resolve(location.href, options.ivd) : (__dirname + '/ivd.json')
var request = get(dataURL, function (response) {
if (response.statusCode !== 200) {
callback(new Error('Request to ' + dataURL + ' responded with status code ' + response.statusCode));
}
response.on('error', callback);
response.on('data', function (chunk) { dataJSON += chunk; });
response.on('close', onJSONReady);
});
} else { // in node
fs.readFile(__dirname + '/data/ivd.json', function (error, data) {
if (error) return callback(error);
dataJSON = data;
onJSONReady();
});
}
} else {
setTimeout(function () {
callback.call(ivs, null);
}, 0);
}
};
/**
* Execute function for each Kanji and IVS (if exists) in given string
* @param {string} string - The string in which this function seeks for Kanji
* @param {KanjiCallback} callback - The function called every time when Kanji was found
*/
IVS.prototype.forEachKanji = function (string, callback) {
var size = string.length;
var ret = '';
for (var index = 0; index < size; index++) {
var kanji = '';
var ivs = '';
var foundIndex = index;
if (string[index].match(KANJI_REGEX)) {
kanji = string[index];
} else if (index + 1 < size
&& string.substr(index, 2).match(KANJI_SURROGATE_REGEX)) {
kanji = string.substr(index, 2);
index++;
} else {
kanji = null;
ret += string[index];
}
if (kanji) {
// if string is still capable for having IVS
if (index + 2 < size
&& (string.substr(index + 1, 2).match(IVS_REGEX))) {
ivs = string.substr(index + 1, 2);
index += 2;
}
ret += callback(kanji, ivs, foundIndex);
}
}
return ret;
};
/**
* @callback KanjiCallback
* @param {string} kanji - Found Kanji string
* @param {string} ivs - The IVS allocated to found Kanji string. No length if not exists.
* @param {number} index - The index where the Kanji found in given string
* @returns {string} The string which the found kanji will be replaced by
*/
/**
* Unify IVSes in the given string to the given type
* @param {string} category - The IVS category to which the IVSes will be unified to. `'Aj'` and `'HD'` are supported.
* @param {string} string -
* @return {string} IVS-Unified string
*/
IVS.prototype.unify = function (category, string) {
var IVS = this;
var IVD = this.IVD;
return IVS.forEachKanji(string, function (kanji, ivs, index) {
if (!ivs) return kanji;
if (!IVD.IVD[kanji]) return kanji + ivs;
if (!IVD.IVD[kanji][ivs]) return kanji + ivs;
var info = IVD.IVD[kanji][ivs];
if (info[0] === category) return kanji + ivs;
var aliasName = info[1];
if (aliasName === '') return kanji + ivs;
var unified = null;
IVD.aliases[aliasName].forEach(function (alias) {
var parsed = parseKanji(alias);
if (parsed.kanji === kanji && parsed.ivs) {
if (IVD.IVD[parsed.kanji][parsed.ivs][0] === category) {
unified = alias;
}
}
});
if (unified) {
return unified;
} else {
return kanji + ivs;
}
});
};
/**
* Unify IVSes in given string to Adobe-Japan1. Same as IVS.unify('AJ', string).
* @param {string} string -
*/
IVS.prototype.AJ = function (string) {
return this.unify('AJ', string);
};
/**
* Unify IVSes in given string to Hanyo-Denshi. Same as IVS.unify('HD', string).
* @param {string} string -
*/
IVS.prototype.HD = function (string) {
return this.unify('HD', string);
};
/**
* Completely strip IVSes from given string.
* @param {string} string -
* @param {object} options - The options object
* @param {boolean} options.resolve - Some of the default glyphs in GlyphWiki is linked to IVDes of other code points. This option resolves mapping to other code points as conversion. Default is `false`.
* @return {string} - IVS-stripped string
*/
IVS.prototype.strip = function (string, options) {
var IVS = this;
var IVD = this.IVD;
var defaults = {
resolve: false,
};
if (typeof options === 'object') {
options = deepExtend(defaults, options);
} else {
options = defaults;
}
if (options.resolve) {
string = IVS.forEachKanji(string, function (kanji, ivs) {
if (!ivs) return kanji;
if (IVD.IVD[kanji] === undefined) return kanji;
if (IVD.IVD[kanji][ivs] === undefined) return kanji;
if (IVD.IVD[kanji][ivs][1] !== '') {
var aliasName = IVD.IVD[kanji][ivs][1];
var resolved = null;
IVD.aliases[aliasName].forEach(function (alias) {
var parsed = parseKanji(alias);
if (!parsed.ivs) {
if (!resolved || parsed.kanji === kanji) {
resolved = parsed.kanji;
}
}
});
if (resolved) {
return resolved;
} else {
return kanji;
}
} else {
return kanji;
}
});
}
return string.replace(new RegExp('\uDB40[\uDD00-\uDDEF]', 'g'), '');
};
/**
* Append IVSes for non-IVSed kanjies in given string using default glyphs in GlyphWiki.
* @param {string} string -
* @param {object} options - The options object
* @param {string} options.category - The IVS category used to append IVS which is the one of `'AJ'`, `'HD'`, `'AJonly'`, `'HDonly'`. Default is `'AJ'`.
* @param {boolean} options.force - This option forces to append U+E0100 if default glyph was not found in IVD. This doesn't affect kanjies which is not documented in IVD. Default is `true`.
* @param {boolean} options.resolve - Some of the default glyphs in GlyphWiki is linked to IVDes of other code points. This option resolves mapping to other code points as conversion. Default is `false`.
* @return {string} - IVSed string
*/
IVS.prototype.append = function (string, options) {
var IVS = this;
var defaults = {
category: 'AJ',
force: true,
resolve: false,
};
if (typeof options === 'object') {
options = deepExtend(defaults, options);
} else {
options = defaults;
}
var priorities = map(options.category, {
AJ: ['AJ', 'HD'],
HD: ['HD', 'AJ'],
AJonly: ['AJ'],
HDonly: ['HD'],
});
return IVS.forEachKanji(string, function (kanji, ivs, index) {
if (ivs) return kanji + ivs;
if (IVS.IVD.IVD[kanji] === undefined) return kanji;
if (IVS.IVD.IVD[kanji].std === '') {
if (options.force && IVS.IVD.IVD[kanji]['\uDB40\uDD00'] !== undefined) {
return kanji + '\uDB40\uDD00';
} else {
return kanji;
}
} else {
var aliasName = IVS.IVD.IVD[kanji].std;
var IVSed = null;
priorities.forEach(function (category) {
if (IVSed) return;
IVS.IVD.aliases[aliasName].forEach(function (alias) {
if (IVSed) return;
var parsed = parseKanji(alias);
if ((options.resolve || parsed.kanji === kanji) && parsed.ivs) {
if (IVS.IVD.IVD[parsed.kanji][parsed.ivs][0] === category) {
IVSed = parsed.kanji + parsed.ivs;
}
}
});
});
if (IVSed) {
return IVSed;
} else {
return kanji;
}
}
});
};
module.exports = IVS;