|
1 | 1 | /**
|
2 | 2 | * @license Apache-2.0
|
3 | 3 | *
|
4 |
| -* Copyright (c) 2018 The Stdlib Authors. |
| 4 | +* Copyright (c) 2023 The Stdlib Authors. |
5 | 5 | *
|
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License");
|
7 | 7 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | // MODULES //
|
22 | 22 |
|
23 | 23 | var tape = require( 'tape' );
|
24 |
| -var noop = require( '@stdlib/utils-noop' ); |
25 |
| -var Float32Array = require( '@stdlib/array-float32' ); |
26 |
| -var Float64Array = require( '@stdlib/array-float64' ); |
27 |
| -var whileEach = require( './../../dist' ); |
| 24 | +var main = require( './../../dist' ); |
28 | 25 |
|
29 | 26 |
|
30 | 27 | // TESTS //
|
31 | 28 |
|
32 |
| -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
33 | 30 | t.ok( true, __filename );
|
34 |
| - t.strictEqual( typeof whileEach, 'function', 'main export is a function' ); |
35 |
| - t.end(); |
36 |
| -}); |
37 |
| - |
38 |
| -tape( 'the function throws an error if not provided a collection', function test( t ) { |
39 |
| - var values; |
40 |
| - var i; |
41 |
| - |
42 |
| - values = [ |
43 |
| - '5', |
44 |
| - 5, |
45 |
| - NaN, |
46 |
| - true, |
47 |
| - false, |
48 |
| - null, |
49 |
| - void 0, |
50 |
| - {}, |
51 |
| - function noop() {}, |
52 |
| - /.*/, |
53 |
| - new Date() |
54 |
| - ]; |
55 |
| - |
56 |
| - for ( i = 0; i < values.length; i++ ) { |
57 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
58 |
| - } |
59 |
| - t.end(); |
60 |
| - |
61 |
| - function badValue( value ) { |
62 |
| - return function badValue() { |
63 |
| - whileEach( value, noop, noop ); |
64 |
| - }; |
65 |
| - } |
66 |
| -}); |
67 |
| - |
68 |
| -tape( 'the function throws an error if not provided a predicate function', function test( t ) { |
69 |
| - var values; |
70 |
| - var i; |
71 |
| - |
72 |
| - values = [ |
73 |
| - '5', |
74 |
| - 5, |
75 |
| - NaN, |
76 |
| - true, |
77 |
| - false, |
78 |
| - null, |
79 |
| - void 0, |
80 |
| - {}, |
81 |
| - [], |
82 |
| - /.*/, |
83 |
| - new Date() |
84 |
| - ]; |
85 |
| - |
86 |
| - for ( i = 0; i < values.length; i++ ) { |
87 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
88 |
| - } |
89 |
| - t.end(); |
90 |
| - |
91 |
| - function badValue( value ) { |
92 |
| - return function badValue() { |
93 |
| - whileEach( [ 1, 2, 3 ], value, noop ); |
94 |
| - }; |
95 |
| - } |
96 |
| -}); |
97 |
| - |
98 |
| -tape( 'the function throws an error if not provided a function to invoke', function test( t ) { |
99 |
| - var values; |
100 |
| - var i; |
101 |
| - |
102 |
| - values = [ |
103 |
| - '5', |
104 |
| - 5, |
105 |
| - NaN, |
106 |
| - true, |
107 |
| - false, |
108 |
| - null, |
109 |
| - void 0, |
110 |
| - {}, |
111 |
| - [], |
112 |
| - /.*/, |
113 |
| - new Date() |
114 |
| - ]; |
115 |
| - |
116 |
| - for ( i = 0; i < values.length; i++ ) { |
117 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
118 |
| - } |
119 |
| - t.end(); |
120 |
| - |
121 |
| - function badValue( value ) { |
122 |
| - return function badValue() { |
123 |
| - whileEach( [ 1, 2, 3 ], noop, value ); |
124 |
| - }; |
125 |
| - } |
126 |
| -}); |
127 |
| - |
128 |
| -tape( 'if provided an empty collection, the function never invokes a provided function', function test( t ) { |
129 |
| - var arr = []; |
130 |
| - |
131 |
| - function predicate() { |
132 |
| - t.fail( 'should not be invoked' ); |
133 |
| - } |
134 |
| - |
135 |
| - function foo() { |
136 |
| - t.fail( 'should not be invoked' ); |
137 |
| - } |
138 |
| - |
139 |
| - whileEach( arr, predicate, foo ); |
140 |
| - |
141 |
| - t.deepEqual( arr, [], 'expected result' ); |
142 |
| - t.end(); |
143 |
| -}); |
144 |
| - |
145 |
| -tape( 'the function returns the input collection', function test( t ) { |
146 |
| - var arr; |
147 |
| - var out; |
148 |
| - |
149 |
| - function predicate() { |
150 |
| - t.pass( 'invoked predicated function' ); |
151 |
| - return true; |
152 |
| - } |
153 |
| - |
154 |
| - function foo() { |
155 |
| - t.pass( 'invoked provided function' ); |
156 |
| - } |
157 |
| - |
158 |
| - arr = [ 1, 2, 3 ]; |
159 |
| - |
160 |
| - out = whileEach( arr, predicate, foo ); |
161 |
| - |
162 |
| - t.strictEqual( out, arr, 'returns input collection' ); |
163 |
| - t.end(); |
164 |
| -}); |
165 |
| - |
166 |
| -tape( 'while a test condition is true, the function invokes a provided function for each element in a collection (array)', function test( t ) { |
167 |
| - var arr; |
168 |
| - var out; |
169 |
| - |
170 |
| - arr = [ 1, 2, NaN, 3 ]; |
171 |
| - out = [ 0, 0, 0, 0 ]; |
172 |
| - |
173 |
| - function predicate( value ) { |
174 |
| - return ( value === value ); |
175 |
| - } |
176 |
| - |
177 |
| - function copy( value, index ) { |
178 |
| - out[ index ] = value; |
179 |
| - } |
180 |
| - |
181 |
| - whileEach( arr, predicate, copy ); |
182 |
| - |
183 |
| - t.deepEqual( out, [ 1, 2, 0, 0 ], 'expected result' ); |
184 |
| - t.end(); |
185 |
| -}); |
186 |
| - |
187 |
| -tape( 'while a test condition is true, the function invokes a provided function for each element in a collection (array-like object)', function test( t ) { |
188 |
| - var expected; |
189 |
| - var arr; |
190 |
| - var out; |
191 |
| - |
192 |
| - function predicate( value ) { |
193 |
| - return ( value === value ); |
194 |
| - } |
195 |
| - |
196 |
| - function copy( value, index ) { |
197 |
| - out[ index ] = value; |
198 |
| - } |
199 |
| - |
200 |
| - arr = { |
201 |
| - 'length': 4, |
202 |
| - '0': 1, |
203 |
| - '1': 2, |
204 |
| - '2': NaN, |
205 |
| - '3': 3 |
206 |
| - }; |
207 |
| - out = { |
208 |
| - 'length': 4, |
209 |
| - '0': 0, |
210 |
| - '1': 0, |
211 |
| - '2': 0, |
212 |
| - '3': 0 |
213 |
| - }; |
214 |
| - |
215 |
| - whileEach( arr, predicate, copy ); |
216 |
| - |
217 |
| - expected = { |
218 |
| - 'length': 4, |
219 |
| - '0': 1, |
220 |
| - '1': 2, |
221 |
| - '2': 0, |
222 |
| - '3': 0 |
223 |
| - }; |
224 |
| - |
225 |
| - t.deepEqual( out, expected, 'expected result' ); |
226 |
| - t.end(); |
227 |
| -}); |
228 |
| - |
229 |
| -tape( 'while a test condition is true, the function invokes a provided function for each element in a collection (typed array)', function test( t ) { |
230 |
| - var arr; |
231 |
| - var out; |
232 |
| - |
233 |
| - function predicate( value ) { |
234 |
| - return ( value === value ); |
235 |
| - } |
236 |
| - |
237 |
| - function copy( value, index ) { |
238 |
| - out[ index ] = value; |
239 |
| - } |
240 |
| - |
241 |
| - arr = new Float64Array( [ 1.0, 2.0, NaN, 3.0 ] ); |
242 |
| - out = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); |
243 |
| - |
244 |
| - whileEach( arr, predicate, copy ); |
245 |
| - |
246 |
| - t.strictEqual( out[ 0 ], 1.0, 'expected result' ); |
247 |
| - t.strictEqual( out[ 1 ], 2.0, 'expected result' ); |
248 |
| - t.strictEqual( out[ 2 ], 0.0, 'expected result' ); |
249 |
| - t.strictEqual( out[ 3 ], 0.0, 'expected result' ); |
250 |
| - |
251 |
| - t.end(); |
252 |
| -}); |
253 |
| - |
254 |
| -tape( 'the function supports providing an execution context', function test( t ) { |
255 |
| - var ctx; |
256 |
| - var arr; |
257 |
| - |
258 |
| - function predicate( value ) { |
259 |
| - return ( value === value ); |
260 |
| - } |
261 |
| - |
262 |
| - function sum( value ) { |
263 |
| - /* eslint-disable no-invalid-this */ |
264 |
| - this.sum += value; |
265 |
| - this.count += 1; |
266 |
| - } |
267 |
| - |
268 |
| - ctx = { |
269 |
| - 'sum': 0, |
270 |
| - 'count': 0 |
271 |
| - }; |
272 |
| - arr = [ 1.0, 2.0, 3.0 ]; |
273 |
| - |
274 |
| - whileEach( arr, predicate, sum, ctx ); |
275 |
| - |
276 |
| - t.strictEqual( ctx.sum/ctx.count, 2.0, 'expected result' ); |
277 |
| - |
278 |
| - t.end(); |
279 |
| -}); |
280 |
| - |
281 |
| -tape( 'the function provides basic support for dynamic arrays', function test( t ) { |
282 |
| - var arr; |
283 |
| - var out; |
284 |
| - |
285 |
| - function predicate( value ) { |
286 |
| - return ( value === value ); |
287 |
| - } |
288 |
| - |
289 |
| - function copy( value, index, collection ) { |
290 |
| - out.push( value ); |
291 |
| - if ( index === collection.length-1 && collection.length < 10 ) { |
292 |
| - collection.push( index+2 ); |
293 |
| - } |
294 |
| - } |
295 |
| - |
296 |
| - arr = [ 1, 2, 3 ]; |
297 |
| - out = []; |
298 |
| - |
299 |
| - whileEach( arr, predicate, copy ); |
300 |
| - |
301 |
| - t.deepEqual( arr, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 'expected result' ); |
302 |
| - t.deepEqual( out, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 'expected result' ); |
303 |
| - t.end(); |
304 |
| -}); |
305 |
| - |
306 |
| -tape( 'the function does not skip empty elements', function test( t ) { |
307 |
| - var expected; |
308 |
| - var arr; |
309 |
| - |
310 |
| - function predicate( value, index ) { |
311 |
| - t.strictEqual( value, expected[ index ], 'provides expected value' ); |
312 |
| - return true; |
313 |
| - } |
314 |
| - |
315 |
| - function verify( value, index ) { |
316 |
| - t.strictEqual( value, expected[ index ], 'provides expected value' ); |
317 |
| - } |
318 |
| - |
319 |
| - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays |
320 |
| - expected = [ 1, void 0, void 0, 4 ]; |
321 |
| - |
322 |
| - whileEach( arr, predicate, verify ); |
323 |
| - t.end(); |
324 |
| -}); |
325 |
| - |
326 |
| -tape( 'the function invokes the predicate function and the function to apply with three arguments: value, index, collection', function test( t ) { |
327 |
| - var indices; |
328 |
| - var values; |
329 |
| - var arr; |
330 |
| - |
331 |
| - function predicate( value, index, collection ) { |
332 |
| - t.strictEqual( value, values[ index ], 'provides expected value' ); |
333 |
| - t.strictEqual( index, indices[ index ], 'provides expected index' ); |
334 |
| - t.strictEqual( collection, arr, 'provides input collection' ); |
335 |
| - return true; |
336 |
| - } |
337 |
| - |
338 |
| - function verify( value, index, collection ) { |
339 |
| - t.strictEqual( value, values[ index ], 'provides expected value' ); |
340 |
| - t.strictEqual( index, indices[ index ], 'provides expected index' ); |
341 |
| - t.strictEqual( collection, arr, 'provides input collection' ); |
342 |
| - } |
343 |
| - |
344 |
| - arr = [ 1, 2, 3, 4 ]; |
345 |
| - values = [ 1, 2, 3, 4 ]; |
346 |
| - indices = [ 0, 1, 2, 3 ]; |
347 |
| - |
348 |
| - whileEach( arr, predicate, verify ); |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
349 | 32 | t.end();
|
350 | 33 | });
|
0 commit comments