Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var B = require('../').Buffer
2
var test = require('tape')
3
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
4
5
test('detect utf16 surrogate pairs', function (t) {
6
var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
7
var buf = new B(text)
8
t.equal(text, buf.toString())
9
t.end()
10
})
11
12
test('replace orphaned utf16 surrogate lead code point', function (t) {
13
var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
14
var buf = new B(text)
15
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
16
t.end()
17
})
18
19
test('replace orphaned utf16 surrogate trail code point', function (t) {
20
var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
21
var buf = new B(text)
22
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
23
t.end()
24
})
25
26
test('do not write partial utf16 code units', function (t) {
27
var f = new B([0, 0, 0, 0, 0])
28
t.equal(f.length, 5)
29
var size = f.write('あいうえお', 'utf16le')
30
t.equal(size, 4)
31
t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
32
t.end()
33
})
34
35
test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
36
var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
37
38
var buf = new B(8)
39
buf.fill(0)
40
buf.write(text)
41
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ]))
42
43
buf = new B(7)
44
buf.fill(0)
45
buf.write(text)
46
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ]))
47
48
buf = new B(6)
49
buf.fill(0)
50
buf.write(text)
51
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
52
53
buf = new B(5)
54
buf.fill(0)
55
buf.write(text)
56
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
57
58
buf = new B(4)
59
buf.fill(0)
60
buf.write(text)
61
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
62
63
buf = new B(3)
64
buf.fill(0)
65
buf.write(text)
66
t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
67
68
buf = new B(2)
69
buf.fill(0)
70
buf.write(text)
71
t.deepEqual(buf, new B([ 0x00, 0x00 ]))
72
73
buf = new B(1)
74
buf.fill(0)
75
buf.write(text)
76
t.deepEqual(buf, new B([ 0x00 ]))
77
78
t.end()
79
})
80
81
test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
82
var text = 'a' + '\uDE38\uD83D' + 'b'
83
84
var buf = new B(8)
85
buf.fill(0)
86
buf.write(text)
87
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ]))
88
89
buf = new B(7)
90
buf.fill(0)
91
buf.write(text)
92
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ]))
93
94
buf = new B(6)
95
buf.fill(0)
96
buf.write(text)
97
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ]))
98
99
buf = new B(5)
100
buf.fill(0)
101
buf.write(text)
102
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ]))
103
104
buf = new B(4)
105
buf.fill(0)
106
buf.write(text)
107
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ]))
108
109
buf = new B(3)
110
buf.fill(0)
111
buf.write(text)
112
t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ]))
113
114
buf = new B(2)
115
buf.fill(0)
116
buf.write(text)
117
t.deepEqual(buf, new B([ 0x61, 0x00 ]))
118
119
buf = new B(1)
120
buf.fill(0)
121
buf.write(text)
122
t.deepEqual(buf, new B([ 0x61 ]))
123
124
t.end()
125
})
126
127