From b5a7f4ff7ef8613fb9922b4f9ca0ea4855547321 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 18 Sep 2019 13:37:39 -0400 Subject: [PATCH] Light: Modernizing the source (class syntax and other small changes) --- lib/light.js | 186 +++++++++++++++++++++------------------------- package-lock.json | 75 ++++++++++++------- package.json | 2 +- test/.jshintrc | 2 +- test/light.js | 74 +++++++++--------- 5 files changed, 172 insertions(+), 167 deletions(-) diff --git a/lib/light.js b/lib/light.js index 0746dc5a2..e56da7734 100644 --- a/lib/light.js +++ b/lib/light.js @@ -3,13 +3,12 @@ const EVS = require("./evshield"); const within = require("./mixins/within"); const { uint16, toFixed, scale } = require("./fn"); const Emitter = require("events"); -const util = require("util"); const priv = new Map(); const Controllers = { DEFAULT: { initialize: { - value(opts, dataHandler) { + value(options, dataHandler) { this.io.pinMode(this.pin, this.io.MODES.ANALOG); this.io.analogRead(this.pin, dataHandler); }, @@ -22,17 +21,17 @@ const Controllers = { }, EVS_EV3: { initialize: { - value(opts, dataHandler) { + value(options, dataHandler) { const state = priv.get(this); - if (opts.mode) { - opts.mode = opts.mode.toUpperCase(); + if (options.mode) { + options.mode = options.mode.toUpperCase(); } - state.mode = opts.mode === "REFLECTED" ? EVS.Type_EV3_LIGHT_REFLECTED : EVS.Type_EV3_LIGHT; + state.mode = options.mode === "REFLECTED" ? EVS.Type_EV3_LIGHT_REFLECTED : EVS.Type_EV3_LIGHT; - state.shield = EVS.shieldPort(opts.pin); - state.ev3 = new EVS(Object.assign(opts, { + state.shield = EVS.shieldPort(options.pin); + state.ev3 = new EVS(Object.assign(options, { io: this.io })); state.ev3.setup(state.shield, EVS.Type_EV3); @@ -51,23 +50,22 @@ const Controllers = { }, EVS_NXT: { initialize: { - value(opts, dataHandler) { + value(options, dataHandler) { const state = priv.get(this); - if (opts.mode) { - opts.mode = opts.mode.toUpperCase(); + if (options.mode) { + options.mode = options.mode.toUpperCase(); } - state.mode = opts.mode === "REFLECTED" ? EVS.Type_NXT_LIGHT_REFLECTED : EVS.Type_NXT_LIGHT; + state.mode = options.mode === "REFLECTED" ? EVS.Type_NXT_LIGHT_REFLECTED : EVS.Type_NXT_LIGHT; - state.shield = EVS.shieldPort(opts.pin); - state.ev3 = new EVS(Object.assign(opts, { + state.shield = EVS.shieldPort(options.pin); + state.ev3 = new EVS(Object.assign(options, { io: this.io })); state.ev3.setup(state.shield, state.mode); state.ev3.read(state.shield, state.shield.analog, EVS.Analog_Bytes, (data) => { - const value = data[0] | (data[1] << 8); - dataHandler(value); + dataHandler(data[0] | (data[1] << 8)); }); } }, @@ -91,13 +89,13 @@ const Controllers = { }, initialize: { - value(opts, dataHandler) { - const address = opts.address || 0x39; + value(options, dataHandler) { + const address = options.address || 0x39; const command = byte => byte | 0b10000000; - opts.address = address; + options.address = address; - this.io.i2cConfig(opts); + this.io.i2cConfig(options); // Page 15 // Control Register (0h) @@ -229,14 +227,14 @@ const Controllers = { let Tint = TintMs[TintIndex]; let lux = 0; - // if (typeof opts.gain !== "undefined") { + // if (typeof options.gain !== "undefined") { // isAutoGain = false; - // gain = opts.gain; + // gain = options.gain; // } - // if (typeof opts.integration !== "undefined") { + // if (typeof options.integration !== "undefined") { // isAutoGain = false; - // Tint = opts.integration; + // Tint = options.integration; // } @@ -395,19 +393,16 @@ const Controllers = { value: [0x23, 0x5C] }, initialize: { - value(opts, dataHandler) { - const address = opts.address || 0x23; - const mode = opts.mode || 0x10; - opts.address = address; - this.io.i2cConfig(opts); + value(options, dataHandler) { + const address = options.address || 0x23; + const mode = options.mode || 0x10; + options.address = address; + this.io.i2cConfig(options); this.io.i2cWrite(address, mode); const read = () => { setTimeout(() => { this.io.i2cReadOnce(address, 2, (data) => { - let raw = data[0]; - raw <<= 8; - raw |= data[1]; - dataHandler(raw); + dataHandler((data[0] << 8) | data[1]); read(); }); }, 120); @@ -439,91 +434,82 @@ Controllers.ALSPT19 = Controllers["ALS-PT19"] = Controllers.DEFAULT; * */ -function Light(opts) { - - if (!(this instanceof Light)) { - return new Light(opts); - } +class Light extends Emitter { + constructor(options) { + super(); - let controller = null; - let raw = 0; - let last = 0; - const freq = opts.freq || 25; + let controller = null; + let raw = 0; + let last = 0; + const freq = options.freq || 25; - Board.Component.call( - this, opts = Board.Options(opts) - ); + Board.Component.call( + this, options = Board.Options(options) + ); - if (typeof opts.controller === "string") { - controller = Controllers[opts.controller]; - } else { - controller = opts.controller || Controllers.DEFAULT; - } + if (typeof options.controller === "string") { + controller = Controllers[options.controller]; + } else { + controller = options.controller || Controllers.DEFAULT; + } - Board.Controller.call(this, controller, opts); + Board.Controller.call(this, controller, options); - if (!this.toIntensityLevel) { - this.toIntensityLevel = opts.toIntensityLevel || function(x) { - return x; - }; - } + if (!this.toIntensityLevel) { + this.toIntensityLevel = options.toIntensityLevel || (x => x); + } - if (!this.toLux) { - this.toLux = opts.toLux || function(x) { - return x; - }; - } + if (!this.toLux) { + this.toLux = options.toLux || (x => x); + } - Object.defineProperties(this, { - value: { - get() { - return raw; + Object.defineProperties(this, { + value: { + get() { + return raw; + }, }, - }, - level: { - get() { - return this.toIntensityLevel(raw); + level: { + get() { + return this.toIntensityLevel(raw); + }, }, - }, - }); + }); - priv.set(this, {}); + priv.set(this, {}); - /* istanbul ignore else */ - if (typeof this.initialize === "function") { - this.initialize(opts, (data) => { - raw = data; - }); - } + /* istanbul ignore else */ + if (typeof this.initialize === "function") { + this.initialize(options, data => raw = data); + } - if (typeof this.lux === "undefined") { - Object.defineProperty(this, "lux", { - get() { - return this.toLux(raw); - }, - }); - } + if (typeof this.lux === "undefined") { + Object.defineProperty(this, "lux", { + get() { + return this.toLux(raw); + }, + }); + } - const data = { - level: 0, - lux: 0, - }; + const data = { + level: 0, + lux: 0, + }; - setInterval(() => { - data.level = this.level; - data.lux = this.lux; + setInterval(() => { + data.level = this.level; + data.lux = this.lux; - this.emit("data", data); + this.emit("data", data); - if (raw !== last) { - last = raw; - this.emit("change", data); - } - }, freq); + if (raw !== last) { + last = raw; + this.emit("change", data); + } + }, freq); + } } -util.inherits(Light, Emitter); - Object.assign(Light.prototype, within); diff --git a/package-lock.json b/package-lock.json index ad6835aa7..9d405b57c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -719,9 +719,9 @@ }, "dependencies": { "glob": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha1-wZyd+aAocC1nhhI4SmVSQExjbRU=", + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", + "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", "dev": true, "requires": { "fs.realpath": "^1.0.0", @@ -1689,40 +1689,59 @@ } }, "grunt-contrib-jshint": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/grunt-contrib-jshint/-/grunt-contrib-jshint-1.1.0.tgz", - "integrity": "sha1-Np2QmyWTxA6L55lAshNAhQx5Oaw=", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/grunt-contrib-jshint/-/grunt-contrib-jshint-2.1.0.tgz", + "integrity": "sha512-65S2/C/6RfjY/umTxfwXXn+wVvaYmykHkHSsW6Q6rhkbv3oudTEgqnFFZvWzWCoHUb+3GMZLbP3oSrNyvshmIQ==", "dev": true, "requires": { - "chalk": "^1.1.1", + "chalk": "^2.4.2", "hooker": "^0.2.3", - "jshint": "~2.9.4" + "jshint": "~2.10.2" }, "dependencies": { "ansi-styles": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", - "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", - "dev": true + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } }, "chalk": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", - "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", "dev": true, "requires": { - "ansi-styles": "^2.2.1", - "escape-string-regexp": "^1.0.2", - "has-ansi": "^2.0.0", - "strip-ansi": "^3.0.0", - "supports-color": "^2.0.0" + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" } }, - "supports-color": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, + "requires": { + "color-name": "1.1.3" + } + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "dev": true + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } } } }, @@ -2460,16 +2479,16 @@ "dev": true }, "jshint": { - "version": "2.9.7", - "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.9.7.tgz", - "integrity": "sha512-Q8XN38hGsVQhdlM+4gd1Xl7OB1VieSuCJf+fEJjpo59JH99bVJhXRXAh26qQ15wfdd1VPMuDWNeSWoNl53T4YA==", + "version": "2.10.2", + "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.10.2.tgz", + "integrity": "sha512-e7KZgCSXMJxznE/4WULzybCMNXNAd/bf5TSrvVEq78Q/K8ZwFpmBqQeDtNiHc3l49nV4E/+YeHU/JZjSUIrLAA==", "dev": true, "requires": { "cli": "~1.0.0", "console-browserify": "1.1.x", "exit": "0.1.x", "htmlparser2": "3.8.x", - "lodash": "~4.17.10", + "lodash": "~4.17.11", "minimatch": "~3.0.2", "shelljs": "0.3.x", "strip-json-comments": "1.0.x" diff --git a/package.json b/package.json index f87b753e0..a22c7e618 100644 --- a/package.json +++ b/package.json @@ -170,7 +170,7 @@ "coveralls": "^3.0.2", "grunt": "^1.0.4", "grunt-cli": "^1.2.0", - "grunt-contrib-jshint": "^1.1.0", + "grunt-contrib-jshint": "^2.1.0", "grunt-contrib-nodeunit": "^2.0.0", "grunt-contrib-watch": "^1.1.0", "grunt-jsbeautifier": "^0.2.13", diff --git a/test/.jshintrc b/test/.jshintrc index 9cb02cf01..459ac6d08 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -1,4 +1,5 @@ { + "esversion": 7, "curly": true, "eqeqeq": true, "immed": true, @@ -11,7 +12,6 @@ "eqnull": true, "node": true, "strict": false, - "esnext": true, "quotmark": "double", "unused": true, "globals": { diff --git a/test/light.js b/test/light.js index 368142ddb..609a1a3d6 100644 --- a/test/light.js +++ b/test/light.js @@ -38,20 +38,20 @@ exports["Light"] = { board: this.board }); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); }, instanceof(test) { test.expect(1); - test.equal(Light({ board: this.board }) instanceof Light, true); + test.equal(new Light({ board: this.board }) instanceof Light, true); test.done(); }, @@ -132,13 +132,13 @@ exports["Light: ALSPT19"] = { shape(test) { test.expect(proto.length + instance.length); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); }, @@ -348,13 +348,13 @@ exports["Light: EVS_EV3, Ambient (Default)"] = { shape(test) { test.expect(proto.length + instance.length); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); }, @@ -456,13 +456,13 @@ exports["Light: EVS_EV3, Reflected"] = { shape(test) { test.expect(proto.length + instance.length); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); }, @@ -563,13 +563,13 @@ exports["Light: EVS_NXT, Ambient (Default)"] = { shape(test) { test.expect(proto.length + instance.length); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); }, @@ -670,13 +670,13 @@ exports["Light: EVS_NXT, Reflected"] = { shape(test) { test.expect(proto.length + instance.length); - proto.forEach(function(method) { - test.equal(typeof this.light[method.name], "function"); - }, this); + proto.forEach(({name}) => { + test.equal(typeof this.light[name], "function"); + }); - instance.forEach(function(property) { - test.notEqual(typeof this.light[property.name], 0); - }, this); + instance.forEach(({name}) => { + test.notEqual(typeof this.light[name], 0); + }); test.done(); },