|
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 indexOf = require( './../../dist' ); |
| 24 | +var main = require( './../../dist' ); |
25 | 25 |
|
26 | 26 |
|
27 | 27 | // TESTS //
|
28 | 28 |
|
29 |
| -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
30 | 30 | t.ok( true, __filename );
|
31 |
| - t.strictEqual( typeof indexOf, 'function', 'main export is a function' ); |
32 |
| - t.end(); |
33 |
| -}); |
34 |
| - |
35 |
| -tape( 'the function throws a type error if not provided an array-like object', function test( t ) { |
36 |
| - var values; |
37 |
| - var i; |
38 |
| - |
39 |
| - values = [ |
40 |
| - 5, |
41 |
| - NaN, |
42 |
| - true, |
43 |
| - null, |
44 |
| - void 0, |
45 |
| - {}, // not array-like, as no `length` property |
46 |
| - function noop() {} // has `length` property, but is a function |
47 |
| - ]; |
48 |
| - |
49 |
| - for ( i = 0; i < values.length; i++ ) { |
50 |
| - t.throws( badValue( values[i] ), TypeError, 'throws type error when provided ' + values[ i ] ); |
51 |
| - } |
52 |
| - t.end(); |
53 |
| - |
54 |
| - function badValue( value ) { |
55 |
| - return function badValue() { |
56 |
| - indexOf( value, 3 ); |
57 |
| - }; |
58 |
| - } |
59 |
| -}); |
60 |
| - |
61 |
| -tape( 'if provided a `fromIndex`, which is not an integer, the function throws a type error', function test( t ) { |
62 |
| - var values; |
63 |
| - var i; |
64 |
| - |
65 |
| - values = [ |
66 |
| - '5', |
67 |
| - 5.234, |
68 |
| - NaN, |
69 |
| - true, |
70 |
| - null, |
71 |
| - void 0, |
72 |
| - {}, |
73 |
| - [], |
74 |
| - function noop() {} |
75 |
| - ]; |
76 |
| - |
77 |
| - for ( i = 0; i < values.length; i++ ) { |
78 |
| - t.throws( badValue( values[i] ), TypeError, 'throws type error when provided ' + values[ i ] ); |
79 |
| - } |
80 |
| - t.end(); |
81 |
| - |
82 |
| - function badValue( value ) { |
83 |
| - return function badValue() { |
84 |
| - indexOf( [ 1, 2, 3 ], 3, value ); |
85 |
| - }; |
86 |
| - } |
87 |
| -}); |
88 |
| - |
89 |
| -tape( 'if provided an array-like object having length `0`, the function always returns `-1`', function test( t ) { |
90 |
| - var idx = indexOf( [], 5 ); |
91 |
| - t.equal( idx, -1, 'returns -1' ); |
92 |
| - t.end(); |
93 |
| -}); |
94 |
| - |
95 |
| -tape( 'if provided a `fromIndex` which exceeds the input array length, the function always returns `-1`', function test( t ) { |
96 |
| - var idx = indexOf( [ 1, 2, 3 ], 2, 999999999999 ); |
97 |
| - t.equal( idx, -1, 'returns -1' ); |
98 |
| - t.end(); |
99 |
| -}); |
100 |
| - |
101 |
| -tape( 'the function returns the first index at which a given element can be found', function test( t ) { |
102 |
| - var idx = indexOf( [ 1, 1, 1, 2, 2, 3, 3 ], 2 ); |
103 |
| - t.equal( idx, 3, 'returns first occurrence index' ); |
104 |
| - t.end(); |
105 |
| -}); |
106 |
| - |
107 |
| -tape( 'the function supports finding `NaN` elements', function test( t ) { |
108 |
| - var idx = indexOf( [ 1, 1, 1, 2, NaN, 2, 3, NaN, 3 ], NaN ); |
109 |
| - t.equal( idx, 4, 'returns first occurrence index' ); |
110 |
| - t.end(); |
111 |
| -}); |
112 |
| - |
113 |
| -tape( 'if a search element is not present, the function returns `-1`', function test( t ) { |
114 |
| - var idx = indexOf( [ 1, 2, 3 ], 4 ); |
115 |
| - t.equal( idx, -1, 'returns -1' ); |
116 |
| - t.end(); |
117 |
| -}); |
118 |
| - |
119 |
| -tape( 'the function supports specifying a `fromIndex`', function test( t ) { |
120 |
| - var arr; |
121 |
| - var idx; |
122 |
| - |
123 |
| - arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ]; |
124 |
| - |
125 |
| - idx = indexOf( arr, 2 ); |
126 |
| - t.equal( idx, 2, 'returns first occurrence index' ); |
127 |
| - |
128 |
| - idx = indexOf( arr, 2, idx+1 ); |
129 |
| - t.equal( idx, 4, 'returns second occurrence index' ); |
130 |
| - |
131 |
| - idx = indexOf( arr, 2, idx+1 ); |
132 |
| - t.equal( idx, 5, 'returns third occurrence index' ); |
133 |
| - |
134 |
| - idx = indexOf( arr, 2, idx+1 ); |
135 |
| - t.equal( idx, 7, 'returns fourth occurrence index' ); |
136 |
| - |
137 |
| - idx = indexOf( arr, 2, idx+1 ); |
138 |
| - t.equal( idx, -1, 'returns -1' ); |
139 |
| - |
140 |
| - t.end(); |
141 |
| -}); |
142 |
| - |
143 |
| -tape( 'if provided a `fromIndex` which is less than `0`, the function determines a start index relative to the last array element', function test( t ) { |
144 |
| - var arr; |
145 |
| - var idx; |
146 |
| - |
147 |
| - arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ]; |
148 |
| - |
149 |
| - idx = indexOf( arr, 3, -1 ); |
150 |
| - t.equal( idx, 8, 'returns last element index' ); |
151 |
| - |
152 |
| - idx = indexOf( arr, 2, -1 ); |
153 |
| - t.equal( idx, -1, 'returns -1' ); |
154 |
| - |
155 |
| - idx = indexOf( arr, 2, -2 ); |
156 |
| - t.equal( idx, 7, 'returns second to last element index' ); |
157 |
| - |
158 |
| - idx = indexOf( arr, 2, -3 ); |
159 |
| - t.equal( idx, 7, 'returns second to last element index' ); |
160 |
| - |
161 |
| - idx = indexOf( arr, 2, -4 ); |
162 |
| - t.equal( idx, 5, 'returns 5' ); |
163 |
| - |
164 |
| - t.end(); |
165 |
| -}); |
166 |
| - |
167 |
| -tape( 'if provided a `fromIndex` which is less than `0` and whose absolute value exceeds the array length, the function starts searching from the first array element', function test( t ) { |
168 |
| - var arr; |
169 |
| - var idx; |
170 |
| - |
171 |
| - arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ]; |
172 |
| - |
173 |
| - idx = indexOf( arr, 2, -99999999999999 ); |
174 |
| - t.equal( idx, 2, 'returns first occurrence index' ); |
175 |
| - |
176 |
| - idx = indexOf( arr, 1, -99999999999999 ); |
177 |
| - t.equal( idx, 0, 'returns first occurrence index' ); |
178 |
| - |
179 |
| - t.end(); |
180 |
| -}); |
181 |
| - |
182 |
| -tape( 'the function supports searching `strings`', function test( t ) { |
183 |
| - var str; |
184 |
| - var idx; |
185 |
| - |
186 |
| - str = 'bebop'; |
187 |
| - idx = indexOf( str, 'o' ); |
188 |
| - |
189 |
| - t.equal( idx, 3, 'returns first occurrence index' ); |
190 |
| - t.end(); |
191 |
| -}); |
192 |
| - |
193 |
| -tape( 'the function supports array-like `objects`', function test( t ) { |
194 |
| - var obj; |
195 |
| - var idx; |
196 |
| - |
197 |
| - obj = { |
198 |
| - '0': 'beep', |
199 |
| - '1': 'boop', |
200 |
| - '2': 'bap', |
201 |
| - '3': 'bop', |
202 |
| - 'length': 4 |
203 |
| - }; |
204 |
| - |
205 |
| - idx = indexOf( obj, 'bap' ); |
206 |
| - |
207 |
| - t.equal( idx, 2, 'returns first occurrence index' ); |
208 |
| - t.end(); |
209 |
| -}); |
210 |
| - |
211 |
| -tape( 'the function does not guarantee that only "own" properties are searched', function test( t ) { |
212 |
| - var foo; |
213 |
| - var idx; |
214 |
| - |
215 |
| - function Foo() { |
216 |
| - this[ 0 ] = 'beep'; |
217 |
| - this[ 1 ] = 'boop'; |
218 |
| - this[ 2 ] = 'woot'; |
219 |
| - this[ 3 ] = 'bop'; |
220 |
| - this.length = 4; |
221 |
| - return this; |
222 |
| - } |
223 |
| - Foo.prototype[ 2 ] = 'bap'; |
224 |
| - |
225 |
| - foo = new Foo(); |
226 |
| - |
227 |
| - idx = indexOf( foo, 'bap' )
7CB5
; |
228 |
| - t.equal( idx, -1, 'returns -1' ); |
229 |
| - |
230 |
| - delete foo[ 2 ]; |
231 |
| - |
232 |
| - idx = indexOf( foo, 'bap' ); |
233 |
| - t.equal( idx, 2, 'returns property on prototype' ); |
234 |
| - |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
235 | 32 | t.end();
|
236 | 33 | });
|
0 commit comments