forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
449 lines (380 loc) · 13 KB
/
utils.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
'use strict';
const assert = require('assert');
const { JSDOM } = require('jsdom');
const slugify = require('../src/slugify');
const db = require('./mocks/databasemock');
describe('Utility Methods', () => {
// https://gist.github.com/robballou/9ee108758dc5e0e2d028
// create some jsdom magic to allow jQuery to work
const dom = new JSDOM('<html><body></body></html>');
global.window = dom.window;
global.document = dom.window.document;
global.jQuery = require('jquery');
global.$ = global.jQuery;
const { $ } = global;
const utils = require('../public/src/utils');
// https://github.com/jprichardson/string.js/blob/master/test/string.test.js
it('should decode HTML entities', (done) => {
assert.strictEqual(
utils.decodeHTMLEntities('Ken Thompson & Dennis Ritchie'),
'Ken Thompson & Dennis Ritchie'
);
assert.strictEqual(
utils.decodeHTMLEntities('3 < 4'),
'3 < 4'
);
assert.strictEqual(
utils.decodeHTMLEntities('http://'),
'http://'
);
done();
});
it('should strip HTML tags', (done) => {
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>'), 'just some text');
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>', ['p']), 'just <b>some</b> text');
assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> text', ['i']), 'just some <image/> text');
assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> <div>text</div>', ['i', 'div']), 'just some <image/> text');
done();
});
it('should preserve case if requested', (done) => {
assert.strictEqual(slugify('UPPER CASE', true), 'UPPER-CASE');
done();
});
it('should work if a number is passed in', (done) => {
assert.strictEqual(slugify(12345), '12345');
done();
});
describe('username validation', () => {
it('accepts latin-1 characters', () => {
const username = "John\"'-. Doeäâèéë1234";
assert(utils.isUserNameValid(username), 'invalid username');
});
it('rejects empty string', () => {
const username = '';
assert.equal(utils.isUserNameValid(username), false, 'accepted as valid username');
});
it('should reject new lines', () => {
assert.equal(utils.isUserNameValid('myusername\r\n'), false);
});
it('should reject new lines', () => {
assert.equal(utils.isUserNameValid('myusername\n'), false);
});
it('should reject tabs', () => {
assert.equal(utils.isUserNameValid('myusername\t'), false);
});
it('accepts square brackets', () => {
const username = '[best clan] julian';
assert(utils.isUserNameValid(username), 'invalid username');
});
it('accepts regular username', () => {
assert(utils.isUserNameValid('myusername'), 'invalid username');
});
it('accepts quotes', () => {
assert(utils.isUserNameValid('baris "the best" usakli'), 'invalid username');
});
});
describe('email validation', () => {
it('accepts sample address', () => {
const email = '[email protected]';
assert(utils.isEmailValid(email), 'invalid email');
});
it('rejects empty address', () => {
const email = '';
assert.equal(utils.isEmailValid(email), false, 'accepted as valid email');
});
});
describe('UUID generation', () => {
it('return unique random value every time', () => {
delete require.cache[require.resolve('../src/utils')];
const { generateUUID } = require('../src/utils');
const uuid1 = generateUUID();
const uuid2 = generateUUID();
assert.notEqual(uuid1, uuid2, 'matches');
});
});
describe('cleanUpTag', () => {
it('should cleanUp a tag', (done) => {
const cleanedTag = utils.cleanUpTag(',/#!$^*;TaG1:{}=_`<>\'"~()?|');
assert.equal(cleanedTag, 'tag1');
done();
});
it('should return empty string for invalid tags', (done) => {
assert.strictEqual(utils.cleanUpTag(undefined), '');
assert.strictEqual(utils.cleanUpTag(null), '');
assert.strictEqual(utils.cleanUpTag(false), '');
assert.strictEqual(utils.cleanUpTag(1), '');
assert.strictEqual(utils.cleanUpTag(0), '');
done();
});
});
it('should remove punctuation', (done) => {
const removed = utils.removePunctuation('some text with , ! punctuation inside "');
assert.equal(removed, 'some text with punctuation inside ');
done();
});
it('should return true if string has language key', (done) => {
assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true);
done();
});
it('should return false if string does not have language key', (done) => {
assert.equal(utils.hasLanguageKey('some text with no language keys'), false);
done();
});
it('should shallow merge two objects', (done) => {
const a = { foo: 1, cat1: 'ginger' };
const b = { baz: 2, cat2: 'phoebe' };
const obj = utils.merge(a, b);
assert.strictEqual(obj.foo, 1);
assert.strictEqual(obj.baz, 2);
assert.strictEqual(obj.cat1, 'ginger');
assert.strictEqual(obj.cat2, 'phoebe');
done();
});
it('should return the file extesion', (done) => {
assert.equal(utils.fileExtension('/path/to/some/file.png'), 'png');
done();
});
it('should return file mime type', (done) => {
assert.equal(utils.fileMimeType('/path/to/some/file.png'), 'image/png');
done();
});
it('should check if url is relative', (done) => {
assert.equal(utils.isRelativeUrl('/topic/1/slug'), true);
done();
});
it('should check if url is relative', (done) => {
assert.equal(utils.isRelativeUrl('https://nodebb.org'), false);
done();
});
it('should make number human readable', (done) => {
assert.equal(utils.makeNumberHumanReadable('1000'), '1.0k');
done();
});
it('should make number human readable', (done) => {
assert.equal(utils.makeNumberHumanReadable('1100000'), '1.1m');
done();
});
it('should make number human readable', (done) => {
assert.equal(utils.makeNumberHumanReadable('100'), '100');
done();
});
it('should make number human readable', (done) => {
assert.equal(utils.makeNumberHumanReadable(null), null);
done();
});
it('should make numbers human readable on elements', (done) => {
const el = $('<div title="100000"></div>');
utils.makeNumbersHumanReadable(el);
assert.equal(el.html(), '100.0k');
done();
});
it('should add commas to numbers', (done) => {
assert.equal(utils.addCommas('100'), '100');
done();
});
it('should add commas to numbers', (done) => {
assert.equal(utils.addCommas('1000'), '1,000');
done();
});
it('should add commas to numbers', (done) => {
assert.equal(utils.addCommas('1000000'), '1,000,000');
done();
});
it('should add commas to elements', (done) => {
const el = $('<div>1000000</div>');
utils.addCommasToNumbers(el);
assert.equal(el.html(), '1,000,000');
done();
});
it('should return passed in value if invalid', (done) => {
// eslint-disable-next-line no-loss-of-precision
const bigInt = -111111111111111111;
const result = utils.toISOString(bigInt);
assert.equal(bigInt, result);
done();
});
it('should return false if browser is not android', (done) => {
global.navigator = {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36',
};
assert.equal(utils.isAndroidBrowser(), false);
done();
});
it('should return true if browser is android', (done) => {
global.navigator = {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Android /58.0.3029.96 Safari/537.36',
};
assert.equal(utils.isAndroidBrowser(), true);
done();
});
it('should return false if not touch device', (done) => {
assert(!utils.isTouchDevice());
done();
});
it('should check if element is in viewport', (done) => {
const el = $('<div>some text</div>');
assert(utils.isElementInViewport(el));
done();
});
it('should get empty object for url params', (done) => {
const params = utils.params();
assert.equal(Object.keys(params), 0);
done();
});
it('should get url params', (done) => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp=2' });
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.strictEqual(params.herp, 2);
done();
});
it('should get url params as arrays', (done) => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3' });
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.deepStrictEqual(params.herp, [2, 3]);
done();
});
it('should get a single param', (done) => {
assert.equal(utils.param('somekey'), undefined);
done();
});
it('should get the full URLSearchParams object', async () => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3', full: true });
assert(params instanceof URLSearchParams);
assert.strictEqual(params.get('foo'), '1');
assert.strictEqual(params.get('bar'), 'test');
assert.strictEqual(params.get('herp[]'), '2');
});
describe('toType', () => {
it('should return param as is if not string', (done) => {
assert.equal(123, utils.toType(123));
done();
});
it('should convert return string numbers as numbers', (done) => {
assert.equal(123, utils.toType('123'));
done();
});
it('should convert string "false" to boolean false', (done) => {
assert.strictEqual(false, utils.toType('false'));
done();
});
it('should convert string "true" to boolean true', (done) => {
assert.strictEqual(true, utils.toType('true'));
done();
});
it('should parse json', (done) => {
const data = utils.toType('{"a":"1"}');
assert.equal(data.a, '1');
done();
});
it('should return string as is if its not json,true,false or number', (done) => {
const regularStr = 'this is a regular string';
assert.equal(regularStr, utils.toType(regularStr));
done();
});
});
describe('utils.props', () => {
const data = {};
it('should set nested data', (done) => {
assert.equal(10, utils.props(data, 'a.b.c.d', 10));
done();
});
it('should return nested object', (done) => {
const obj = utils.props(data, 'a.b.c');
assert.equal(obj.d, 10);
done();
});
it('should returned undefined without throwing', (done) => {
assert.equal(utils.props(data, 'a.b.c.foo.bar'), undefined);
done();
});
it('should return undefined if second param is null', (done) => {
assert.equal(utils.props(undefined, null), undefined);
done();
});
});
describe('isInternalURI', () => {
const target = { host: '', protocol: 'https' };
const reference = { host: '', protocol: 'https' };
it('should return true if they match', (done) => {
assert(utils.isInternalURI(target, reference, ''));
done();
});
it('should return true if they match', (done) => {
target.host = 'nodebb.org';
reference.host = 'nodebb.org';
assert(utils.isInternalURI(target, reference, ''));
done();
});
it('should handle relative path', (done) => {
target.pathname = '/forum';
assert(utils.isInternalURI(target, reference, '/forum'));
done();
});
it('should return false if they do not match', (done) => {
target.pathname = '';
reference.host = 'designcreateplay.com';
assert(!utils.isInternalURI(target, reference));
done();
});
});
it('escape html', (done) => {
const escaped = utils.escapeHTML('&<>');
assert.equal(escaped, '&<>');
done();
});
it('should escape regex chars', (done) => {
const escaped = utils.escapeRegexChars('some text {}');
assert.equal(escaped, 'some\\ text\\ \\{\\}');
done();
});
it('should get hours array', (done) => {
const currentHour = new Date().getHours();
const hours = utils.getHoursArray();
let index = hours.length - 1;
for (let i = currentHour, ii = currentHour - 24; i > ii; i -= 1) {
const hour = i < 0 ? 24 + i : i;
assert.equal(hours[index], `${hour}:00`);
index -= 1;
}
done();
});
it('should get days array', (done) => {
const currentDay = new Date(Date.now()).getTime();
const days = utils.getDaysArray();
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let index = 0;
for (let x = 29; x >= 0; x -= 1) {
const tmpDate = new Date(currentDay - (1000 * 60 * 60 * 24 * x));
assert.equal(`${months[tmpDate.getMonth()]} ${tmpDate.getDate()}`, days[index]);
index += 1;
}
done();
});
it('`utils.rtrim` should remove trailing space', (done) => {
assert.strictEqual(utils.rtrim(' thing '), ' thing');
assert.strictEqual(utils.rtrim('\tthing\t\t'), '\tthing');
assert.strictEqual(utils.rtrim('\t thing \t'), '\t thing');
done();
});
it('should profile function', (done) => {
const st = process.hrtime();
setTimeout(() => {
process.profile('it took', st);
done();
}, 500);
});
it('should return object with data', async () => {
const user = require('../src/user');
const uid1 = await user.create({ username: 'promise1' });
const uid2 = await user.create({ username: 'promise2' });
const result = await utils.promiseParallel({
user1: user.getUserData(uid1),
user2: user.getUserData(uid2),
});
assert(result.hasOwnProperty('user1') && result.hasOwnProperty('user2'));
assert.strictEqual(result.user1.uid, uid1);
assert.strictEqual(result.user2.uid, uid2);
});
});