This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.spec.js
121 lines (107 loc) · 2.54 KB
/
utils.spec.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
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import * as utils from '../src/utils.js'
import filter from 'it-filter'
import take from 'it-take'
import map from 'it-map'
describe('utils', () => {
it('filter - sync', async () => {
const data = [1, 2, 3, 4]
/**
* @param {number} val
*/
const filterer = val => val % 2 === 0
const res = []
for await (const val of filter(data, filterer)) {
res.push(val)
}
expect(res).to.be.eql([2, 4])
})
it('filter - async', async () => {
const data = [1, 2, 3, 4]
/**
* @param {number} val
*/
const filterer = val => val % 2 === 0
const res = []
for await (const val of filter(data, filterer)) {
res.push(val)
}
expect(res).to.be.eql([2, 4])
})
it('sortAll', async () => {
const data = [1, 2, 3, 4]
/**
* @param {number} a
* @param {number} b
*/
const sorter = (a, b) => {
if (a < b) {
return 1
}
if (a > b) {
return -1
}
return 0
}
const res = []
for await (const val of utils.sortAll(data, sorter)) {
res.push(val)
}
expect(res).to.be.eql([4, 3, 2, 1])
})
it('sortAll - fail', async () => {
const data = [1, 2, 3, 4]
const sorter = () => { throw new Error('fail') }
const res = []
try {
for await (const val of utils.sortAll(data, sorter)) {
res.push(val)
}
} catch (/** @type {any} */ err) {
expect(err.message).to.be.eql('fail')
return
}
throw new Error('expected error to be thrown')
})
it('should take n values from iterator', async () => {
const data = [1, 2, 3, 4]
const n = 3
const res = []
for await (const val of take(data, n)) {
res.push(val)
}
expect(res).to.be.eql([1, 2, 3])
})
it('should take nothing from iterator', async () => {
const data = [1, 2, 3, 4]
const n = 0
for await (const _ of take(data, n)) { // eslint-disable-line
throw new Error('took a value')
}
})
it('should map iterator values', async () => {
const data = [1, 2, 3, 4]
/**
* @param {number} n
*/
const mapper = n => n * 2
const res = []
for await (const val of map(data, mapper)) {
res.push(val)
}
expect(res).to.be.eql([2, 4, 6, 8])
})
it('replaceStartWith', () => {
expect(
utils.replaceStartWith('helloworld', 'hello')
).to.eql(
'world'
)
expect(
utils.replaceStartWith('helloworld', 'world')
).to.eql(
'helloworld'
)
})
})