node-bauer-factory
General utilities for nodejs.
Installation
npm install bauer-factory
Usage
var factory = require("bauer-factory");
.type
Returned value can be string, number, boolean, date, regexp, error, array, object, arguments, null, undefined or function.
var type = factory.type(arg);
.isNull
factory.isNull(arg)
factory.type(arg) === "null"
.isDefined
factory.isDefined(arg)
factory.type(arg) !== "undefined"
.isUndefined
factory.isUndefined(arg)
factory.type(arg) === "undefined"
.isDate
factory.isDate(arg)
factory.type(arg) === "date"
.isError
factory.isError(arg)
factory.type(arg) === "error"
.isBoolean
factory.isBoolean(arg)
factory.type(arg) === "boolean"
.isArray
factory.isArray(arg)
factory.type(arg) === "array"
.isNumber
factory.isNumber(arg)
factory.type(arg) === "number"
.isString
factory.isString(arg)
factory.type(arg) === "string"
.isObject
factory.isObject(arg)
factory.type(arg) === "object"
.isRegExp
factory.isRegExp(arg)
factory.type(arg) === "regexp"
.isFunction
factory.isFunction(arg)
factory.type(arg) === "function"
.isArguments
factory.isArguments(arg)
factory.type(arg) === "arguments"
.createMethod
Accepts an object containing types/lengths as keys and values as functions.
var func = factory.createMethod({
0: function() {},
s: function(s) {},
sf: function(s) {},
_: function() {},
});
Letters are taken as the first character of the argument's type as returned by factory.type
. Any combination can be used to route the function execution. This takes priority over argument's length routing.
var func = factory.createMethod({
o: function() {},
a: function(s) {},
sffb: function(s,f0,f1,b) {},
});
Numbers are taken as the length of the arguments object. Nested rules are supported.
var func = factory.createMethod({
5: {
sssss: function() {},
assss: function() {},
},
1: function(arg) {},
});
Underscore holds the default code. If no rule is matched and there's no _
throws an ReferenceError
.
var func = factory.createMethod({
_: function() {},
});
Strings can be used as code. They are converted to functions internally with the defined arguments.
var func = factory.createMethod({
s: "return this.get(s)",
ss: "return this.both(s0,s1)",
f: "this.on('ready',f)",
});
If the code does not use any external vars its possible to optimize the generated function by passing a second argument as true
.
var optimized = factory.createMethod({
s: "return this.get(s)",
ss: "return this.both(s0,s1)",
f: "this.on('ready',f)",
},true);
.createClass
Creates a class with given methods, constructor and inheritance.
var Bauer = factory.createClass({
inherits: require("events").EventEmitter,
constructor: function() {
},
killTerrorists: {
},
tortureSuspects: {
},
doWhateverIsNecessary: function() {},
});
The created class can be instantiated and inherited just like any other class.
var jack = new Bauer();
jack.killTerrorists();
jack.tortureSuspects();
jack.doWhateverIsNecessary();
.createObject
Creates a class just like .class does and returns an instance of it.
var jack = factory.createObject({
inherits: require("events").EventEmitter,
constructor: function() {
},
killTerrorists: {
s: function() {},
n: function() {},
},
tortureSuspects: {
1: function() {},
2: function() {},
},
doWhateverIsNecessary: function() {},
});