forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-utils-spec.js
120 lines (102 loc) · 5.61 KB
/
text-utils-spec.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
const textUtils = require('../src/text-utils');
describe('text utilities', () => {
describe('.hasPairedCharacter(string)', () =>
it('returns true when the string contains a surrogate pair, variation sequence, or combined character', () => {
expect(textUtils.hasPairedCharacter('abc')).toBe(false);
expect(textUtils.hasPairedCharacter('a\uD835\uDF97b\uD835\uDF97c')).toBe(
true
);
expect(textUtils.hasPairedCharacter('\uD835\uDF97')).toBe(true);
expect(textUtils.hasPairedCharacter('\u2714\uFE0E')).toBe(true);
expect(textUtils.hasPairedCharacter('e\u0301')).toBe(true);
expect(textUtils.hasPairedCharacter('\uD835')).toBe(false);
expect(textUtils.hasPairedCharacter('\uDF97')).toBe(false);
expect(textUtils.hasPairedCharacter('\uFE0E')).toBe(false);
expect(textUtils.hasPairedCharacter('\u0301')).toBe(false);
expect(textUtils.hasPairedCharacter('\uFE0E\uFE0E')).toBe(false);
expect(textUtils.hasPairedCharacter('\u0301\u0301')).toBe(false);
}));
describe('.isPairedCharacter(string, index)', () =>
it('returns true when the index is the start of a high/low surrogate pair, variation sequence, or combined character', () => {
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 0)
).toBe(false);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 1)
).toBe(true);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 2)
).toBe(false);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 3)
).toBe(false);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 4)
).toBe(true);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 5)
).toBe(false);
expect(
textUtils.isPairedCharacter('a\uD835\uDF97b\uD835\uDF97c', 6)
).toBe(false);
expect(textUtils.isPairedCharacter('a\u2714\uFE0E', 0)).toBe(false);
expect(textUtils.isPairedCharacter('a\u2714\uFE0E', 1)).toBe(true);
expect(textUtils.isPairedCharacter('a\u2714\uFE0E', 2)).toBe(false);
expect(textUtils.isPairedCharacter('a\u2714\uFE0E', 3)).toBe(false);
expect(textUtils.isPairedCharacter('\uD835')).toBe(false);
expect(textUtils.isPairedCharacter('\uDF97')).toBe(false);
expect(textUtils.isPairedCharacter('\uFE0E')).toBe(false);
expect(textUtils.isPairedCharacter('\uFE0E')).toBe(false);
expect(textUtils.isPairedCharacter('\uFE0E\uFE0E')).toBe(false);
expect(textUtils.isPairedCharacter('ae\u0301c', 0)).toBe(false);
expect(textUtils.isPairedCharacter('ae\u0301c', 1)).toBe(true);
expect(textUtils.isPairedCharacter('ae\u0301c', 2)).toBe(false);
expect(textUtils.isPairedCharacter('ae\u0301c', 3)).toBe(false);
expect(textUtils.isPairedCharacter('ae\u0301c', 4)).toBe(false);
}));
describe('.isDoubleWidthCharacter(character)', () =>
it('returns true when the character is either japanese, chinese or a full width form', () => {
expect(textUtils.isDoubleWidthCharacter('我')).toBe(true);
expect(textUtils.isDoubleWidthCharacter('私')).toBe(true);
expect(textUtils.isDoubleWidthCharacter('B')).toBe(true);
expect(textUtils.isDoubleWidthCharacter(',')).toBe(true);
expect(textUtils.isDoubleWidthCharacter('¢')).toBe(true);
expect(textUtils.isDoubleWidthCharacter('a')).toBe(false);
}));
describe('.isHalfWidthCharacter(character)', () =>
it('returns true when the character is an half width form', () => {
expect(textUtils.isHalfWidthCharacter('ハ')).toBe(true);
expect(textUtils.isHalfWidthCharacter('ヒ')).toBe(true);
expect(textUtils.isHalfWidthCharacter('ᆲ')).toBe(true);
expect(textUtils.isHalfWidthCharacter('■')).toBe(true);
expect(textUtils.isHalfWidthCharacter('B')).toBe(false);
}));
describe('.isKoreanCharacter(character)', () =>
it('returns true when the character is a korean character', () => {
expect(textUtils.isKoreanCharacter('우')).toBe(true);
expect(textUtils.isKoreanCharacter('가')).toBe(true);
expect(textUtils.isKoreanCharacter('ㅢ')).toBe(true);
expect(textUtils.isKoreanCharacter('ㄼ')).toBe(true);
expect(textUtils.isKoreanCharacter('O')).toBe(false);
}));
describe('.isWrapBoundary(previousCharacter, character)', () =>
it('returns true when the character is CJK or when the previous character is a space/tab', () => {
const anyCharacter = 'x';
expect(textUtils.isWrapBoundary(anyCharacter, '我')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, '私')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'B')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, ',')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, '¢')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'ハ')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'ヒ')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'ᆲ')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, '■')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, '우')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, '가')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'ㅢ')).toBe(true);
expect(textUtils.isWrapBoundary(anyCharacter, 'ㄼ')).toBe(true);
expect(textUtils.isWrapBoundary(' ', 'h')).toBe(true);
expect(textUtils.isWrapBoundary('\t', 'h')).toBe(true);
expect(textUtils.isWrapBoundary('a', 'h')).toBe(false);
}));
});