-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (95 loc) · 2.08 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
import {classify} from './GraphemeBreakProperty.js'
const Any = 0,
Prepend = 1,
CR = 2,
LF = 3,
Control = 4,
Extend = 5,
Regional = 6,
SpacingMark = 7,
L = 8,
V = 9,
T = 10,
LV = 11,
LVT = 12,
ZWJ = 13;
const grapheme_iterator = s => ({
*[Symbol.iterator]() {
let pp = -1; // GB1
let egc = "";
let regionalstack = 0;
for (const cs of s) {
const cc = cs.codePointAt(0);
const cp = classify(cc);
if (cp === Extend || cp === ZWJ
|| cp == SpacingMark /* GB9a */)
egc += cs; // GB9
else
switch (pp) {
case ZWJ:
case Prepend: // GB9b
case -1: // GB1
egc += cs;
break;
case Extend:
if (cp === Extend)
egc += cs;
else {
yield egc;
egc = cs;
}
break;
case CR:
if (cp === LF)
egc += cs; // GB3
else {
yield egc;
egc = cs; // GB4
}
break;
case L: // GB6
if (cp === L
|| cp === V
|| cp === LV
|| cp === LVT)
egc += cs;
else {
yield egc;
egc = cs;
}
break;
case LV: case V: // GB7
if (cp === V || cp === T)
egc += cs;
else {
yield egc;
egc = cs;
}
break;
case LVT: case T: // GB8
if (cp === T)
egc += cs;
else {
yield egc;
egc = cs;
}
break;
case Regional: // GB12
if (regionalstack % 2) { // GB13
egc += cs;
break;
}
// case Any: // GB999
// case Control: // GB4
// case LF: // GB4
default:
yield egc;
egc = cs;
}
regionalstack = cp === Regional ? regionalstack + 1 : 0;
pp = cp;
}
if (egc !== "") yield egc; // GB2
}
});
export default grapheme_iterator;