parser-unicode-properties.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. 'use strict';
  2. /**
  3. * The MIT License (MIT)
  4. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  5. */
  6. var NON_BINARY_PROP_NAMES_TO_ALIASES = {
  7. General_Category: 'gc',
  8. Script: 'sc',
  9. Script_Extensions: 'scx'
  10. };
  11. var NON_BINARY_ALIASES_TO_PROP_NAMES = inverseMap(NON_BINARY_PROP_NAMES_TO_ALIASES);
  12. var BINARY_PROP_NAMES_TO_ALIASES = {
  13. ASCII: 'ASCII',
  14. ASCII_Hex_Digit: 'AHex',
  15. Alphabetic: 'Alpha',
  16. Any: 'Any',
  17. Assigned: 'Assigned',
  18. Bidi_Control: 'Bidi_C',
  19. Bidi_Mirrored: 'Bidi_M',
  20. Case_Ignorable: 'CI',
  21. Cased: 'Cased',
  22. Changes_When_Casefolded: 'CWCF',
  23. Changes_When_Casemapped: 'CWCM',
  24. Changes_When_Lowercased: 'CWL',
  25. Changes_When_NFKC_Casefolded: 'CWKCF',
  26. Changes_When_Titlecased: 'CWT',
  27. Changes_When_Uppercased: 'CWU',
  28. Dash: 'Dash',
  29. Default_Ignorable_Code_Point: 'DI',
  30. Deprecated: 'Dep',
  31. Diacritic: 'Dia',
  32. Emoji: 'Emoji',
  33. Emoji_Component: 'Emoji_Component',
  34. Emoji_Modifier: 'Emoji_Modifier',
  35. Emoji_Modifier_Base: 'Emoji_Modifier_Base',
  36. Emoji_Presentation: 'Emoji_Presentation',
  37. Extended_Pictographic: 'Extended_Pictographic',
  38. Extender: 'Ext',
  39. Grapheme_Base: 'Gr_Base',
  40. Grapheme_Extend: 'Gr_Ext',
  41. Hex_Digit: 'Hex',
  42. IDS_Binary_Operator: 'IDSB',
  43. IDS_Trinary_Operator: 'IDST',
  44. ID_Continue: 'IDC',
  45. ID_Start: 'IDS',
  46. Ideographic: 'Ideo',
  47. Join_Control: 'Join_C',
  48. Logical_Order_Exception: 'LOE',
  49. Lowercase: 'Lower',
  50. Math: 'Math',
  51. Noncharacter_Code_Point: 'NChar',
  52. Pattern_Syntax: 'Pat_Syn',
  53. Pattern_White_Space: 'Pat_WS',
  54. Quotation_Mark: 'QMark',
  55. Radical: 'Radical',
  56. Regional_Indicator: 'RI',
  57. Sentence_Terminal: 'STerm',
  58. Soft_Dotted: 'SD',
  59. Terminal_Punctuation: 'Term',
  60. Unified_Ideograph: 'UIdeo',
  61. Uppercase: 'Upper',
  62. Variation_Selector: 'VS',
  63. White_Space: 'space',
  64. XID_Continue: 'XIDC',
  65. XID_Start: 'XIDS'
  66. };
  67. var BINARY_ALIASES_TO_PROP_NAMES = inverseMap(BINARY_PROP_NAMES_TO_ALIASES);
  68. var GENERAL_CATEGORY_VALUE_TO_ALIASES = {
  69. Cased_Letter: 'LC',
  70. Close_Punctuation: 'Pe',
  71. Connector_Punctuation: 'Pc',
  72. Control: ['Cc', 'cntrl'],
  73. Currency_Symbol: 'Sc',
  74. Dash_Punctuation: 'Pd',
  75. Decimal_Number: ['Nd', 'digit'],
  76. Enclosing_Mark: 'Me',
  77. Final_Punctuation: 'Pf',
  78. Format: 'Cf',
  79. Initial_Punctuation: 'Pi',
  80. Letter: 'L',
  81. Letter_Number: 'Nl',
  82. Line_Separator: 'Zl',
  83. Lowercase_Letter: 'Ll',
  84. Mark: ['M', 'Combining_Mark'],
  85. Math_Symbol: 'Sm',
  86. Modifier_Letter: 'Lm',
  87. Modifier_Symbol: 'Sk',
  88. Nonspacing_Mark: 'Mn',
  89. Number: 'N',
  90. Open_Punctuation: 'Ps',
  91. Other: 'C',
  92. Other_Letter: 'Lo',
  93. Other_Number: 'No',
  94. Other_Punctuation: 'Po',
  95. Other_Symbol: 'So',
  96. Paragraph_Separator: 'Zp',
  97. Private_Use: 'Co',
  98. Punctuation: ['P', 'punct'],
  99. Separator: 'Z',
  100. Space_Separator: 'Zs',
  101. Spacing_Mark: 'Mc',
  102. Surrogate: 'Cs',
  103. Symbol: 'S',
  104. Titlecase_Letter: 'Lt',
  105. Unassigned: 'Cn',
  106. Uppercase_Letter: 'Lu'
  107. };
  108. var GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES = inverseMap(GENERAL_CATEGORY_VALUE_TO_ALIASES);
  109. var SCRIPT_VALUE_TO_ALIASES = {
  110. Adlam: 'Adlm',
  111. Ahom: 'Ahom',
  112. Anatolian_Hieroglyphs: 'Hluw',
  113. Arabic: 'Arab',
  114. Armenian: 'Armn',
  115. Avestan: 'Avst',
  116. Balinese: 'Bali',
  117. Bamum: 'Bamu',
  118. Bassa_Vah: 'Bass',
  119. Batak: 'Batk',
  120. Bengali: 'Beng',
  121. Bhaiksuki: 'Bhks',
  122. Bopomofo: 'Bopo',
  123. Brahmi: 'Brah',
  124. Braille: 'Brai',
  125. Buginese: 'Bugi',
  126. Buhid: 'Buhd',
  127. Canadian_Aboriginal: 'Cans',
  128. Carian: 'Cari',
  129. Caucasian_Albanian: 'Aghb',
  130. Chakma: 'Cakm',
  131. Cham: 'Cham',
  132. Cherokee: 'Cher',
  133. Common: 'Zyyy',
  134. Coptic: ['Copt', 'Qaac'],
  135. Cuneiform: 'Xsux',
  136. Cypriot: 'Cprt',
  137. Cyrillic: 'Cyrl',
  138. Deseret: 'Dsrt',
  139. Devanagari: 'Deva',
  140. Dogra: 'Dogr',
  141. Duployan: 'Dupl',
  142. Egyptian_Hieroglyphs: 'Egyp',
  143. Elbasan: 'Elba',
  144. Ethiopic: 'Ethi',
  145. Georgian: 'Geor',
  146. Glagolitic: 'Glag',
  147. Gothic: 'Goth',
  148. Grantha: 'Gran',
  149. Greek: 'Grek',
  150. Gujarati: 'Gujr',
  151. Gunjala_Gondi: 'Gong',
  152. Gurmukhi: 'Guru',
  153. Han: 'Hani',
  154. Hangul: 'Hang',
  155. Hanifi_Rohingya: 'Rohg',
  156. Hanunoo: 'Hano',
  157. Hatran: 'Hatr',
  158. Hebrew: 'Hebr',
  159. Hiragana: 'Hira',
  160. Imperial_Aramaic: 'Armi',
  161. Inherited: ['Zinh', 'Qaai'],
  162. Inscriptional_Pahlavi: 'Phli',
  163. Inscriptional_Parthian: 'Prti',
  164. Javanese: 'Java',
  165. Kaithi: 'Kthi',
  166. Kannada: 'Knda',
  167. Katakana: 'Kana',
  168. Kayah_Li: 'Kali',
  169. Kharoshthi: 'Khar',
  170. Khmer: 'Khmr',
  171. Khojki: 'Khoj',
  172. Khudawadi: 'Sind',
  173. Lao: 'Laoo',
  174. Latin: 'Latn',
  175. Lepcha: 'Lepc',
  176. Limbu: 'Limb',
  177. Linear_A: 'Lina',
  178. Linear_B: 'Linb',
  179. Lisu: 'Lisu',
  180. Lycian: 'Lyci',
  181. Lydian: 'Lydi',
  182. Mahajani: 'Mahj',
  183. Makasar: 'Maka',
  184. Malayalam: 'Mlym',
  185. Mandaic: 'Mand',
  186. Manichaean: 'Mani',
  187. Marchen: 'Marc',
  188. Medefaidrin: 'Medf',
  189. Masaram_Gondi: 'Gonm',
  190. Meetei_Mayek: 'Mtei',
  191. Mende_Kikakui: 'Mend',
  192. Meroitic_Cursive: 'Merc',
  193. Meroitic_Hieroglyphs: 'Mero',
  194. Miao: 'Plrd',
  195. Modi: 'Modi',
  196. Mongolian: 'Mong',
  197. Mro: 'Mroo',
  198. Multani: 'Mult',
  199. Myanmar: 'Mymr',
  200. Nabataean: 'Nbat',
  201. New_Tai_Lue: 'Talu',
  202. Newa: 'Newa',
  203. Nko: 'Nkoo',
  204. Nushu: 'Nshu',
  205. Ogham: 'Ogam',
  206. Ol_Chiki: 'Olck',
  207. Old_Hungarian: 'Hung',
  208. Old_Italic: 'Ital',
  209. Old_North_Arabian: 'Narb',
  210. Old_Permic: 'Perm',
  211. Old_Persian: 'Xpeo',
  212. Old_Sogdian: 'Sogo',
  213. Old_South_Arabian: 'Sarb',
  214. Old_Turkic: 'Orkh',
  215. Oriya: 'Orya',
  216. Osage: 'Osge',
  217. Osmanya: 'Osma',
  218. Pahawh_Hmong: 'Hmng',
  219. Palmyrene: 'Palm',
  220. Pau_Cin_Hau: 'Pauc',
  221. Phags_Pa: 'Phag',
  222. Phoenician: 'Phnx',
  223. Psalter_Pahlavi: 'Phlp',
  224. Rejang: 'Rjng',
  225. Runic: 'Runr',
  226. Samaritan: 'Samr',
  227. Saurashtra: 'Saur',
  228. Sharada: 'Shrd',
  229. Shavian: 'Shaw',
  230. Siddham: 'Sidd',
  231. SignWriting: 'Sgnw',
  232. Sinhala: 'Sinh',
  233. Sogdian: 'Sogd',
  234. Sora_Sompeng: 'Sora',
  235. Soyombo: 'Soyo',
  236. Sundanese: 'Sund',
  237. Syloti_Nagri: 'Sylo',
  238. Syriac: 'Syrc',
  239. Tagalog: 'Tglg',
  240. Tagbanwa: 'Tagb',
  241. Tai_Le: 'Tale',
  242. Tai_Tham: 'Lana',
  243. Tai_Viet: 'Tavt',
  244. Takri: 'Takr',
  245. Tamil: 'Taml',
  246. Tangut: 'Tang',
  247. Telugu: 'Telu',
  248. Thaana: 'Thaa',
  249. Thai: 'Thai',
  250. Tibetan: 'Tibt',
  251. Tifinagh: 'Tfng',
  252. Tirhuta: 'Tirh',
  253. Ugaritic: 'Ugar',
  254. Vai: 'Vaii',
  255. Warang_Citi: 'Wara',
  256. Yi: 'Yiii',
  257. Zanabazar_Square: 'Zanb'
  258. };
  259. var SCRIPT_VALUE_ALIASES_TO_VALUE = inverseMap(SCRIPT_VALUE_TO_ALIASES);
  260. function inverseMap(data) {
  261. var inverse = {};
  262. for (var name in data) {
  263. if (!data.hasOwnProperty(name)) {
  264. continue;
  265. }
  266. var value = data[name];
  267. if (Array.isArray(value)) {
  268. for (var i = 0; i < value.length; i++) {
  269. inverse[value[i]] = name;
  270. }
  271. } else {
  272. inverse[value] = name;
  273. }
  274. }
  275. return inverse;
  276. }
  277. function isValidName(name) {
  278. return NON_BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name) || BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
  279. }
  280. function isValidValue(name, value) {
  281. if (isGeneralCategoryName(name)) {
  282. return isGeneralCategoryValue(value);
  283. }
  284. if (isScriptCategoryName(name)) {
  285. return isScriptCategoryValue(value);
  286. }
  287. return false;
  288. }
  289. function isAlias(name) {
  290. return NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
  291. }
  292. function isGeneralCategoryName(name) {
  293. return name === 'General_Category' || name == 'gc';
  294. }
  295. function isScriptCategoryName(name) {
  296. return name === 'Script' || name === 'Script_Extensions' || name === 'sc' || name === 'scx';
  297. }
  298. function isGeneralCategoryValue(value) {
  299. return GENERAL_CATEGORY_VALUE_TO_ALIASES.hasOwnProperty(value) || GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES.hasOwnProperty(value);
  300. }
  301. function isScriptCategoryValue(value) {
  302. return SCRIPT_VALUE_TO_ALIASES.hasOwnProperty(value) || SCRIPT_VALUE_ALIASES_TO_VALUE.hasOwnProperty(value);
  303. }
  304. function isBinaryPropertyName(name) {
  305. return BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
  306. }
  307. function getCanonicalName(name) {
  308. if (NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name)) {
  309. return NON_BINARY_ALIASES_TO_PROP_NAMES[name];
  310. }
  311. if (BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name)) {
  312. return BINARY_ALIASES_TO_PROP_NAMES[name];
  313. }
  314. return null;
  315. }
  316. function getCanonicalValue(value) {
  317. if (GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES.hasOwnProperty(value)) {
  318. return GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES[value];
  319. }
  320. if (SCRIPT_VALUE_ALIASES_TO_VALUE.hasOwnProperty(value)) {
  321. return SCRIPT_VALUE_ALIASES_TO_VALUE[value];
  322. }
  323. if (BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(value)) {
  324. return BINARY_ALIASES_TO_PROP_NAMES[value];
  325. }
  326. return null;
  327. }
  328. module.exports = {
  329. isAlias: isAlias,
  330. isValidName: isValidName,
  331. isValidValue: isValidValue,
  332. isGeneralCategoryValue: isGeneralCategoryValue,
  333. isScriptCategoryValue: isScriptCategoryValue,
  334. isBinaryPropertyName: isBinaryPropertyName,
  335. getCanonicalName: getCanonicalName,
  336. getCanonicalValue: getCanonicalValue,
  337. NON_BINARY_PROP_NAMES_TO_ALIASES: NON_BINARY_PROP_NAMES_TO_ALIASES,
  338. NON_BINARY_ALIASES_TO_PROP_NAMES: NON_BINARY_ALIASES_TO_PROP_NAMES,
  339. BINARY_PROP_NAMES_TO_ALIASES: BINARY_PROP_NAMES_TO_ALIASES,
  340. BINARY_ALIASES_TO_PROP_NAMES: BINARY_ALIASES_TO_PROP_NAMES,
  341. GENERAL_CATEGORY_VALUE_TO_ALIASES: GENERAL_CATEGORY_VALUE_TO_ALIASES,
  342. GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES: GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES,
  343. SCRIPT_VALUE_TO_ALIASES: SCRIPT_VALUE_TO_ALIASES,
  344. SCRIPT_VALUE_ALIASES_TO_VALUE: SCRIPT_VALUE_ALIASES_TO_VALUE
  345. };