UNPKG

johnny-five

Version:

The JavaScript Robotics and Hardware Programming Framework. Use with: Arduino (all models), Electric Imp, Beagle Bone, Intel Galileo & Edison, Linino One, Pinoccio, pcDuino3, Raspberry Pi, Particle/Spark Core & Photon, Tessel 2, TI Launchpad and more!

39 lines (31 loc) 730 B
const Emitter = require("./emitter"); class Withinable extends Emitter { constructor() { super(); } within(range, unit, callback) { let upper; if (typeof range === "number") { upper = range; range = [0, upper]; } if (!Array.isArray(range)) { throw new Error("within expected a range array"); } if (typeof unit === "function") { callback = unit; unit = "value"; } if (typeof this[unit] === "undefined") { return this; } this.on("data", () => { const value = this[unit]; if (value >= range[0] && value <= range[1]) { callback.call(this, null, value); } }); return this; } } module.exports = Withinable;