UNPKG

3.02 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.CharacterType = void 0;
28exports.getCharacterType = getCharacterType;
29const CharacterType = {
30 SPACE: 0,
31 ALPHA_LETTER: 1,
32 PUNCT: 2,
33 HAN_LETTER: 3,
34 KATAKANA_LETTER: 4,
35 HIRAGANA_LETTER: 5,
36 HALFWIDTH_KATAKANA_LETTER: 6,
37 THAI_LETTER: 7
38};
39exports.CharacterType = CharacterType;
40
41function isAlphabeticalScript(charCode) {
42 return charCode < 0x2e80;
43}
44
45function isAscii(charCode) {
46 return (charCode & 0xff80) === 0;
47}
48
49function isAsciiAlpha(charCode) {
50 return charCode >= 0x61 && charCode <= 0x7a || charCode >= 0x41 && charCode <= 0x5a;
51}
52
53function isAsciiDigit(charCode) {
54 return charCode >= 0x30 && charCode <= 0x39;
55}
56
57function isAsciiSpace(charCode) {
58 return charCode === 0x20 || charCode === 0x09 || charCode === 0x0d || charCode === 0x0a;
59}
60
61function isHan(charCode) {
62 return charCode >= 0x3400 && charCode <= 0x9fff || charCode >= 0xf900 && charCode <= 0xfaff;
63}
64
65function isKatakana(charCode) {
66 return charCode >= 0x30a0 && charCode <= 0x30ff;
67}
68
69function isHiragana(charCode) {
70 return charCode >= 0x3040 && charCode <= 0x309f;
71}
72
73function isHalfwidthKatakana(charCode) {
74 return charCode >= 0xff60 && charCode <= 0xff9f;
75}
76
77function isThai(charCode) {
78 return (charCode & 0xff80) === 0x0e00;
79}
80
81function getCharacterType(charCode) {
82 if (isAlphabeticalScript(charCode)) {
83 if (isAscii(charCode)) {
84 if (isAsciiSpace(charCode)) {
85 return CharacterType.SPACE;
86 } else if (isAsciiAlpha(charCode) || isAsciiDigit(charCode) || charCode === 0x5f) {
87 return CharacterType.ALPHA_LETTER;
88 }
89
90 return CharacterType.PUNCT;
91 } else if (isThai(charCode)) {
92 return CharacterType.THAI_LETTER;
93 } else if (charCode === 0xa0) {
94 return CharacterType.SPACE;
95 }
96
97 return CharacterType.ALPHA_LETTER;
98 }
99
100 if (isHan(charCode)) {
101 return CharacterType.HAN_LETTER;
102 } else if (isKatakana(charCode)) {
103 return CharacterType.KATAKANA_LETTER;
104 } else if (isHiragana(charCode)) {
105 return CharacterType.HIRAGANA_LETTER;
106 } else if (isHalfwidthKatakana(charCode)) {
107 return CharacterType.HALFWIDTH_KATAKANA_LETTER;
108 }
109
110 return CharacterType.ALPHA_LETTER;
111}
\No newline at end of file