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 9c69e3d commit 6688a4e
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/utils.js
Expand Up @@ -212,7 +212,28 @@ var isBuffer = function isBuffer(obj) {
};

var combine = function combine(a, b) {
return [].concat(a, 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) {
if (secondIsArray) {
a.push.apply(a, b);
} else {
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];
};

module.exports = {
Expand Down

0 comments on commit 6688a4e

Please sign in to comment.