Skip to content

Commit

Permalink
Merge pull request #21 from rrichardson/master
Browse files Browse the repository at this point in the history
Filter no longer creates a sparse array as a result when passed an array
  • Loading branch information
mmalecki committed Jan 12, 2013
2 parents 3faea79 + 1d2b265 commit 3cb7104
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
npm-debug.log
*.swp
*.swo
24 changes: 17 additions & 7 deletions lib/index.js
Expand Up @@ -327,13 +327,23 @@ utile.randomString = function (length) {
// the predicate `pred`
//
utile.filter = function (obj, pred) {
var copy = Array.isArray(obj) ? [] : {};
utile.each(obj, function (val, key) {
if (pred(val, key, obj)) {
copy[key] = val;
}
});

var copy;
if (Array.isArray(obj)) {
copy = [];
utile.each(obj, function (val, key) {
if (pred(val, key, obj)) {
copy.push(val);
}
});
}
else {
copy = {};
utile.each(obj, function (val, key) {
if (pred(val, key, obj)) {
copy[key] = val;
}
});
}
return copy;
};

Expand Down

0 comments on commit 3cb7104

Please sign in to comment.