@@ -210,49 +210,81 @@ describe('asyncmap', function () {
210210 await sleep ( 10 ) ;
211211 return el * 2 ;
212212 } ;
213- const coll = [ 1 , 2 , 3 ] ;
213+ const coll = [ 1 , 2 , 3 , 4 , 5 ] ;
214+ const newColl = [ 2 , 4 , 6 , 8 , 10 ] ;
214215 it ( 'should map elements one at a time' , async function ( ) {
215216 const start = Date . now ( ) ;
216- expect ( await asyncmap ( coll , mapper , false ) ) . to . eql ( [ 2 , 4 , 6 ] ) ;
217- expect ( Date . now ( ) - start ) . to . be . at . least ( 30 ) ;
217+ expect ( await asyncmap ( coll , mapper , false ) ) . to . eql ( newColl ) ;
218+ expect ( Date . now ( ) - start ) . to . be . at . least ( 50 ) ;
218219 } ) ;
219220 it ( 'should map elements in parallel' , async function ( ) {
220221 const start = Date . now ( ) ;
221- expect ( await asyncmap ( coll , mapper ) ) . to . eql ( [ 2 , 4 , 6 ] ) ;
222+ expect ( await asyncmap ( coll , mapper ) ) . to . eql ( newColl ) ;
222223 expect ( Date . now ( ) - start ) . to . be . at . most ( 20 ) ;
223224 } ) ;
225+ it ( 'should map elements with concurrency' , async function ( ) {
226+ const start = Date . now ( ) ;
227+ expect ( await asyncmap ( coll , mapper , { concurrency : 2 } ) ) . to . eql ( newColl ) ;
228+ expect ( Date . now ( ) - start ) . to . be . at . least ( 29 ) ;
229+ expect ( Date . now ( ) - start ) . to . be . at . most ( 40 ) ;
230+ } ) ;
224231 it ( 'should handle an empty array' , async function ( ) {
225232 expect ( await asyncmap ( [ ] , mapper , false ) ) . to . eql ( [ ] ) ;
226233 } ) ;
227234 it ( 'should handle an empty array in parallel' , async function ( ) {
228235 expect ( await asyncmap ( [ ] , mapper ) ) . to . eql ( [ ] ) ;
229236 } ) ;
237+ it ( 'should work for a sync mapper function' , async function ( ) {
238+ const syncmapper = ( el : number ) : number => el * 2 ;
239+ expect ( await asyncmap ( coll , syncmapper , false ) ) . to . eql ( newColl ) ;
240+ expect ( await asyncmap ( coll , syncmapper ) ) . to . eql ( newColl ) ;
241+ } ) ;
242+ it ( 'should raise an error if options is null' , async function ( ) {
243+ // @ts -expect-error - testing invalid inputs
244+ await expect ( asyncmap ( coll , mapper , null ) ) . to . be . rejectedWith (
245+ 'Options cannot be null'
246+ ) ;
247+ } ) ;
230248} ) ;
231249
232250describe ( 'asyncfilter' , function ( ) {
233251 const filter = async function ( el : number ) : Promise < boolean > {
234- await sleep ( 5 ) ;
252+ await sleep ( 10 ) ;
235253 return el % 2 === 0 ;
236254 } ;
237255 const coll = [ 1 , 2 , 3 , 4 , 5 ] ;
256+ const newColl = [ 2 , 4 ] ;
238257 it ( 'should filter elements one at a time' , async function ( ) {
239258 const start = Date . now ( ) ;
240- expect ( await asyncfilter ( coll , filter , false ) ) . to . eql ( [ 2 , 4 ] ) ;
241- expect ( Date . now ( ) - start ) . to . be . at . least ( 19 ) ;
259+ expect ( await asyncfilter ( coll , filter , false ) ) . to . eql ( newColl ) ;
260+ expect ( Date . now ( ) - start ) . to . be . at . least ( 50 ) ;
242261 } ) ;
243262 it ( 'should filter elements in parallel' , async function ( ) {
244263 const start = Date . now ( ) ;
245- expect ( await asyncfilter ( coll , filter ) ) . to . eql ( [ 2 , 4 ] ) ;
246- expect ( Date . now ( ) - start ) . to . be . below ( 9 ) ;
264+ expect ( await asyncfilter ( coll , filter ) ) . to . eql ( newColl ) ;
265+ expect ( Date . now ( ) - start ) . to . be . at . most ( 20 ) ;
247266 } ) ;
248- it ( 'should handle an empty array ' , async function ( ) {
267+ it ( 'should filter elements with concurrency ' , async function ( ) {
249268 const start = Date . now ( ) ;
269+ expect ( await asyncfilter ( coll , filter , { concurrency : 2 } ) ) . to . eql ( newColl ) ;
270+ expect ( Date . now ( ) - start ) . to . be . at . least ( 29 ) ;
271+ expect ( Date . now ( ) - start ) . to . be . at . most ( 40 ) ;
272+ } ) ;
273+ it ( 'should handle an empty array' , async function ( ) {
250274 expect ( await asyncfilter ( [ ] , filter , false ) ) . to . eql ( [ ] ) ;
251- expect ( Date . now ( ) - start ) . to . be . below ( 9 ) ;
252275 } ) ;
253276 it ( 'should handle an empty array in parallel' , async function ( ) {
254- const start = Date . now ( ) ;
255277 expect ( await asyncfilter ( [ ] , filter ) ) . to . eql ( [ ] ) ;
256- expect ( Date . now ( ) - start ) . to . be . below ( 9 ) ;
278+ } ) ;
279+ it ( 'should work for a sync filter function' , async function ( ) {
280+ const syncfilter = ( el : number ) : boolean => el % 2 === 0 ;
281+ expect ( await asyncfilter ( coll , syncfilter , false ) ) . to . eql ( newColl ) ;
282+ expect ( await asyncfilter ( coll , syncfilter ) ) . to . eql ( newColl ) ;
283+ } ) ;
284+ it ( 'should raise an error if options is null' , async function ( ) {
285+ // @ts -expect-error - testing invalid inputs
286+ await expect ( asyncfilter ( coll , filter , null ) ) . to . be . rejectedWith (
287+ 'Options cannot be null'
288+ ) ;
257289 } ) ;
258290} ) ;
0 commit comments