This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunicode.js
128 lines (107 loc) · 4.96 KB
/
unicode.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import {
isLowSurrogateHalf,
isHighSurrogateHalf,
isCombiningMark,
isInsideSurrogatePair,
isInsideCombinedSymbol
} from '../src/unicode';
describe( 'utils', () => {
describe( 'unicode', () => {
describe( 'isHighSurrogateHalf', () => {
it( 'should return true if given character is a high surrogate half', () => {
// '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
// '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
expect( isHighSurrogateHalf( String.fromCharCode( 55394 ) ) ).to.be.true;
} );
it( 'should return false if given character is a low surrogate half', () => {
// '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
// '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
expect( isHighSurrogateHalf( String.fromCharCode( 57166 ) ) ).to.be.false;
} );
it( 'should return false if given character is not a surrogate half', () => {
expect( isHighSurrogateHalf( 'a' ) ).to.be.false;
expect( isHighSurrogateHalf( '𨭎' ) ).to.be.false;
} );
it( 'should return false for falsy values', () => {
expect( isHighSurrogateHalf( null ) ).to.be.false;
expect( isHighSurrogateHalf( undefined ) ).to.be.false;
expect( isHighSurrogateHalf( false ) ).to.be.false;
expect( isHighSurrogateHalf( 0 ) ).to.be.false;
} );
} );
describe( 'isLowSurrogateHalf', () => {
it( 'should return true if given character is a low surrogate half', () => {
// '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
// '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
expect( isLowSurrogateHalf( String.fromCharCode( 57166 ) ) ).to.be.true;
} );
it( 'should return false if given character is a high surrogate half', () => {
// '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
// '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
expect( isLowSurrogateHalf( String.fromCharCode( 55394 ) ) ).to.be.false;
} );
it( 'should return false if given character is not a surrogate half', () => {
expect( isLowSurrogateHalf( 'a' ) ).to.be.false;
expect( isLowSurrogateHalf( '𨭎' ) ).to.be.false;
} );
it( 'should return false for falsy values', () => {
expect( isLowSurrogateHalf( null ) ).to.be.false;
expect( isLowSurrogateHalf( undefined ) ).to.be.false;
expect( isLowSurrogateHalf( false ) ).to.be.false;
expect( isLowSurrogateHalf( 0 ) ).to.be.false;
} );
} );
describe( 'isCombiningMark', () => {
it( 'should return true if given character is a combining mark', () => {
expect( isCombiningMark( '̣' ) ).to.be.true;
} );
it( 'should return false if given character is not a combining mark', () => {
expect( isCombiningMark( 'a' ) ).to.be.false;
expect( isCombiningMark( 'q̣' ) ).to.be.false;
} );
it( 'should return false for falsy values', () => {
expect( isCombiningMark( null ) ).to.be.false;
expect( isCombiningMark( undefined ) ).to.be.false;
expect( isCombiningMark( false ) ).to.be.false;
expect( isCombiningMark( 0 ) ).to.be.false;
} );
} );
describe( 'isInsideSurrogatePair', () => {
const testString = 'a𨭎b';
it( 'should return true if given offset in a string is inside a surrogate pair', () => {
expect( isInsideSurrogatePair( testString, 2 ) ).to.be.true;
} );
it( 'should return false if given offset in a string is not inside a surrogate pair', () => {
expect( isInsideSurrogatePair( testString, 1 ) ).to.be.false;
expect( isInsideSurrogatePair( testString, 3 ) ).to.be.false;
} );
it( 'should return false if given offset in a string is at the string beginning or end', () => {
expect( isInsideSurrogatePair( testString, 0 ) ).to.be.false;
expect( isInsideSurrogatePair( testString, 4 ) ).to.be.false;
} );
it( 'should return false if given offset is between two surrogate pairs', () => {
expect( isInsideSurrogatePair( '𨭎𨭎', 2 ) ).to.be.false;
} );
} );
describe( 'isInsideCombinedSymbol', () => {
const testString = 'aa̻̐ͩbb';
it( 'should return true if given offset in a string is inside a symbol with combining mark', () => {
expect( isInsideCombinedSymbol( testString, 2 ) ).to.be.true;
expect( isInsideCombinedSymbol( testString, 3 ) ).to.be.true;
expect( isInsideCombinedSymbol( testString, 4 ) ).to.be.true;
} );
it( 'should return false if given offset in a string is not inside a symbol with combining mark', () => {
expect( isInsideCombinedSymbol( testString, 1 ) ).to.be.false;
expect( isInsideCombinedSymbol( testString, 5 ) ).to.be.false;
} );
it( 'should return false if given offset in a string is at the string beginning or end', () => {
expect( isInsideCombinedSymbol( testString, 0 ) ).to.be.false;
expect( isInsideCombinedSymbol( testString, 6 ) ).to.be.false;
} );
} );
} );
} );