This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq.js
186 lines (156 loc) · 4.62 KB
/
seq.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
var expect = require('must')
, count = require('../lib/count')
, each = require('../lib/each')
, take = require('../lib/take')
, type = require('../lib/type')
, seq = require('../lib/seq')
, src = require('../lib/src')
describe('seq', function() {
describe('when given an empty sequence or null value', function() {
it('should return null', function() {
each(
[ null
, undefined
, []
, ""
, {}
]
, function(empty) {
expect(seq(empty)).to.equal(null)
}
)
})
})
describe('when a sequence contains null or undefined', function() {
it('should not treat it as the end of the sequence', function() {
each(
[ [1, null, 2]
, [1, undefined, 2]
, [null]
, [undefined]
]
, function(arr) {
var s = seq(arr)
var i = 0
each(s, function(d) {
expect(d).to.equal(arr[i++])
})
}
)
})
})
each(
[ [ [1, 2, 3, 4, 5]
, [1, 2, 3, 4, 5]
]
, [ { foo : 1, bar : true, baz : 'wibble' }
, [['foo', 1], ['bar', true], ['baz', 'wibble']]
]
, [ 'H'+'e'+'l'+'l'+'o'
, ['H','e','l','l','o']
]
], function(t) {
describe('when given the ' + type(t[0]) + ': ' + src(t[0]), function() {
var s = seq(t[0])
it('should return an equivalent sequence', function() {
var i = 0
each(s, function(d) {
expect(d).to.eql(t[1][i++])
})
expect(s.length).to.equal(t[1].length)
})
})
}
)
each(
[ true
, 666
], function(test) {
describe('when given a ' + type(test), function() {
it('should throw an error', function() {
expect(function() { seq(test) }).to.throw(TypeError)
})
})
}
)
describe('when given a string with unicode shenanigans', function() {
it('should make a sequence of characters where code pairs are considered', function() {
var chr = ['I', 'ñ', 't', 'ë', 'r', 'n', 'â', 't', 'i', 'ô', 'n', 'à', 'l', 'i', 'z', 'æ', 't', 'i', 'ø', 'n', '☃', '💩']
, str = seq(chr.join(''))
expect(str.length).to.eql(chr.length)
var i = 0
each(str, function(c) {
expect(c).to.equal(chr[i++])
})
expect(i).to.equal(str.length)
})
})
describe('when rest is called', function() {
it('should return the same instance every time', function() {
var s = seq([1, 2, 3])
expect(s.rest).to.equal(s.rest)
expect(seq.isPrototypeOf(s.rest)).to.be.true
})
})
describe('when called with a next function' , function() {
it('should behave as a lazy sequence', function() {
var s = seq(Math.random)
each(take(5, s), function(n) {
expect(n).to.be.between(0, 1)
})
})
})
describe('when called with a next function and valid length `n`', function() {
it('should automatically end the lazy sequence after `n` iterations', function() {
var s = seq(Math.random, 3)
expect(s.length).to.equal(3)
expect(s.rest.length).to.equal(2)
expect(s.rest.rest.length).to.equal(1)
expect(s.rest.rest.rest).to.equal(null)
})
})
describe('when called with a next function that immediately returns `seq.done`', function() {
var empty = seq(function() { return seq.done })
it('should have a zero length', function() {
expect(empty.length).to.equal(0)
})
it('should return `null` for first', function() {
expect(empty.first).to.equal(null)
})
it('should return `null` for rest', function() {
expect(empty.rest).to.equal(null)
})
it('should not iterate when calling `next`', function() {
expect(empty.next()).to.equal(seq.done)
expect(empty.next()).to.equal(seq.done)
expect(empty.next()).to.equal(seq.done)
})
})
describe('when next is called', function() {
var s = seq([1, 2])
it('should behave as the ES6 iterator protocol', function() {
each(
[ { value: s.first }
, { value: s.rest.first }
]
, function(test) {
expect(s.next()).to.eql(test)
}
)
expect(s.next()).to.equal(seq.done)
})
describe('and if called after iteration is done', function() {
it('should automatically rewind and behave the same as before', function() {
each(
[ { value: s.first }
, { value: s.rest.first }
]
, function(test) {
expect(s.next()).to.eql(test)
}
)
expect(s.next()).to.equal(seq.done)
})
})
})
})