Skip to content

Commit

Permalink
$.fn.map: ensuring callbacks that return an array of elements are sup…
Browse files Browse the repository at this point in the history
…ported too
  • Loading branch information
fabiospampinato committed Dec 30, 2019
1 parent 9c571dc commit 52da7f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/collection/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ interface Cash {

fn.map = function ( this: Cash, callback: MapCallback<EleLoose> ) {

return cash ( map.call ( this, ( ele: EleLoose, i: number ) => callback.call ( ele, i, ele ) ) );
return cash ( concat.apply ( [], map.call ( this, ( ele: EleLoose, i: number ) => callback.call ( ele, i, ele ) ) ) );

};
2 changes: 1 addition & 1 deletion src/core/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const doc = document,
tbody = createElement ( 'tbody' ),
tr = createElement ( 'tr' ),
{isArray, prototype: ArrayPrototype} = Array,
{filter, indexOf, map, push, slice, some, splice} = ArrayPrototype;
{concat, filter, indexOf, map, push, slice, some, splice} = ArrayPrototype;

const idRe = /^#[\w-]*$/,
classRe = /^\.[\w-]*$/,
Expand Down
14 changes: 14 additions & 0 deletions test/modules/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ describe ( 'Collection', { beforeEach: getFixtureInit ( fixture ) }, function ()

});

it ( 'supports callbacks that returns multiple elements', function ( t ) {

var mapped = $('<div>').map ( function () {

return [document.head, document.body];

});

t.is ( mapped.length, 2 );
t.true ( !!mapped.get ().find ( e => e === document.head ) );
t.true ( !!mapped.get ().find ( e => e === document.body ) );

});

});

describe ( '$.fn.slice', function ( it ) {
Expand Down

0 comments on commit 52da7f9

Please sign in to comment.