|
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 Float64Array = require( '@stdlib/array-float64' ); |
26 |
| -var someByRight = require( './../../dist' ); |
| 24 | +var main = require( './../../dist' ); |
27 | 25 |
|
28 | 26 |
|
29 | 27 | // TESTS //
|
30 | 28 |
|
31 |
| -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
32 | 30 | t.ok( true, __filename );
|
33 |
| - t.strictEqual( typeof someByRight, 'function', 'main export is a function' ); |
34 |
| - t.end(); |
35 |
| -}); |
36 |
| - |
37 |
| -tape( 'the function throws an error if not provided a collection', function test( t ) { |
38 |
| - var values; |
39 |
| - var i; |
40 |
| - |
41 |
| - values = [ |
42 |
| - '5', |
43 |
| - 5, |
44 |
| - NaN, |
45 |
| - true, |
46 |
| - false, |
47 |
| - null, |
48 |
| - void 0, |
49 |
| - {}, |
50 |
| - function noop() {}, |
51 |
| - /.*/, |
52 |
| - new Date() |
53 |
| - ]; |
54 |
| - |
55 |
| - for ( i = 0; i < values.length; i++ ) { |
56 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
57 |
| - } |
58 |
| - t.end(); |
59 |
| - |
60 |
| - function badValue( value ) { |
61 |
| - return function badValue() { |
62 |
| - someByRight( value, 1, noop ); |
63 |
| - }; |
64 |
| - } |
65 |
| -}); |
66 |
| - |
67 |
| -tape( 'the function throws an error if not provided a second argument which is a positive integer', function test( t ) { |
68 |
| - var values; |
69 |
| - var i; |
70 |
| - |
71 |
| - values = [ |
72 |
| - '5', |
73 |
| - -5, |
74 |
| - 0, |
75 |
| - 3.14, |
76 |
| - NaN, |
77 |
| - true, |
78 |
| - false, |
79 |
| - null, |
80 |
| - void 0, |
81 |
| - {}, |
82 |
| - [], |
83 |
| - function noop() {} |
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 |
| - someByRight( [ 1, 2, 3 ], value, noop ); |
94 |
| - }; |
95 |
| - } |
96 |
| -}); |
97 |
| - |
98 |
| -tape( 'the function throws an error if not provided a predicate function', 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 |
| - someByRight( [ 1, 2, 3 ], 2, value ); |
124 |
| - }; |
125 |
| - } |
126 |
| -}); |
127 |
| - |
128 |
| -tape( 'if provided an empty collection, the function returns `false`', function test( t ) { |
129 |
| - var bool; |
130 |
| - var arr; |
131 |
| - |
132 |
| - function foo() { |
133 |
| - t.fail( 'should not be invoked' ); |
134 |
| - } |
135 |
| - arr = []; |
136 |
| - bool = someByRight( arr, 1, foo ); |
137 |
| - |
138 |
| - t.strictEqual( bool, false, 'returns false' ); |
139 |
| - t.end(); |
140 |
| -}); |
141 |
| - |
142 |
| -tape( 'the function returns `true` if a collection contains at least `n` elements which pass a test (array)', function test( t ) { |
143 |
| - var bool; |
144 |
| - var arr; |
145 |
| - |
146 |
| - arr = [ -1, 1, -2, 3 ]; |
147 |
| - |
148 |
| - function isNegative( value ) { |
149 |
| - return ( value < 0 ); |
150 |
| - } |
151 |
| - |
152 |
| - bool = someByRight( arr, 2, isNegative ); |
153 |
| - |
154 |
| - t.strictEqual( bool, true, 'returns true' ); |
155 |
| - t.end(); |
156 |
| -}); |
157 |
| - |
158 |
| -tape( 'the function returns `false` if a collection does not contain at least `n` elements which pass a test (array)', function test( t ) { |
159 |
| - var bool; |
160 |
| - var arr; |
161 |
| - |
162 |
| - arr = [ -1, -2, -3 ]; |
163 |
| - |
164 |
| - function isPositive( value ) { |
165 |
| - return ( value > 0 ); |
166 |
| - } |
167 |
| - |
168 |
| - bool = someByRight( arr, 1, isPositive ); |
169 |
| - |
170 |
| - t.strictEqual( bool, false, 'returns false' ); |
171 |
| - t.end(); |
172 |
| -}); |
173 |
| - |
174 |
| -tape( 'the function returns `true` if a collection contains at least `n` elements which pass a test (array-like object)', function test( t ) { |
175 |
| - var bool; |
176 |
| - var arr; |
177 |
| - |
178 |
| - arr = { |
179 |
| - 'length': 4, |
180 |
| - '0': -1, |
181 |
| - '1': 2, |
182 |
| - '2': -3, |
183 |
| - '3': 4 |
184 |
| - }; |
185 |
| - |
186 |
| - function isNegative( value ) { |
187 |
| - return ( value < 0 ); |
188 |
| - } |
189 |
| - |
190 |
| - bool = someByRight( arr, 2, isNegative ); |
191 |
| - |
192 |
| - t.strictEqual( bool, true, 'returns true' ); |
193 |
| - t.end(); |
194 |
| -}); |
195 |
| - |
196 |
| -tape( 'the function returns `false` if a collection does not contain at least `n` elements which pass a test (array-like object)', function test( t ) { |
197 |
| - var bool; |
198 |
| - var arr; |
199 |
| - |
200 |
| - arr = { |
201 |
| - 'length': 3, |
202 |
| - '0': -1, |
203 |
| - '1': -2, |
204 |
| - '2': -3 |
205 |
| - }; |
206 |
| - |
207 |
| - function isPositive( value ) { |
208 |
| - return ( value > 0 ); |
209 |
| - } |
210 |
| - |
211 |
| - bool = someByRight( arr, 1, isPositive ); |
212 |
| - |
213 |
| - t.strictEqual( bool, false, 'returns false' ); |
214 |
| - t.end(); |
215 |
| -}); |
216 |
| - |
217 |
| -tape( 'the function returns `true` if a collection contains at least `n` elements which pass a test (typed array)', function test( t ) { |
218 |
| - var bool; |
219 |
| - var arr; |
220 |
| - |
221 |
| - arr = new Float64Array( [ -1.0, -2.0, -3.0, 4.0 ] ); |
222 |
| - |
223 |
| - function isNegative( value ) { |
224 |
| - return ( value < 0 ); |
225 |
| - } |
226 |
| - |
227 |
| - bool = someByRight( arr, 3, isNegative ); |
228 |
| - |
229 |
| - t.strictEqual( bool, true, 'returns true' ); |
230 |
| - t.end(); |
231 |
| -}); |
232 |
| - |
233 |
| -tape( 'the function returns `false` if a collection does not contain at least `n` elements which pass a test (typed array)', function test( t ) { |
234 |
| - var bool; |
235 |
| - var arr; |
236 |
| - |
237 |
| - arr = new Float64Array( [ -1.0, -2.0, -3.0 ] ); |
238 |
| - |
239 |
| - function isPositive( value ) { |
240 |
| - return ( value > 0 ); |
241 |
| - } |
242 |
| - |
243 |
| - bool = someByRight( arr, 1, isPositive ); |
244 |
| - |
245 |
| - t.strictEqual( bool, false, 'returns false' ); |
246 |
| - t.end(); |
247 |
| -}); |
248 |
| - |
249 |
| -tape( 'the function supports providing an execution context', function test( t ) { |
250 |
| - var bool; |
251 |
| - var ctx; |
252 |
| - var arr; |
253 |
| - |
254 |
| - function sum( value ) { |
255 |
| - /* eslint-disable no-invalid-this */ |
256 |
| - this.sum += value; |
257 |
| - this.count += 1; |
258 |
| - return ( value < 0 ); |
259 |
| - } |
260 |
| - |
261 |
| - ctx = { |
262 |
| - 'sum': 0, |
263 |
| - 'count': 0 |
264 |
| - }; |
265 |
| - arr = [ -1.0, 2.0, -3.0, 1.0 ]; |
266 |
| - |
267 |
| - bool = someByRight( arr, 2, sum, ctx ); |
268 |
| - |
269 |
| - t.strictEqual( bool, true, 'returns true' ); |
270 |
| - t.strictEqual( ctx.sum/ctx.count, -0.25, 'expected result' ); |
271 |
| - |
272 |
| - t.end(); |
273 |
| -}); |
274 |
| - |
275 |
| -tape( 'the function provides basic support for dynamic arrays', function test( t ) { |
276 |
| - var bool; |
277 |
| - var arr; |
278 |
| - |
279 |
| - arr = [ 1, 2, 3 ]; |
280 |
| - |
281 |
| - function isNegative( value, index, collection ) { |
282 |
| - if ( index === 0 ) { |
283 |
| - collection.unshift( value-1 ); |
284 |
| - } |
285 |
| - return ( value < 0 ); |
286 |
| - } |
287 |
| - |
288 |
| - bool = someByRight( arr, 1, isNegative ); |
289 |
| - |
290 |
| - t.deepEqual( arr, [ -2, -1, 0, 1, 2, 3 ], 'expected result' ); |
291 |
| - t.strictEqual( bool, true, 'returns true' ); |
292 |
| - |
293 |
| - t.end(); |
294 |
| -}); |
295 |
| - |
296 |
| -tape( 'the function does not skip empty elements', function test( t ) { |
297 |
| - var expected; |
298 |
| - var bool; |
299 |
| - var arr; |
300 |
| - var i; |
301 |
| - |
302 |
| - arr = [ -1, 1, , , 4 ]; // eslint-disable-line no-sparse-arrays |
303 |
| - expected = [ 4, void 0, void 0, 1, -1 ]; |
304 |
| - i = -1; |
305 |
| - |
306 |
| - function verify( value ) { |
307 |
| - i += 1; |
308 |
| - t.strictEqual( value, expected[ i ], 'provides expected value' ); |
309 |
| - return ( value < 0 ); |
310 |
| - } |
311 |
| - |
312 |
| - bool = someByRight( arr, 1, verify ); |
313 |
| - |
314 |
| - t.strictEqual( bool, true, 'returns true' ); |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
315 | 32 | t.end();
|
316 | 33 | });
|
0 commit comments