From 28415f0b74650f641e0bdf50fe24c7a49f45304d Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 11 Mar 2015 12:31:45 -0700 Subject: [PATCH] Add basic #includes helper for arrays --- lib/adaptor.js | 2 +- lib/driver.js | 2 +- lib/registry.js | 5 +++-- lib/utils/helpers.js | 8 ++++++++ spec/lib/utils/helpers.spec.js | 12 ++++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/adaptor.js b/lib/adaptor.js index 6a2b7ff..bb393f4 100644 --- a/lib/adaptor.js +++ b/lib/adaptor.js @@ -35,7 +35,7 @@ var Adaptor = module.exports = function Adaptor(opts) { this.details = {}; _.each(opts, function(opt, name) { - if (!~["robot", "name", "adaptor", "events"].indexOf(name)) { + if (!_.includes(["robot", "name", "adaptor", "events"], name)) { this.details[name] = opt; } }, this); diff --git a/lib/driver.js b/lib/driver.js index abd8328..a574bfd 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -39,7 +39,7 @@ var Driver = module.exports = function Driver(opts) { _.each(opts, function(opt, name) { var banned = ["robot", "name", "connection", "driver", "events"]; - if (!~banned.indexOf(name)) { + if (!_.includes(banned, name)) { this.details[name] = opt; } }, this); diff --git a/lib/registry.js b/lib/registry.js index fa00c4f..e64c264 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -12,7 +12,8 @@ "use strict"; -var Logger = require("./logger"); +var Logger = require("./logger"), + _ = require("./utils/helpers"); // Explicitly these modules here, so Browserify can grab them later require("./test/loopback"); @@ -102,7 +103,7 @@ var Registry = module.exports = { for (var name in this.data) { var repo = this.data[name]; - if (repo[entry] && ~repo[entry].indexOf(value)) { + if (repo[entry] && _.includes(repo[entry], value)) { return repo.module; } } diff --git a/lib/utils/helpers.js b/lib/utils/helpers.js index 78fd016..b84c4a1 100644 --- a/lib/utils/helpers.js +++ b/lib/utils/helpers.js @@ -202,3 +202,11 @@ extend(H, { partial: partial, partialRight: partialRight }); + +function includes(arr, value) { + return !!~arr.indexOf(value); +} + +extend(H, { + includes: includes +}); diff --git a/spec/lib/utils/helpers.spec.js b/spec/lib/utils/helpers.spec.js index 4ee7b12..977c499 100644 --- a/spec/lib/utils/helpers.spec.js +++ b/spec/lib/utils/helpers.spec.js @@ -277,4 +277,16 @@ describe("Helpers", function() { 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); + }); + }); });