-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathutils-test.ts
211 lines (210 loc) · 6.29 KB
/
utils-test.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { assert } from 'chai';
import * as format from '../lib/utils/format';
import * as arrayUtils from '../lib/utils/array';
import * as typeUtils from '../lib/utils/type';
describe('format', function () {
describe('.escapeSpecialChars(string)', function () {
it('should escape all special characters', function () {
const string = '+-&&||!(){}[]^"~*?:\\';
const escapedString = format.escapeSpecialChars(string);
assert.equal(
escapedString,
'\\+\\-\\&\\&\\|\\|\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\\\'
);
});
});
describe('.dateISOify()', function () {
it('should convert all Date objects into ISO string representation of the time', function () {
const date = new Date();
const dateISOify = format.dateISOify(date);
assert.equal(dateISOify, date.toISOString());
const dateHash = { now: date };
const dateHashISOify = format.dateISOify(dateHash);
assert.equal(dateHashISOify.now, date.toISOString());
const dateArray = [date];
const dateArrayISOify = format.dateISOify(dateArray);
assert.equal(dateArrayISOify[0], date.toISOString());
});
});
});
describe('typeUtils', function () {
describe('.isNumber(), strict', function () {
it('should detect positively a parameter that contains a primitive number', function () {
assert.equal(typeUtils.isNumber(0), true, '0 is a number');
assert.equal(typeUtils.isNumber(-1), true, '-1 is a number');
assert.equal(
typeUtils.isNumber('-1'),
true,
'"-1" is a number (parseInt valid)'
);
assert.equal(typeUtils.isNumber('0'), true, '"0" is a number');
assert.equal(typeUtils.isNumber('1'), true, '"1" is a number');
assert.equal(
typeUtils.isNumber('1000000'),
true,
'"1000000" is a number'
);
assert.equal(
typeUtils.isNumber('100000000'),
true,
'"100000000" is a number'
);
});
it('should detect negatively a parameter that contains a mixed number or no number', function () {
assert.equal(
typeUtils.isNumber('1a'),
false,
'"1a" is not a number (mixed number)'
);
assert.equal(
typeUtils.isNumber('- 1'),
false,
'"- 1" is not a number (not parseInt valid)'
);
assert.equal(
typeUtils.isNumber('a123'),
false,
'"a123" is not a number (not parseInt valid)'
);
assert.equal(
typeUtils.isNumber('a'),
false,
'"a" is not a number (string)'
);
assert.equal(
typeUtils.isNumber(''),
false,
'"" is not a number (string)'
);
});
});
describe('.isNumber(), non strict', function () {
it('should detect positively a parameter that contains a primitive and a mixed number', function () {
assert.equal(typeUtils.isNumber(0, false), true, '0 is a number');
assert.equal(typeUtils.isNumber(-1, false), true, '-1 is a number');
assert.equal(
typeUtils.isNumber('-1', false),
true,
'"-1" is a number (parseInt valid)'
);
assert.equal(typeUtils.isNumber('0', false), true, '"0" is a number');
assert.equal(typeUtils.isNumber('1', false), true, '"1" is a number');
assert.equal(
typeUtils.isNumber('1000000', false),
true,
'"1000000" is a number'
);
assert.equal(
typeUtils.isNumber('100000000', false),
true,
'"100000000" is a number'
);
assert.equal(
typeUtils.isNumber('1a', false),
true,
'"1" is a number (parseInt valid)'
);
assert.equal(
typeUtils.isNumber('1123a', false),
true,
'"1123" is a number (parseInt valid)'
);
});
it('should detect negatively a parameter that contains no valid number', function () {
assert.equal(
typeUtils.isNumber('- 1', false),
false,
'"- 1" is not a number (not parseInt valid)'
);
assert.equal(
typeUtils.isNumber('a123', false),
false,
'"a123" is not a number (not parseInt valid)'
);
assert.equal(
typeUtils.isNumber('a', false),
false,
'"a" is not a number (string)'
);
assert.equal(
typeUtils.isNumber('', false),
false,
'"" is not a number (string)'
);
});
});
});
describe('array', function () {
describe('.toArray()', function () {
it('should array-ize everything', function () {
assert.equal(
Array.isArray(arrayUtils.toArray([])),
true,
'[] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray('')),
true,
'[""] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray([{ a: 123 }])),
true,
'[{a: 123}] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray([1])),
true,
'[1] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray(['a'])),
true,
'["a"] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray([0])),
true,
'[0] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray([-1])),
true,
'[-1] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray('')),
true,
'[""] is an array'
);
assert.equal(
Array.isArray(arrayUtils.toArray(0)),
true,
'[0] is an array'
);
assert.equal(Array.isArray(arrayUtils.toArray()), true, '[] is an array');
});
it('should handle null and undefined properly', function () {
assert.equal(
arrayUtils.toArray(undefined, '')[0] === '',
true,
'undefined was cleaned up as empty string'
);
assert.equal(
arrayUtils.toArray(undefined)[0] === '',
true,
'using the default setup, undefined was cleaned up as empty string'
);
assert.equal(
arrayUtils.toArray(null, '')[0] === '',
true,
'null was cleaned up as empty string'
);
assert.equal(
arrayUtils.toArray(null)[0] === '',
true,
'using the default setup, null was cleaned up as empty string'
);
});
});
});