Add basic #includes helper for arrays

This commit is contained in:
Andrew Stewart 2015-03-11 12:31:45 -07:00
parent 6c5208577a
commit 28415f0b74
5 changed files with 25 additions and 4 deletions

View File

@ -35,7 +35,7 @@ var Adaptor = module.exports = function Adaptor(opts) {
this.details = {}; this.details = {};
_.each(opts, function(opt, name) { _.each(opts, function(opt, name) {
if (!~["robot", "name", "adaptor", "events"].indexOf(name)) { if (!_.includes(["robot", "name", "adaptor", "events"], name)) {
this.details[name] = opt; this.details[name] = opt;
} }
}, this); }, this);

View File

@ -39,7 +39,7 @@ var Driver = module.exports = function Driver(opts) {
_.each(opts, function(opt, name) { _.each(opts, function(opt, name) {
var banned = ["robot", "name", "connection", "driver", "events"]; var banned = ["robot", "name", "connection", "driver", "events"];
if (!~banned.indexOf(name)) { if (!_.includes(banned, name)) {
this.details[name] = opt; this.details[name] = opt;
} }
}, this); }, this);

View File

@ -12,7 +12,8 @@
"use strict"; "use strict";
var Logger = require("./logger"); var Logger = require("./logger"),
_ = require("./utils/helpers");
// Explicitly these modules here, so Browserify can grab them later // Explicitly these modules here, so Browserify can grab them later
require("./test/loopback"); require("./test/loopback");
@ -102,7 +103,7 @@ var Registry = module.exports = {
for (var name in this.data) { for (var name in this.data) {
var repo = this.data[name]; var repo = this.data[name];
if (repo[entry] && ~repo[entry].indexOf(value)) { if (repo[entry] && _.includes(repo[entry], value)) {
return repo.module; return repo.module;
} }
} }

View File

@ -202,3 +202,11 @@ extend(H, {
partial: partial, partial: partial,
partialRight: partialRight partialRight: partialRight
}); });
function includes(arr, value) {
return !!~arr.indexOf(value);
}
extend(H, {
includes: includes
});

View File

@ -277,4 +277,16 @@ describe("Helpers", function() {
expect(fn).to.be.calledWith("one", "two", "three"); expect(fn).to.be.calledWith("one", "two", "three");
}); });
}); });
describe("#includes", function() {
it("checks if an array includes a value", function() {
var fn = _.includes;
var arr = [1, "2", 3];
expect(fn(arr, 1)).to.be.eql(true);
expect(fn(arr, "2")).to.be.eql(true);
expect(fn(arr, {})).to.be.eql(false);
});
});
}); });