-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.mjs
139 lines (121 loc) · 3.8 KB
/
test.mjs
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
129
130
131
132
133
134
135
136
137
138
139
import JSON5 from 'json5';
import * as assert from 'assert';
import { gzipSync } from 'zlib';
import { JSONTemperer } from '../dist/json.js';
import { JSON5Temperer } from '../dist/json5.js';
import { NewlineTemperer } from '../dist/newline.js';
function gzippedSize(input) {
return gzipSync(input).byteLength;
}
{
console.log('Testing JSON array...');
const original = JSON.stringify(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']);
const tempered = new JSONTemperer().process(original);
assert.strictEqual(
tempered.length, original.length,
'raw string size'
)
assert.ok(
gzippedSize(tempered) < gzippedSize(original),
'gzipped size should decrease'
);
assert.deepStrictEqual(
[...JSON.parse(original)].sort(),
[...JSON.parse(tempered)].sort(),
'should have all the same values'
);
}
{
console.log('Testing JSON5 array...');
const original = JSON5.stringify(['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']);
const tempered = new JSON5Temperer().process(original);
assert.strictEqual(
tempered.length, original.length,
'raw string size'
)
assert.ok(
gzippedSize(tempered) < gzippedSize(original),
'gzipped size should decrease'
);
assert.deepStrictEqual(
[...JSON5.parse(original)].sort(),
[...JSON5.parse(tempered)].sort(),
'should have all the same values'
);
}
{
console.log('Testing JSON object...');
const original = JSON.stringify({
aqua:'#0ff', black:'#000', blue:'#00f', fuchsia:'#f0f',
gray:'#808080', green:'#008000', lime:'#0f0', maroon:'#800000',
navy:'#000080', olive:'#808000', purple:'#800080', red:'#f00',
silver:'#c0c0c0', teal:'#008080', white:'#fff', yellow:'#ff0',
});
const tempered = new JSONTemperer().process(original);
assert.strictEqual(
tempered.length, original.length,
'raw string size'
)
assert.ok(
gzippedSize(tempered) < gzippedSize(original),
'gzipped size should decrease'
);
assert.deepStrictEqual(
JSON.parse(original),
JSON.parse(tempered),
'should have all the same values'
);
}
{
console.log('Testing JSON5 object...');
const original = JSON5.stringify({
'whitespace in key': 'whitespace in value',
'not primitive': ['a', 'not', 'primitive', 'value', null, [1, 2, 3]],
primitive: true,
1: 'one',
2: 'two times one',
3: 'two times one plus one',
});
const tempered = new JSON5Temperer().process(original);
assert.strictEqual(
tempered.length, original.length,
'raw string size'
)
assert.ok(
gzippedSize(tempered) < gzippedSize(original),
'gzipped size should decrease'
);
assert.deepStrictEqual(
JSON5.parse(original),
JSON5.parse(tempered),
'should have all the same values'
);
}
{
console.log('Testing newline separated text...');
const original = [
'Lorem ipsum dolor sit amet',
'consectetur adipiscing elit',
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua',
'Ut enim ad minim veniam',
'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat',
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur',
'Excepteur sint occaecat cupidatat non proident',
'sunt in culpa qui officia deserunt mollit anim id est laborum',
'\u0020...and one more line to check that leading and trailing newlines are presereved\u0020'
].join('\n');
const tempered = new NewlineTemperer().process(original);
assert.strictEqual(
tempered.length, original.length,
'raw string size'
)
assert.ok(
gzippedSize(tempered) < gzippedSize(original),
'gzipped size should decrease'
);
assert.deepStrictEqual(
original.split('\n').sort(),
tempered.split('\n').sort(),
'should have all the same lines'
);
}