Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
buster.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cjohansen committed Jun 7, 2012
1 parent 567b0c4 commit bd11b8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/buster-core.js
Expand Up @@ -182,6 +182,32 @@ var buster = (function (setTimeout, B) {
};
}

if (Array.prototype.filter) {
buster.filter = function (arr, fn, thisp) {
return arr.filter(fn, thisp);
};
} else {
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
buster.filter = function (fn, thisp) {
"use strict";
if (this == null) { throw new TypeError(); }

var t = Object(this);
var len = t.length >>> 0;
if (typeof fn != "function") { throw new TypeError(); }

var res = [];
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
if (fn.call(thisp, val, i, t)) { res.push(val); }
}
}

return res;
};
}

if (isNode) {
module.exports = buster;
buster.eventEmitter = require("./buster-event-emitter");
Expand Down
9 changes: 9 additions & 0 deletions test/buster-core-test.js
Expand Up @@ -246,6 +246,15 @@
}
});

bu.testCase("BusterFilterTest", {
"should filter items": function () {
function isBigEnough(element) { return (element >= 10); }
var filtered = B.filter([12, 5, 8, 130, 44], isBigEnough);

assert.deepEqual(filtered, [12, 130, 44]);
}
});

bu.testCase("BusterParallelTest", {
setUp: function () {
this.fns = [sinon.stub(), sinon.stub(), sinon.stub()];
Expand Down

0 comments on commit bd11b8e

Please sign in to comment.