Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
649 lines (467 loc) · 12.4 KB
/
Copy pathutils.js
File metadata and controls
649 lines (467 loc) · 12.4 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/** _ _ _
* __| (_)___ __ _ _ ___ | |_ ___
* / _` | (_-</ _| '_/ -_)| _/ _ \
* \__,_|_/__/\__|_| \___(_)__\___/
*
* Copyright © 2020 - MIT License
* Greg Deback <greg@discre.to>
* <https://discre.to>
*
* Utils
*/
'use strict'
// Import module
import * as _ from '../src/utils.js'
let
w = window,
d = document
/**
* Utils unit tests
*
* @coversModule utils
*/
describe('utils', () => {
/**
* Query selector
* Just a querySelector shorthand
*
* @covers utils.$
*/
describe('$', () => {
// $('body')
it('body gives <body>', () => {
expect(_.$('body')).to.be(d.body)
})
// $('#mocha')
it('#mocha exists', () => {
expect(_.$('#mocha')).to.be(d.getElementById('mocha'))
})
// $('#')
it('# should fail', () => {
expect(() => _.$('#')).throwError()
})
})
/**
* Query selector all
* Just a querySelectorAll shorthand
*
* @covers utils.$
*/
describe('$$', () => {
// $$('body')
it('body gives [ <body> ]', () => {
expect(_.$$('body')).to.be.a(w.NodeList)
expect(_.$$('body')[0]).to.be(d.body)
})
// $$('#')
it('# should fail too', () => {
expect(() => _.$$('#')).throwError()
})
})
/**
* Each
*
* @covers utils.each
*/
describe('each', () => {
// Arrays
it('calls method(value, index) on arrays', () => {
let
// value is index * 2
arr = [ 0, 2, 4 ],
n = arr.length,
p = 0
_.each(arr, (val, i) => {
expect(val).to.be(i * 2)
p++
})
expect(p).to.be(n)
})
// Objects
it('calls method(key, value) on objects', () => {
let
// value is char code of key
obj = { A: 65, B: 66, C: 67 },
n = Object.keys(obj).length,
p = 0
_.each(obj, (key, val) => {
expect(val).to.be(key.charCodeAt(0))
p++
})
expect(p).to.be(n)
})
// NodeLists
it('works on NodeList too', () => {
let
list = _.$$('script'),
n = list.length,
p = 0
_.each(list, (el, i) => {
expect(el.src).not.to.be(undefined)
p++
})
expect(p).to.be(n)
})
})
/**
* Merge
*
* @covers utils.merge
*/
describe('merge', () => {
// Nothing on non-iterable value
it('should do nothing', () => {
let
obj = { a: true },
prev = Object.keys(obj)
// Empty object
_.merge(obj, {})
expect(Object.keys(obj)).to.eql(prev)
// Boolean
_.merge(obj, false)
expect(Object.keys(obj)).to.eql(prev)
// Number
_.merge(obj, 1)
expect(Object.keys(obj)).to.eql(prev)
// You get the idea
})
// Add one key
it('adds a key', () => {
let
obj = { a: true },
prev = Object.keys(obj)
// Let's add b
_.merge(obj, { b: false })
expect(Object.keys(obj)).not.to.eql(prev)
expect(obj.b).to.be(false)
})
// Replace previous
it('replaces existing key', () => {
let
obj = { a: true },
prev = Object.keys(obj)
// Set a to false
_.merge(obj, { a: false })
expect(Object.keys(obj)).to.eql(prev)
expect(obj.a).to.be(false)
})
// Recursive
it('works recursively', () => {
let
obj = { a: true, b: { c: 0, d: 1 } },
opts = { b: { d: 2, e: 4 }, f: false }
// Expected: { a: true, b: { c: 0, d: 2, e: 4 }, f: false }
_.merge(obj, opts)
expect(Object.keys(obj).sort()).to.eql([ 'a', 'b', 'f' ])
expect(Object.keys(obj.b).sort()).to.eql([ 'c', 'd', 'e' ])
expect(obj.b.c).to.be(0)
expect(obj.b.d).to.be(2)
})
})
/**
* DOM
*
* @covers utils.dom
*/
describe('dom', () => {
// Object only
it('takes only objects', () => {
expect(() => _.dom()).throwError()
expect(() => _.dom('div')).throwError()
})
// New node
it('creates a node', () => {
let
div = _.dom({ tag: 'div' })
// 'DIV' HTMLElement
expect(div).to.be.an(w.HTMLElement)
expect(div.nodeName).to.be('DIV')
})
// Attributes & events
it('sets attributes & events', () => {
let
a = true,
div = _.dom({
tag: 'div',
atts: { 'data-test': 'test', class: 'class1 class2' },
evts: { click: () => (a = !a) },
html: 'Hello'
}),
clk = new w.Event('click')
// Attribute
expect(div.getAttribute('data-test')).to.be('test')
// Classes
expect(div.classList).to.contain('class1')
expect(div.classList).to.contain('class2')
// Inner HTML
expect(div.innerHTML).to.be('Hello')
// Event
div.dispatchEvent(clk)
expect(a).to.be(false)
})
// Inner HTML
it('sets innerHTML', () => {
let
div = _.dom({
tag: 'div',
html: 'yes',
child: []
})
expect(div.innerHTML).to.be('yes')
})
// Children
it('can have children', () => {
let
div = _.dom({
tag: 'div',
html: 'yes',
child: [ { tag: 'h1' }, { tag: 'p' } ]
})
// Keep inner HTML (as #text), that's 3 nodes
expect(div.childNodes.length).to.be(3)
expect(div.childNodes[0].nodeType).to.be(d.TEXT_NODE)
expect(div.childNodes[1].nodeName).to.be('H1')
})
// Allows HTML elements in tree
it('accepts DOM element children', () => {
let
p = _.dom({ tag: 'p' }),
div = _.dom({
tag: 'div',
child: [ p ]
})
// <p> is first child
expect(div.childNodes.length).to.be(1)
expect(div.firstChild).to.be(p)
})
// Quick syntax
// tag(#id)?(.(class_i))*
it('supports quick syntax', () => {
let
div = _.dom({ tag: 'div#id.class1.class2' })
// 'DIV' with id 'id' & 2 classes
expect(div.nodeName).to.be('DIV')
expect(div.id).to.be('id')
expect(div.classList).to.contain('class1')
expect(div.classList).to.contain('class2')
})
// Can store key nodes in map
it('can build a map', () => {
let
map = {},
div = _.dom({
tag: 'box:div',
child: [
{ tag: 'title:h1' },
{ tag: 'text:div' },
{ tag: 'text:p' } ]
}, map)
// Root element is dom
expect(map.box).to.be(div)
expect(map.title).to.be.an(w.HTMLElement)
expect(map.title.nodeName).to.be('H1')
// Last reference wins
expect(map.text.nodeName).to.be('P') // not 'DIV'
})
})
/**
* Load script (async)
*
* @covers utils.script
* @see ./message.js
*/
describe('script', () => {
// Default is async & before
it('inserts an async script before caller', () => {
let
msg = null,
script
return new Promise((resolve, reject) => {
// Listen to script message
w.addEventListener('message', (evt) => {
resolve(evt.data)
})
// Load message.js
script = _.script('./message.js')
expect(script).to.be.a(w.HTMLElement)
expect(script.getAttribute('src')).to.be('./message.js')
// Default is async
expect(script.getAttribute('async')).to.be.ok()
// Inserted first
expect(d.querySelector('script')).to.be(script)
if (typeof process !== 'undefined')
reject(null)
}).then((msg) => {
// Did he say hello?
expect(msg).to.be.eql({ hello: 'world' })
}, () => { // NodeJS...
})
})
// Can be sync
it('can be sync', () => {
let
script = _.script('./message.js', false)
// Async attribute is 'false'
// @todo Shouldn't it be undefined?!
expect(script.getAttribute('async')).to.be('false')
})
// Appended last
it('can be appended instead', () => {
let
script = _.script('./message.js', true, false),
all = d.querySelectorAll('script')
// Sync flag
expect(script.getAttribute('async')).to.be.ok()
// But last
expect(all[all.length - 1]).to.be(script)
})
})
/**
* Class manipulation
*
* @covers utils.css
*/
describe('css', () => {
// HTML element only
it('should fail on non element', () => {
expect(() => _.css()).throwError()
})
// Add class
it('adds a class', () => {
let
div = _.dom({ tag: 'div' })
// class1 is not there
expect(div.classList).not.to.contain('class1')
// Once
_.css(div, 'class1', true)
expect(div.classList).to.contain('class1')
// Twice has no effect
_.css(div, 'class1', true)
expect(div.classList).to.contain('class1')
expect(div.className).to.be('class1')
})
// @todo Multiple classes
//it('should support many classes', () => {
// expect(() => _.css()).throwError()
//})
// Remove class
it('removes a class', () => {
let
div = _.dom({ tag: 'div.class1' })
// class1 is there
expect(div.classList).to.contain('class1')
// And not there
_.css(div, 'class1', false)
expect(div.classList).not.to.contain('class1')
})
// Toogle class
it('should toggle a class', () => {
let
div = _.dom({ tag: 'div.class1' })
// Day...
expect(div.classList).to.contain('class1')
// Night...
_.css(div, 'class1')
expect(div.classList).not.to.contain('class1')
// Day!
_.css(div, 'class1')
expect(div.classList).to.contain('class1')
})
})
/**
* Locale
*
* @covers utils.locale
*/
describe('locale', () => {
// navigator.languages is readonly, but always inludes english
it('always has en-US & en', () => {
let
langs = navigator.languages
// Browsers & JSDOM speaks english, -US preferred
expect(langs).to.contain('en-US')
expect(langs).to.contain('en')
expect(langs.indexOf('en-US')).to.be.lessThan(langs.indexOf('en'))
})
// Pick up first match
it('picks up first match else fallback', () => {
let
i18n = {
'en-US': true,
en: true
}
// In order (languages like *, en-US, en)
expect(_.locale(i18n)).to.be('en-US')
delete i18n['en-US']
expect(_.locale(i18n)).to.be('en')
// English by default
delete i18n['en']
expect(_.locale(i18n)).to.be('en')
// Except if specified
expect(_.locale(i18n, 'fr')).to.be('fr')
})
})
/**
* XHR as a promise
*
* @covers utils.xhr
* @see ./message.php
*/
describe('xhr', () => {
if (!w.CORS)
return
// XHR response as a promise (default is GET)
it('returns response as a promise', () => {
return _.xhr(w.CORS + '/message.php').then((xhr) => {
// 200 OK: Hello world (for a change)
expect(xhr.status).to.be(200)
expect(xhr.responseText.trim()).to.be('Hello world')
}, (err) => {
expect(err).to.be(true)
})
})
// Resolve even with 404
it('resolves with status 404', () => {
return _.xhr(w.CORS + '/message.php?404').then((xhr) => {
// 404
expect(xhr.status).to.be(404)
}, (err) => {
expect(err).to.be(true)
})
})
// Pass arguments in data
it('supports GET data', () => {
return _.xhr(w.CORS + '/message.php', 'get', '404').then((xhr) => {
// Reponse text is METHOD
expect(xhr.status).to.be(404)
expect(xhr.responseText).to.be('GET')
}, (err) => {
expect(err).to.be(true)
})
})
// Pass POST data
it('supports POST data', () => {
return _.xhr(w.CORS + '/message.php', 'post', '404').then((xhr) => {
// Reponse text is METHOD
expect(xhr.status).to.be(404)
expect(xhr.responseText).to.be('POST')
}, (err) => {
expect(err).to.be(true)
})
})
// Browser only, will fail with Node
// @fixit
if (typeof process === 'undefined') {
// Rejected if no cors & not available
it('rejects response with wrong CORS', () => {
return _.xhr(w.CORS + '/message.php?nocors', 'get').then((xhr) => {
console.warn(xhr)
expect(xhr).to.be(false)
}, (err) => {
// Fail with status code (0 if no CORS)
expect(err).to.be(0) // not set
})
})
}
})
})