210 fix merge files
This commit is contained in:
parent
642c5c8b1e
commit
0067f33505
227
lib/cylon.js
227
lib/cylon.js
|
@ -6,67 +6,34 @@
|
|||
* Licensed under the Apache 2.0 license.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
<<<<<<< HEAD
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var Cylon,
|
||||
__slice = [].slice,
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
=======
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
|
||||
require('./config');
|
||||
require('./utils');
|
||||
require('./logger');
|
||||
require('./driver');
|
||||
require('./adaptor');
|
||||
require('./config');
|
||||
|
||||
Logger.setup();
|
||||
require('./utils');
|
||||
|
||||
// Cylon is the global namespace for the project, and also in charge of
|
||||
// maintaining the Master singleton that controls all the robots.
|
||||
var Cylon = (function() {
|
||||
function Cylon() {}
|
||||
require('./logger');
|
||||
|
||||
var instance = null;
|
||||
require('./driver');
|
||||
|
||||
require('./adaptor');
|
||||
|
||||
// Public: Fetches singleton instance of Master, or creates a new one if it
|
||||
// doesn't already exist
|
||||
//
|
||||
// Returns a Master instance
|
||||
Cylon.getInstance = function() {
|
||||
return new Master();
|
||||
};
|
||||
Logger.setup();
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Cylon is the global namespace for the project, and also in charge of
|
||||
// maintaining the Master singleton that controls all the robots.
|
||||
Cylon = (function() {
|
||||
var Master, instance;
|
||||
=======
|
||||
var bind = function(fn, me) {
|
||||
return function() { return fn.apply(me, arguments); }
|
||||
}
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
|
||||
// The Master class is our puppeteer that manages all the robots, as well as
|
||||
// starting them and the API.
|
||||
var Master = (function() {
|
||||
var robots = [],
|
||||
api = null,
|
||||
api_config = { host: '127.0.0.1', port: '3000' };
|
||||
function Cylon() {}
|
||||
|
||||
// Public: Creates a new Master
|
||||
//
|
||||
// Returns a Master instance
|
||||
function Master() {
|
||||
this.robot = bind(this.robot, this);
|
||||
instance = null;
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Public: Fetches singleton instance of Master, or creates a new one if it
|
||||
// doesn't already exist
|
||||
//
|
||||
|
@ -115,22 +82,9 @@ var Cylon = (function() {
|
|||
process.on("SIGINT", function() {
|
||||
Cylon.getInstance().stop();
|
||||
return process.kill();
|
||||
=======
|
||||
this.self = this;
|
||||
|
||||
if (process.platform === "win32") {
|
||||
var readline = require("readline");
|
||||
|
||||
var rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
});
|
||||
|
||||
rl.on("SIGINT", function() { return process.emit("SIGINT"); });
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Public: Creates a new Robot
|
||||
//
|
||||
// opts - hash of Robot attributes
|
||||
|
@ -285,126 +239,10 @@ var Cylon = (function() {
|
|||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
||||
robot = robots[_i];
|
||||
_results.push(robot.stop());
|
||||
=======
|
||||
process.on("SIGINT", function() {
|
||||
Cylon.getInstance().stop();
|
||||
process.kill();
|
||||
});
|
||||
}
|
||||
|
||||
// Public: Creates a new Robot
|
||||
//
|
||||
// opts - hash of Robot attributes
|
||||
//
|
||||
// Returns a shiny new Robot
|
||||
// Examples:
|
||||
// Cylon.robot
|
||||
// connection: { name: 'arduino', adaptor: 'firmata' }
|
||||
// device: { name: 'led', driver: 'led', pin: 13 }
|
||||
//
|
||||
// work: (me) ->
|
||||
// me.led.toggle()
|
||||
Master.prototype.robot = function(opts) {
|
||||
var Robot = require("./robot");
|
||||
opts.master = this;
|
||||
|
||||
var robot = new Robot(opts);
|
||||
|
||||
robots.push(robot);
|
||||
return robot;
|
||||
};
|
||||
|
||||
// Public: Returns all Robots the Master knows about
|
||||
//
|
||||
// Returns an array of all Robot instances
|
||||
Master.prototype.robots = function() {
|
||||
return robots;
|
||||
};
|
||||
|
||||
// Public: Configures the API host and port based on passed options
|
||||
//
|
||||
// opts - object containing API options
|
||||
// host - host address API should serve from
|
||||
// port - port API should listen for requests on
|
||||
//
|
||||
// Returns the API configuration
|
||||
Master.prototype.api = function(opts) {
|
||||
if (opts == null) { opts = {}; }
|
||||
|
||||
api_config.host = opts.host || "127.0.0.1";
|
||||
api_config.port = opts.port || "3000";
|
||||
|
||||
return api_config;
|
||||
};
|
||||
|
||||
// Public: Finds a particular robot by name
|
||||
//
|
||||
// name - name of the robot to find
|
||||
// callback - optional callback to be executed
|
||||
//
|
||||
// Returns the found Robot or result of the callback if it's supplied
|
||||
Master.prototype.findRobot = function(name, callback) {
|
||||
var error,
|
||||
robot = null;
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var bot = robots[i];
|
||||
if (bot.name === name) { robot = bot; }
|
||||
}
|
||||
|
||||
if (robot == null) {
|
||||
error = { error: "No Robot found with the name " + name };
|
||||
}
|
||||
|
||||
return callback ? callback(error, robot) : robot;
|
||||
};
|
||||
|
||||
// Public: Finds a particular Robot's device by name
|
||||
//
|
||||
// robotid - name of the robot to find
|
||||
// deviceid - name of the device to find
|
||||
// callback - optional callback to be executed
|
||||
//
|
||||
// Returns the found Device or result of the callback if it's supplied
|
||||
Master.prototype.findRobotDevice = function(robotid, deviceid, callback) {
|
||||
return this.findRobot(robotid, function(err, robot) {
|
||||
var error,
|
||||
device = null;
|
||||
|
||||
if (err) { return callback(err, robot); }
|
||||
|
||||
if (robot.devices[deviceid]) { device = robot.devices[deviceid]; }
|
||||
|
||||
if (device == null) {
|
||||
error = { error: "No device found with the name " + deviceid + "." };
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return callback ? callback(error, device) : device;
|
||||
});
|
||||
};
|
||||
|
||||
// Public: Finds a particular Robot's connection by name
|
||||
//
|
||||
// robotid - name of the robot to find
|
||||
// connid - name of the device to find
|
||||
// callback - optional callback to be executed
|
||||
//
|
||||
// Returns the found Connection or result of the callback if it's supplied
|
||||
Master.prototype.findRobotConnection = function(robotid, connid, callback) {
|
||||
return this.findRobot(robotid, function(err, robot) {
|
||||
var error,
|
||||
connection = null;
|
||||
|
||||
if (err) { return callback(err, robot); }
|
||||
|
||||
if (robot.connections[connid]) { connection = robot.connections[connid]; }
|
||||
|
||||
if (connection == null) {
|
||||
error = { error: "No connection found with the name " + connid + "." };
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Creates a new instance of the Cylon API server, or returns the
|
||||
// already-existing one.
|
||||
//
|
||||
|
@ -415,50 +253,15 @@ var Cylon = (function() {
|
|||
api_config.master = this.self;
|
||||
return api != null ? api : api = new server(api_config);
|
||||
};
|
||||
=======
|
||||
return callback ? callback(error, connection) : connection;
|
||||
});
|
||||
};
|
||||
|
||||
// Public: Starts up the API and the robots
|
||||
//
|
||||
// Returns nothing
|
||||
Master.prototype.start = function() {
|
||||
this.startAPI();
|
||||
return Master;
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
robot.start();
|
||||
}
|
||||
};
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
})();
|
||||
|
||||
// Public: Stops the API and the robots
|
||||
//
|
||||
// Returns nothing
|
||||
Master.prototype.stop = function() {
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
robot.stop();
|
||||
}
|
||||
};
|
||||
return Cylon;
|
||||
|
||||
// Creates a new instance of the Cylon API server, or returns the
|
||||
// already-existing one.
|
||||
//
|
||||
// Returns an Cylon.ApiServer instance
|
||||
Master.prototype.startAPI = function() {
|
||||
var Server = require('./api');
|
||||
api_config.master = this.self;
|
||||
return api != null ? api : api = new Server(api_config);
|
||||
};
|
||||
}).call(this);
|
||||
|
||||
return Master;
|
||||
|
||||
})();
|
||||
|
||||
return Cylon;
|
||||
module.exports = Cylon.getInstance();
|
||||
|
||||
}).call(this);
|
||||
|
||||
module.exports = Cylon.getInstance();
|
||||
|
|
163
lib/logger.js
163
lib/logger.js
|
@ -7,22 +7,13 @@
|
|||
*/
|
||||
|
||||
|
||||
|
||||
(function() {
|
||||
var BasicLogger, NullLogger,
|
||||
__slice = [].slice;
|
||||
|
||||
"use strict";
|
||||
|
||||
var getArgs = function(args) {
|
||||
return args.length >= 1 ? [].slice.call(args, 0) : [];
|
||||
}
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
|
||||
// The Logger is a global object to facilitate logging stuff to the console (or
|
||||
// other output) easily and consistently. It's available anywhere in Cylon, as
|
||||
// well as in external modules that are loaded into Cylon
|
||||
<<<<<<< HEAD
|
||||
// The Logger is a global object to facilitate logging stuff to the console (or
|
||||
// other output) easily and consistently. It's available anywhere in Cylon, as
|
||||
// well as in external modules that are loaded into Cylon
|
||||
global.Logger = {
|
||||
|
||||
// Public: Creates a Logger instance and assigns it to @logger
|
||||
|
@ -64,133 +55,71 @@ var getArgs = function(args) {
|
|||
var args, _ref;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return (_ref = this.logger).fatal.apply(_ref, args);
|
||||
=======
|
||||
global.Logger = {
|
||||
|
||||
// Public: Creates a Logger instance and assigns it to @logger
|
||||
//
|
||||
// logger - logger object to use. Defaults to a BasicLogger, or a NullLogger if
|
||||
// false is supplied
|
||||
//
|
||||
// Returns the new logger instance
|
||||
setup: function(logger) {
|
||||
if (logger == null) { logger = new BasicLogger; }
|
||||
|
||||
if (logger === false) {
|
||||
this.logger = new NullLogger();
|
||||
} else {
|
||||
this.logger = logger;
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
};
|
||||
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
|
||||
BasicLogger = (function() {
|
||||
function BasicLogger() {}
|
||||
=======
|
||||
return this.logger
|
||||
},
|
||||
|
||||
toString: function() { return this.logger.toString(); },
|
||||
BasicLogger.prototype.toString = function() {
|
||||
return "BasicLogger";
|
||||
};
|
||||
|
||||
debug: function() {
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.debug.apply(this.logger, args);
|
||||
},
|
||||
BasicLogger.prototype.debug = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"].concat(__slice.call(args)));
|
||||
};
|
||||
|
||||
info: function() {
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.info.apply(this.logger, args);
|
||||
},
|
||||
BasicLogger.prototype.info = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["I, [" + (new Date().toISOString()) + "] INFO -- :"].concat(__slice.call(args)));
|
||||
};
|
||||
|
||||
warn: function() {
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.warn.apply(this.logger, args);
|
||||
},
|
||||
BasicLogger.prototype.warn = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["W, [" + (new Date().toISOString()) + "] WARN -- :"].concat(__slice.call(args)));
|
||||
};
|
||||
|
||||
error: function() {
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.error.apply(this.logger, args);
|
||||
},
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
BasicLogger.prototype.error = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["E, [" + (new Date().toISOString()) + "] ERROR -- :"].concat(__slice.call(args)));
|
||||
};
|
||||
|
||||
fatal: function() {
|
||||
var args = getArgs(arguments);
|
||||
return this.logger.fatal.apply(this.logger, args);
|
||||
}
|
||||
};
|
||||
BasicLogger.prototype.fatal = function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return console.log.apply(console, ["F, [" + (new Date().toISOString()) + "] FATAL -- :"].concat(__slice.call(args)));
|
||||
};
|
||||
|
||||
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
|
||||
var BasicLogger = (function() {
|
||||
function BasicLogger() {}
|
||||
return BasicLogger;
|
||||
|
||||
BasicLogger.prototype.toString = function() { return "BasicLogger"; };
|
||||
|
||||
BasicLogger.prototype.debug = function() {
|
||||
var args = getArgs(arguments),
|
||||
string = ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.info = function() {
|
||||
var args = getArgs(arguments),
|
||||
string = ["I, [" + (new Date().toISOString()) + "] INFO -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
|
||||
BasicLogger.prototype.warn = function() {
|
||||
var args = getArgs(arguments),
|
||||
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
|
||||
data = string.concat(args.slice());
|
||||
|
||||
<<<<<<< HEAD
|
||||
})();
|
||||
|
||||
// The NullLogger is designed for cases where you want absolutely nothing to
|
||||
// print to anywhere. Every proxied method from the Logger returns a noo
|
||||
// print to anywhere. Every proxied method from the Logger returns a noop.
|
||||
NullLogger = (function() {
|
||||
function NullLogger() {}
|
||||
=======
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
|
||||
BasicLogger.prototype.error = function() {
|
||||
var args = getArgs(arguments),
|
||||
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
|
||||
data = string.concat(args.slice());
|
||||
NullLogger.prototype.toString = function() {
|
||||
return "NullLogger";
|
||||
};
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
NullLogger.prototype.debug = function() {};
|
||||
|
||||
BasicLogger.prototype.fatal = function() {
|
||||
var args = getArgs(arguments),
|
||||
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
|
||||
data = string.concat(args.slice());
|
||||
NullLogger.prototype.info = function() {};
|
||||
|
||||
return console.log.apply(console, data);
|
||||
};
|
||||
NullLogger.prototype.warn = function() {};
|
||||
|
||||
return BasicLogger;
|
||||
NullLogger.prototype.error = function() {};
|
||||
|
||||
})();
|
||||
NullLogger.prototype.fatal = function() {};
|
||||
|
||||
// The NullLogger is designed for cases where you want absolutely nothing to
|
||||
// print to anywhere. Every proxied method from the Logger returns a noop.
|
||||
var NullLogger = (function() {
|
||||
function NullLogger() {}
|
||||
return NullLogger;
|
||||
|
||||
NullLogger.prototype.toString = function() { return "NullLogger"; };
|
||||
})();
|
||||
|
||||
NullLogger.prototype.debug = function() {};
|
||||
NullLogger.prototype.info = function() {};
|
||||
NullLogger.prototype.warn = function() {};
|
||||
NullLogger.prototype.error = function() {};
|
||||
NullLogger.prototype.fatal = function() {};
|
||||
|
||||
return NullLogger;
|
||||
|
||||
})();
|
||||
}).call(this);
|
||||
|
|
211
lib/utils.js
211
lib/utils.js
|
@ -7,7 +7,6 @@
|
|||
*/
|
||||
|
||||
|
||||
|
||||
(function() {
|
||||
var __slice = [].slice;
|
||||
|
||||
|
@ -124,114 +123,19 @@
|
|||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
return target[method].apply(target, args);
|
||||
};
|
||||
=======
|
||||
// Public: Alias to setInterval, combined with Number monkeypatches below to
|
||||
// create an artoo-like syntax.
|
||||
//
|
||||
// interval - interval to run action on
|
||||
// action - action to perform at interval
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// every 5.seconds(), -> console.log("hello world (and again in 5 seconds)!")
|
||||
//
|
||||
// Returns an interval
|
||||
global.every = function(interval, action) {
|
||||
return setInterval(action, interval);
|
||||
};
|
||||
|
||||
// Public: Alias to setTimeout, combined with Number monkeypatches below to
|
||||
// create an artoo-like syntax.
|
||||
//
|
||||
// interval - interval to run action on
|
||||
// action - action to perform at interval
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// after 10.seconds(), -> console.log("hello world from ten seconds ago!")
|
||||
//
|
||||
// Returns an interval
|
||||
global.after = function(delay, action) {
|
||||
return setTimeout(action, delay);
|
||||
};
|
||||
|
||||
// Public: Alias to the `every` function, but passing 0
|
||||
// Examples
|
||||
//
|
||||
// constantly -> console.log("hello world (and again and again)!")
|
||||
//
|
||||
// Returns an interval
|
||||
global.constantly = function(action) {
|
||||
return every(0, action);
|
||||
};
|
||||
|
||||
// Public: Sleep - do nothing for some duration of time.
|
||||
//
|
||||
// ms - number of ms to sleep for
|
||||
//
|
||||
// Returns a function
|
||||
// Examples:
|
||||
// sleep 1.second()
|
||||
global.sleep = function(ms) {
|
||||
var start = Date.now();
|
||||
|
||||
while(Date.now() < start + ms) {
|
||||
var i = 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Copies
|
||||
global.slice = [].slice;
|
||||
global.hasProp = {}.hasOwnProperty;
|
||||
|
||||
// Public: Function to use for class inheritance. Copy of a CoffeeScript helper
|
||||
// function.
|
||||
//
|
||||
// Example
|
||||
//
|
||||
// var Sphero = (function(klass) {
|
||||
// subclass(Sphero, klass);
|
||||
// // Sphero is now a subclass of Parent, and can access it's methods through
|
||||
// // Sphero.__super__
|
||||
// })(Parent);
|
||||
//
|
||||
// Returns subclass
|
||||
global.subclass = function(child, parent) {
|
||||
var ctor = function() { this.constructor = child; };
|
||||
|
||||
for (var key in parent) {
|
||||
if (hasProp.call(parent, key)) { child[key] = parent[key]; }
|
||||
}
|
||||
|
||||
ctor.prototype = parent.prototype;
|
||||
child.prototype = new ctor();
|
||||
child.__super__ = parent.prototype;
|
||||
return child;
|
||||
};
|
||||
|
||||
// Public: Proxies a list of methods from one object to another. It will not
|
||||
// overwrite existing methods unless told to.
|
||||
//
|
||||
// methods - array of functions to proxy
|
||||
// target - object to proxy the functions to
|
||||
// base - (optional) object that proxied functions will be declared on. Defaults
|
||||
// to this
|
||||
// force - (optional) boolean - whether or not to force method assignment
|
||||
//
|
||||
// Returns base
|
||||
global.proxyFunctionsToObject = function(methods, target, base, force) {
|
||||
if (base == null) { base = this; }
|
||||
if (force == null) { force = false; }
|
||||
|
||||
var fn = function(method) {
|
||||
return base[method] = function() {
|
||||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
||||
return target[method].apply(target, args);
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
};
|
||||
for (_i = 0, _len = methods.length; _i < _len; _i++) {
|
||||
method = methods[_i];
|
||||
if (!force) {
|
||||
if (typeof base[method] === 'function') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_fn(method);
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Public: Proxies a list of methods for test stubbing.
|
||||
//
|
||||
// methods - array of functions to proxy
|
||||
|
@ -317,98 +221,5 @@ global.proxyFunctionsToObject = function(methods, target, base, force) {
|
|||
Number.prototype.toScale = function(start, end) {
|
||||
return Math.ceil(this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end));
|
||||
};
|
||||
=======
|
||||
for (var i = 0; i < methods.length; i++) {
|
||||
var method = methods[i];
|
||||
if (!force) {
|
||||
if (typeof base[method] === 'function') { continue; }
|
||||
}
|
||||
>>>>>>> c39f448d14c27cdd9a1524a11c57a5c01a345b3c
|
||||
|
||||
fn(method);
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
// Public: Proxies a list of methods for test stubbing.
|
||||
//
|
||||
// methods - array of functions to proxy
|
||||
// base - (optional) object that proxied functions will be declared on. Defaults
|
||||
// to this
|
||||
//
|
||||
// Returns base
|
||||
global.proxyTestStubs = function(methods, base) {
|
||||
if (base == null) { base = this; }
|
||||
|
||||
for (var i = 0; i < methods.length; i++) {
|
||||
var method = methods[i];
|
||||
|
||||
base[method] = function() { return true; };
|
||||
base.commandList.push(method);
|
||||
}
|
||||
|
||||
return base;
|
||||
};
|
||||
|
||||
// Public: Monkey-patches Number to have Rails-like //seconds() function. Warning,
|
||||
// due to the way the Javascript parser works, applying functions on numbers is
|
||||
// kind of weird. See examples for details.
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 2.seconds()
|
||||
// //=> SyntaxError: Unexpected token ILLEGAL
|
||||
//
|
||||
// 10..seconds()
|
||||
// //=> 10000
|
||||
//
|
||||
// (5).seconds()
|
||||
// //=> 5000
|
||||
//
|
||||
// Returns an integer representing time in milliseconds
|
||||
Number.prototype.seconds = function() {
|
||||
return this * 1000;
|
||||
};
|
||||
|
||||
// Public: Alias for Number::seconds, see comments for that method
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 1.second()
|
||||
// //=> 1000
|
||||
//
|
||||
// Returns an integer representing time in milliseconds
|
||||
Number.prototype.second = function() {
|
||||
return this.seconds(this);
|
||||
};
|
||||
|
||||
// Public: Convert value from old scale (start, end) to (0..1) scale
|
||||
//
|
||||
// start - low point of scale to convert value from
|
||||
// end - high point of scale to convert value from
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 5..fromScale(0, 10)
|
||||
// //=> 0.5
|
||||
//
|
||||
// Returns an integer representing the scaled value
|
||||
Number.prototype.fromScale = function(start, end) {
|
||||
return (this - Math.min(start, end)) / (Math.max(start, end) - Math.min(start, end));
|
||||
};
|
||||
|
||||
// Public: Convert value from (0..1) scale to new (start, end) scale
|
||||
//
|
||||
// start - low point of scale to convert value to
|
||||
// end - high point of scale to convert value to
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 0.5.toScale(0, 10)
|
||||
// //=> 5
|
||||
//
|
||||
// Returns an integer representing the scaled value
|
||||
Number.prototype.toScale = function(start, end) {
|
||||
|
||||
return Math.ceil(this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end));
|
||||
};
|
||||
}).call(this);
|
||||
|
|
Loading…
Reference in New Issue