Skip to content

Commit

Permalink
[Refactor] new implementation for utils.combine
Browse files Browse the repository at this point in the history
  • Loading branch information
elidoran authored and ljharb committed Dec 22, 2016
1 parent 1c18b26 commit 06e84e6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,22 @@ exports.isBuffer = function (obj) {
};

exports.combine = function (a, b) {
return [].concat(a).concat(b);
// we always use both of these, so, let's calculate them now
var firstIsArray = Array.isArray(a);
var secondIsArray = Array.isArray(b);

// mutate `a` to append `b` and then return it
if (firstIsArray) {
secondIsArray ? a.push.apply(a, b) : a.push(b);
return a;
}

// mutate `b` to prepend `a` and then return it
if (secondIsArray) {
b.unshift(a);
return b;
}

// neither are arrays, so, create a new array with both
return [a, b];
};

0 comments on commit 06e84e6

Please sign in to comment.