-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathucd-0.2.0.js
141 lines (117 loc) · 4.06 KB
/
ucd-0.2.0.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
/**
* @license Released under the MIT license.
*
* <pre>
* Copyright (c) 2013 ynakajima <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* </pre>
*
* @fileoverview UCD offers a series of functions that provide a simple
* interface to the Unicode Character Database.
* @author [email protected] (ynakajima)
*/
(function(global) {
// require
var UnicodeData = (typeof require !== 'undefined') ?
require('../data/unicodedata.js') :
global.UnicodeData;
/**
* Unicode Charactor Database
* @type {Object}
*/
var UCD = {};
/**
* Get Unicode Name
* @param {string} character single string.
* @return {string} Unicode Name.
*/
UCD.getName = function(character) {
// 文字コードの取得
var charCode = character.charCodeAt(0);
// charNameListが初期化されていない場合は初期化
if (typeof UCD.characterNameList === 'undefined') {
UCD.readUnicodeData(UnicodeData);
}
// Surrogate Pair
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
var heigh = charCode;
var low = character.charCodeAt(1);
charCode = ((heigh - 0xD800) << 10) + (low - 0xDC00) + 0x10000;
}
// return Unicode Name
var unicodeName = UCD.characterNameList[charCode];
return (typeof unicodeName !== 'undefined') ? unicodeName : 'undefined';
};
/**
* Get Unicode Version
*/
UCD.getUnicodeVersion = function() {
// charNameListが初期化されていない場合は初期化
if (typeof UCD.characterNameList === 'undefined') {
UCD.readUnicodeData(UnicodeData);
}
return UCD.unicodeVersion;
};
/**
* Read UnicodeData
*/
UCD.readUnicodeData = function(unicodeData) {
UCD.unicodeVersion = unicodeData.unicodeVersion;
var characterNameList = [];
// UnicodeNameを読み込んで展開
for (var i = 0, l = unicodeData.characterNameList.length; i < l; i++) {
// 行を取得
var line = unicodeData.characterNameList[i];
// セミコロンで分割
var charData = line.split(';');
// データを切り出して charNameList に格納
if (charData.length >= 2) {
var charCode = parseInt(charData[0], 16);
var charName = charData[1];
// 連続データの場合は連続して処理
if (charName.indexOf(', First>') !== -1) {
var lastCharCode = parseInt(
unicodeData.characterNameList[++i].split(';')[0],
16
);
charName = charName.replace(/, First>$/, '').replace(/^</, '');
for (; charCode <= lastCharCode; charCode++) {
var hexCharCode = charCode.toString(16).toUpperCase();
characterNameList[charCode] = charName + '-' + hexCharCode;
}
} else {
characterNameList[charCode] = charName;
}
}
}
/**
* @type {Array.<string>}
* @private
*/
UCD.characterNameList = characterNameList;
};
// export
if (typeof module !== 'undefined') {
module.exports = UCD;
} else {
global.UCD = UCD;
}
})(this);