Comparing version 0.0.4 to 0.1.0
@@ -21,3 +21,3 @@ var util = require("util"); | ||
/** | ||
Gets/sets the option value. Will attempt to convert values to Number for definitions of `type` "number". | ||
Gets/sets the property value. Will attempt to convert values to Number for definitions of `type` "number". | ||
@property value | ||
@@ -105,73 +105,18 @@ @type Any | ||
function validate(){ | ||
function addValidationMessage(msg){ | ||
_validationMessages.push(msg); | ||
} | ||
_validationMessages.splice(0); | ||
function validType(value, type, required){ | ||
var output; | ||
if (value === undefined){ | ||
output = true && !required; | ||
} else { | ||
if (typeof type === "string"){ | ||
output = typeof value === type; | ||
} else if (typeof type === "function"){ | ||
output = value instanceof type; | ||
} else { | ||
output = true; | ||
} | ||
} | ||
if (!output){ | ||
if (self.typeFailMsg){ | ||
addValidationMessage(self.typeFailMsg); | ||
} else { | ||
addValidationMessage("Invalid type :- " + self.value); | ||
} | ||
} | ||
return output; | ||
_validType = isValidType(self.value, self.type); | ||
if (!_validType){ | ||
_validationMessages.push(self.typeFailMsg || "Invalid type: " + self.value); | ||
} | ||
function validValue(value, validTest, required){ | ||
var validTests = validTest instanceof Array | ||
? validTest | ||
: [ validTest ]; | ||
var output; | ||
if (value === undefined && required){ | ||
output = false; | ||
} else if (value === undefined && !required) { | ||
output = true; | ||
} else { | ||
output = validTests.every(function(validTest){ | ||
if(validTest instanceof RegExp){ | ||
return validTest.test(value); | ||
} else if(typeof validTest === "function"){ | ||
try{ | ||
var extras = { | ||
addValidationMessage: addValidationMessage, | ||
config: options.config | ||
}; | ||
return validTest.call(extras, value); | ||
} catch(e){ | ||
addValidationMessage("valid test function crashed: " + e); | ||
return false; | ||
} | ||
} else { | ||
return true; | ||
} | ||
}); | ||
if (self.required && self.value === undefined){ | ||
_validValue = false; | ||
_validationMessages.push(self.valueFailMsg || "Missing required value"); | ||
} else { | ||
_validValue = isValidValue(self.value, self.valueTest, options.config, _validationMessages); | ||
if (!_validValue){ | ||
_validationMessages.push(self.valueFailMsg || "Invalid value: " + self.value); | ||
} | ||
if (!output){ | ||
if (self.valueFailMsg){ | ||
addValidationMessage(self.valueFailMsg); | ||
} else { | ||
addValidationMessage("Invalid value :- " + self.value); | ||
} | ||
} | ||
return output; | ||
} | ||
_validationMessages.splice(0); | ||
_validType = validType(self.value, self.type, self.required); | ||
_validValue = validValue(self.value, self.valueTest, self.required); | ||
} | ||
@@ -252,2 +197,41 @@ | ||
function isValidType(value, type){ | ||
if (value === undefined){ | ||
return true; | ||
} else if (typeof type === "string"){ | ||
return typeof value === type; | ||
} else if (typeof type === "function"){ | ||
return value instanceof type; | ||
} else { | ||
return true; | ||
} | ||
} | ||
function isValidValue(value, validTests, thing, validationMessages){ | ||
if (!Array.isArray(validTests)) validTests = [ validTests ]; | ||
function addValidationMessage(msg){ | ||
validationMessages.push(msg); | ||
} | ||
return validTests.every(function(validTest){ | ||
if(validTest instanceof RegExp){ | ||
return validTest.test(value); | ||
} else if(typeof validTest === "function"){ | ||
try{ | ||
var extras = { | ||
addValidationMessage: addValidationMessage, | ||
config: thing | ||
}; | ||
return validTest.call(extras, value); | ||
} catch(e){ | ||
addValidationMessage("valid test function crashed: " + e); | ||
return false; | ||
} | ||
} else { | ||
return true; | ||
} | ||
}); | ||
} | ||
module.exports = PropertyDefinition; |
"use strict"; | ||
/** | ||
module dependencies | ||
*/ | ||
var util = require("util"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
_ = require("underscore"), | ||
PropertyDefinition = require("./propertyDefinition"); | ||
PropertyDefinition = require("./propertyDefinition"), | ||
EventEmitter = require("events").EventEmitter; | ||
@@ -44,3 +42,3 @@ var l = console.log; | ||
// additional calls to define() modify existing, or add new property definitions. | ||
// additional calls to define() redefine an existing property, or add new property definitions. | ||
youngLad.define({ name: "firstname", type: "string" }) | ||
@@ -121,5 +119,6 @@ .define({ name: "DOB", type: Date }); | ||
function Thing(){ | ||
Object.defineProperty(this, "_errors", { enumerable: false, configurable: false, value: [] }); | ||
Object.defineProperty(this, "_definitions", { enumerable: false, configurable: false, value: {} }); | ||
} | ||
util.inherits(Thing, EventEmitter); | ||
/** | ||
@@ -133,3 +132,3 @@ Define an option | ||
var vehicleThing = new Thing() | ||
.define({ name: "maxSpeed", type: "number", alias: "m", valid: /\d+/, default: 4 }) | ||
.define({ name: "maxSpeed", type: "number", alias: "m", valueTest: /\d+/, default: 4 }) | ||
.define({ name: "isTaxed", type: "boolean", default: false }) | ||
@@ -193,7 +192,9 @@ .define("specifications", [ | ||
// duplication checks | ||
if (typeof this._definitions[name] !== "undefined"){ | ||
throw new Error("Cannot create config option, name already exists: " + name); | ||
var existingDef = this._definitions[name]; | ||
if (typeof existingDef !== "undefined"){ | ||
delete this._definitions[existingDef.alias] | ||
delete this._definitions[name]; | ||
} | ||
if (definition.alias && typeof this._definitions[definition.alias] !== "undefined"){ | ||
throw new Error("Cannot create config option, alias already exists: " + definition.alias); | ||
throw new Error("Cannot create property, alias already exists: " + definition.alias); | ||
} | ||
@@ -233,4 +234,2 @@ | ||
return output; | ||
} else { | ||
throw new Error("unspecified option: " + optionName); | ||
} | ||
@@ -280,2 +279,7 @@ }; | ||
defaultValues = []; | ||
if (arrayItems === process.argv && path.basename(arrayItems[0]) === "node"){ | ||
arrayItems.splice(0, 2); | ||
} | ||
while (typeof (item = arrayItems.shift()) !== "undefined"){ | ||
@@ -286,6 +290,11 @@ // options | ||
option = item.replace(optionPattern, ""); | ||
if(this.definition(option).type == "boolean"){ | ||
this.set(option, true); | ||
} else if (!optionPattern.test(arrayItems[0])) { | ||
this.set(option, arrayItems.shift()); | ||
var def = this.definition(option); | ||
if (def){ | ||
if(def.type == "boolean"){ | ||
this.set(option, true); | ||
} else if (!optionPattern.test(arrayItems[0])) { | ||
this.set(option, arrayItems.shift()); | ||
} | ||
} else { | ||
error.call(this, new Error("invalid property: " + option)); | ||
} | ||
@@ -324,3 +333,3 @@ } else { | ||
} else { | ||
throw new Error("cannot set a value on this unspecified option: " + option); | ||
error.call(this, new Error("invalid property: " + option)); | ||
} | ||
@@ -339,3 +348,3 @@ } | ||
function getValid() { | ||
return _.every(this.definitions, function(def, option){ | ||
return this._errors.length === 0 && _.every(this.definitions, function(def, option){ | ||
return def.valid; | ||
@@ -357,7 +366,7 @@ }); | ||
validationMessages; | ||
this.options.forEach(function(option){ | ||
validationMessages = self.definition(option).validationMessages; | ||
this.options.forEach(function(property){ | ||
validationMessages = self.definition(property).validationMessages; | ||
if (validationMessages.length){ | ||
output = output.concat({ | ||
option: option, | ||
property: property, | ||
validationMessages: validationMessages | ||
@@ -484,3 +493,3 @@ }); | ||
output.forEach(function(pair){ | ||
if (pair[0].length == 1){ | ||
if (pair[0].length === 1){ | ||
pair[0] = "-" + pair[0]; | ||
@@ -511,3 +520,3 @@ } else { | ||
Thing.prototype.get = function get(option){ | ||
return this.definition(option).value; | ||
return this.definition(option) && this.definition(option).value; | ||
}; | ||
@@ -588,3 +597,8 @@ | ||
}; | ||
function error(err){ | ||
this._errors.push(err); | ||
this.emit("error", err); | ||
} | ||
module.exports = Thing; |
{ | ||
"name": "nature", | ||
"version": "0.0.4", | ||
"description": "Classify the things (models) in your world (app) and how they interact.", | ||
"version": "0.1.0", | ||
"description": "Classify the things in your world and how they interact.", | ||
"main": "lib/nature.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -46,1 +46,3 @@ ***library under construction*** | ||
See the Thing docs for more detail.. | ||
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/c5ed60ded97e6bf11b24cf4d3c41fe97 "githalytics.com")](http://githalytics.com/75lb/nature) |
@@ -8,39 +8,43 @@ var assert = require("assert"), | ||
it("should list defined options"); | ||
it("should copyTo(this), copy its properties to passed in object"); | ||
it("should loadCliArgs()"); | ||
it("should `process.argv.splice(0, 2);` for you"); | ||
it("set(['--option', '---']) should set '---' on `option`"); | ||
it("should be compatible with --option=value style"); | ||
it("add(Thing) should add the Thing, not create a new one"); | ||
it("passing required:true should test for defaultOption.length > 0") | ||
it("this case should be invalid straight after definition: { type: Array, valueTest: function(a){ return a.length > 0; }}"); | ||
it("should throw when a none-existent property is accessed, e.g. console.log(options.dfkdshl)"); | ||
it("free usage on --help"); | ||
it("optimise validation checking, check once on value set or define, cache validation state"); | ||
it("the name 'option' should be changed to 'property'"); | ||
it("should protect from defining properties with reserved names like clone, toJSON, mixIn etc"); | ||
describe("Thing", function(){ | ||
var _config; | ||
describe("Thing API", function(){ | ||
var _thing; | ||
beforeEach(function(){ | ||
_config = new Thing(); | ||
_thing = new Thing(); | ||
}); | ||
describe("properties:", function(){ | ||
it("`valid` should return `true` only when all option values are valid", function(){ | ||
_config.define({ name: "one", type: "number", default: 1 }); | ||
assert.strictEqual(_config.valid, true); | ||
_config.define({ name: "two", type: "number", default: -1034.1 }); | ||
assert.strictEqual(_config.valid, true); | ||
_config.define({ name: "three", type: "number", default: "Cazzo" }); | ||
assert.strictEqual(_config.valid, false); | ||
it("valid - should return true only when all option values are valid", function(){ | ||
_thing.define({ name: "one", type: "number", default: 1 }); | ||
assert.strictEqual(_thing.valid, true); | ||
_thing.define({ name: "two", type: "number", default: -1034.1 }); | ||
assert.strictEqual(_thing.valid, true); | ||
_thing.define({ name: "three", type: "number", default: "Cazzo" }); | ||
assert.strictEqual(_thing.valid, false); | ||
}); | ||
it("`errors` should return an array of errors on invalid values and types", function(){ | ||
_config.define({ name: "one", type: Array, default: 1 }); | ||
_config.define({ name: "two", type: "string", default: 1 }); | ||
_config.define({ name: "three", type: RegExp, default: 1 }); | ||
_config.define({ name: "four", type: "string", default: "clive", valueTest: /test/ }); | ||
_config.define({ name: "five", type: Array, default: "clive", valueTest: function (val){ | ||
it("validationMessages - should return an array of msgs", function(){ | ||
_thing.define({ name: "one", type: Array, default: 1 }); | ||
_thing.define({ name: "two", type: "string", default: 1 }); | ||
_thing.define({ name: "three", type: RegExp, default: 1 }); | ||
_thing.define({ name: "four", type: "string", default: "clive", valueTest: /test/ }); | ||
_thing.define({ name: "five", type: Array, default: "clive", valueTest: function (val){ | ||
return val.length == 0; | ||
}}); | ||
_config.define({ name: "six",type: "number", value: 1 }); | ||
_thing.define({ name: "six",type: "number", value: 1 }); | ||
assert.ok(_config.errors.length == 4, JSON.stringify(_config.errors)); | ||
assert.ok(_thing.validationMessages.length == 4, JSON.stringify(_thing.validationMessages)); | ||
}); | ||
it("`definitions`", function(){ | ||
it("definitions", function(){ | ||
var def1 = new PropertyDefinition({ name: "one", type: Array, default: 1 }), | ||
@@ -50,15 +54,15 @@ def2 = new PropertyDefinition({ name: "two", type: "string", default: 1 }), | ||
_config.define([ def1, def2, def3 ]); | ||
_thing.define([ def1, def2, def3 ]); | ||
assert.strictEqual(Object.keys(_config.definitions).length, 3); | ||
assert.strictEqual(_config.definitions.one, def1); | ||
assert.strictEqual(_config.definitions.two, def2); | ||
assert.strictEqual(_config.definitions.three, def3); | ||
assert.strictEqual(Object.keys(_thing.definitions).length, 3); | ||
assert.strictEqual(_thing.definitions.one, def1); | ||
assert.strictEqual(_thing.definitions.two, def2); | ||
assert.strictEqual(_thing.definitions.three, def3); | ||
}); | ||
it("`options`", function(){ | ||
_config.define({ name: "one", type: Array, default: 1 }); | ||
_config.define({ name: "two", type: "string", default: 1 }); | ||
_config.define({ name: "three", type: RegExp, default: 1 }); | ||
it("options", function(){ | ||
_thing.define({ name: "one", type: Array, default: 1 }); | ||
_thing.define({ name: "two", type: "string", default: 1 }); | ||
_thing.define({ name: "three", type: RegExp, default: 1 }); | ||
assert.deepEqual(_config.options, [ "one", "two", "three" ]); | ||
assert.deepEqual(_thing.options, [ "one", "two", "three" ]); | ||
}) | ||
@@ -71,16 +75,22 @@ }); | ||
var definition = { name: "one", type: "string", default: "one" }; | ||
_config.define(definition); | ||
_thing.define(definition); | ||
assert.strictEqual(definition.type, _config.definition("one").type); | ||
assert.strictEqual(definition.default, _config.definition("one").default); | ||
assert.strictEqual(_config.get("one"), "one"); | ||
assert.strictEqual(definition.type, _thing.definition("one").type); | ||
assert.strictEqual(definition.default, _thing.definition("one").default); | ||
assert.strictEqual(_thing.get("one"), "one"); | ||
}); | ||
it("define(existingDefinition) should modify existing option definition"); | ||
it("define(existingDefinition) should redefine definition", function(){ | ||
_thing.define({ name: "one", type: "number", alias: "a" }); | ||
assert.strictEqual(_thing.definition("one").type, "number"); | ||
assert.strictEqual(_thing.definition("a").type, "number"); | ||
_thing.define({ name: "one", type: "string", alias: "a" }); | ||
assert.strictEqual(_thing.definition("a").type, "string"); | ||
}); | ||
it("define(PropertyDefinition) and retrieve with definition(name)", function(){ | ||
var def = new PropertyDefinition({ name: "one", "type": "number" }); | ||
_config.define(def); | ||
_thing.define(def); | ||
assert.strictEqual(def, _config.definition("one")); | ||
assert.strictEqual(def, _thing.definition("one")); | ||
}); | ||
@@ -90,8 +100,8 @@ | ||
function testValid(){} | ||
_config.define({ name: "one", "type": "number", alias: "o", valueTest: testValid }); | ||
_thing.define({ name: "one", "type": "number", alias: "o", valueTest: testValid }); | ||
assert.strictEqual(_config.definition("one").type, "number"); | ||
assert.strictEqual(_config.definition("o").type, "number"); | ||
assert.strictEqual(_config.definition("one").alias, "o"); | ||
assert.strictEqual(_config.definition("one").valueTest, testValid); | ||
assert.strictEqual(_thing.definition("one").type, "number"); | ||
assert.strictEqual(_thing.definition("o").type, "number"); | ||
assert.strictEqual(_thing.definition("one").alias, "o"); | ||
assert.strictEqual(_thing.definition("one").valueTest, testValid); | ||
}); | ||
@@ -102,16 +112,16 @@ | ||
describe("incorrect usage,", function(){ | ||
it("define(definition) should throw on duplicate option", function(){ | ||
_config.define({ name: "yeah" }); | ||
it("define(definition) should not throw on duplicate option", function(){ | ||
_thing.define({ name: "yeah" }); | ||
assert.throws(function(){ | ||
_config.define({ name: "yeah", }); | ||
assert.doesNotThrow(function(){ | ||
_thing.define({ name: "yeah", }); | ||
}); | ||
}); | ||
it("define(definition) should throw on duplicate alias", function(){ | ||
_config.define({ name: "one", alias: "o" }); | ||
_config.define({ name: "two", alias: "d" }); | ||
_config.define({ name: "three", alias: "t" }); | ||
_thing.define({ name: "one", alias: "o" }); | ||
_thing.define({ name: "two", alias: "d" }); | ||
_thing.define({ name: "three", alias: "t" }); | ||
assert.throws(function(){ | ||
_config.define({ name: "four", alias: "t" }); | ||
_thing.define({ name: "four", alias: "t" }); | ||
}); | ||
@@ -124,19 +134,19 @@ }); | ||
it("hasValue(optionName) should return true if option has value", function(){ | ||
_config.define({ name: "one" }); | ||
assert.strictEqual(_config.hasValue("one"), false); | ||
_thing.define({ name: "one" }); | ||
assert.strictEqual(_thing.hasValue("one"), false); | ||
_config.set("one", 1); | ||
assert.strictEqual(_config.hasValue("one"), true); | ||
_thing.set("one", 1); | ||
assert.strictEqual(_thing.hasValue("one"), true); | ||
}); | ||
it("hasValue(optionNameArray) should return true if has at least one value in list", function(){ | ||
_config.define({ name: "one" }) | ||
_thing.define({ name: "one" }) | ||
.define({ name: "two" }); | ||
assert.strictEqual(_config.hasValue(["one", "two"]), false); | ||
assert.strictEqual(_thing.hasValue(["one", "two"]), false); | ||
_config.set("one", 1); | ||
assert.strictEqual(_config.hasValue(["one", "two"]), true); | ||
_thing.set("one", 1); | ||
assert.strictEqual(_thing.hasValue(["one", "two"]), true); | ||
_config.set("two", 2); | ||
assert.strictEqual(_config.hasValue(["one", "two"]), true); | ||
_thing.set("two", 2); | ||
assert.strictEqual(_thing.hasValue(["one", "two"]), true); | ||
}); | ||
@@ -146,8 +156,8 @@ }); | ||
it("should unset() an option, and its alias", function(){ | ||
_config.define({ name: "one", type: "number", default: 1, alias: "K" }); | ||
assert.strictEqual(_config.get("one"), 1); | ||
assert.strictEqual(_config.get("K"), 1); | ||
_config.unset("one"); | ||
assert.strictEqual(_config.get("one"), undefined); | ||
assert.strictEqual(_config.get("K"), undefined); | ||
_thing.define({ name: "one", type: "number", default: 1, alias: "K" }); | ||
assert.strictEqual(_thing.get("one"), 1); | ||
assert.strictEqual(_thing.get("K"), 1); | ||
_thing.unset("one"); | ||
assert.strictEqual(_thing.get("one"), undefined); | ||
assert.strictEqual(_thing.get("K"), undefined); | ||
}); | ||
@@ -157,55 +167,55 @@ | ||
describe("set(), get()", function(){ | ||
it("should set() and get() an array", function(){ | ||
_config.define({ name: "one", type: Array }); | ||
_config.set("one", [0, 1]); | ||
describe("setting and getting values", function(){ | ||
it("should set(option, value) and get(options) an array", function(){ | ||
_thing.define({ name: "one", type: Array }); | ||
_thing.set("one", [0, 1]); | ||
assert.deepEqual(_config.get("one"), [0, 1]); | ||
assert.deepEqual(_thing.get("one"), [0, 1]); | ||
}) | ||
it("should set(option, value) and get(option)", function(){ | ||
_config.define({ name: "archiveDirectory", type: "string", alias: "d" }); | ||
_config.set("archiveDirectory", "testset"); | ||
it("should set(option, value) and get(option) a string", function(){ | ||
_thing.define({ name: "test", type: "string", alias: "d" }); | ||
_thing.set("test", "testset"); | ||
assert.strictEqual(_config.get("archiveDirectory"), "testset"); | ||
assert.strictEqual(_thing.get("test"), "testset"); | ||
}); | ||
it("should set(alias, value) then get(alias) and get(option)", function(){ | ||
_config.define({ name: "archiveDirectory", type: "string", alias: "d" }); | ||
_config.set("d", "testset"); | ||
_thing.define({ name: "archiveDirectory", type: "string", alias: "d" }); | ||
_thing.set("d", "testset"); | ||
assert.strictEqual(_config.get("d"), "testset"); | ||
assert.strictEqual(_config.get("archiveDirectory"), "testset"); | ||
assert.strictEqual(_thing.get("d"), "testset"); | ||
assert.strictEqual(_thing.get("archiveDirectory"), "testset"); | ||
}); | ||
it("should set default option() value", function(){ | ||
_config.define({ name: "one", type: "number", default: 1 }); | ||
_thing.define({ name: "one", type: "number", default: 1 }); | ||
assert.strictEqual(_config.get("one"), 1); | ||
assert.strictEqual(_thing.get("one"), 1); | ||
}); | ||
it("set(optionsHash) should set options in bulk", function(){ | ||
_config.define({ name: "one", type: "number", alias: "1" }) | ||
_thing.define({ name: "one", type: "number", alias: "1" }) | ||
.define({ name: "two", type: "number", alias: "t" }) | ||
.define({ name: "three", type: "number", alias: "3" }); | ||
assert.strictEqual(_config.get("one"), undefined); | ||
assert.strictEqual(_config.get("t"), undefined); | ||
assert.strictEqual(_config.get("3"), undefined); | ||
assert.strictEqual(_thing.get("one"), undefined); | ||
assert.strictEqual(_thing.get("t"), undefined); | ||
assert.strictEqual(_thing.get("3"), undefined); | ||
_config.set({ one: 1, t: 2, 3: 3 }); | ||
_thing.set({ one: 1, t: 2, 3: 3 }); | ||
assert.strictEqual(_config.get("one"), 1); | ||
assert.strictEqual(_config.get("two"), 2); | ||
assert.strictEqual(_config.get("three"), 3); | ||
assert.strictEqual(_thing.get("one"), 1); | ||
assert.strictEqual(_thing.get("two"), 2); | ||
assert.strictEqual(_thing.get("three"), 3); | ||
}); | ||
it("set(configInstance) should set options in bulk", function(){ | ||
_config.define({ name: "one", type: "number", alias: "1" }) | ||
_thing.define({ name: "one", type: "number", alias: "1" }) | ||
.define({ name: "two", type: "number", alias: "t" }) | ||
.define({ name: "three", type: "number", alias: "3" }); | ||
assert.strictEqual(_config.get("one"), undefined); | ||
assert.strictEqual(_config.get("t"), undefined); | ||
assert.strictEqual(_config.get("3"), undefined); | ||
assert.strictEqual(_thing.get("one"), undefined); | ||
assert.strictEqual(_thing.get("t"), undefined); | ||
assert.strictEqual(_thing.get("3"), undefined); | ||
@@ -217,7 +227,7 @@ var config2 = new Thing() | ||
_config.set(config2); | ||
_thing.set(config2); | ||
assert.strictEqual(_config.get("one"), -1); | ||
assert.strictEqual(_config.get("two"), -2); | ||
assert.strictEqual(_config.get("three"), -3); | ||
assert.strictEqual(_thing.get("one"), -1); | ||
assert.strictEqual(_thing.get("two"), -2); | ||
assert.strictEqual(_thing.get("three"), -3); | ||
@@ -227,48 +237,60 @@ }); | ||
it("set(optionsArray) should set options in bulk", function(){ | ||
var argv = ["node", "test.js", "info", "-d", "--preset", "--recurse", "music", "film", "documentary"]; | ||
argv.splice(0, 2); | ||
var command = argv.shift(); | ||
_config | ||
var argv = ["-d", "--preset", "--recurse", "music", "film", "documentary"]; | ||
_thing | ||
.define({ name: "detailed", alias: "d", type: "boolean" }) | ||
.define({ name: "recurse", type: "boolean" }) | ||
.define({ name: "preset", type: "string" }) | ||
.define({ name: "files", type: Array, defaultOption: true }); | ||
.define({ name: "files", type: Array, defaultOption: true }) | ||
.set(argv); | ||
_config.set(argv); | ||
assert.strictEqual(_thing.get("detailed"), true, JSON.stringify(_thing.toJSON())); | ||
assert.strictEqual(_thing.get("recurse"), true, JSON.stringify(_thing.toJSON())); | ||
assert.strictEqual(_thing.get("preset"), undefined); | ||
assert.deepEqual(_thing.get("files"), ["music", "film", "documentary"]); | ||
}); | ||
it("set(process.argv) should set options in bulk", function(){ | ||
process.argv = ["node", "test.js", "-d", "--preset", "--recurse", "music", "film", "documentary"]; | ||
_thing | ||
.define({ name: "detailed", alias: "d", type: "boolean" }) | ||
.define({ name: "recurse", type: "boolean" }) | ||
.define({ name: "preset", type: "string" }) | ||
.define({ name: "files", type: Array, defaultOption: true }) | ||
.set(process.argv); | ||
assert.strictEqual(_config.get("detailed"), true, JSON.stringify(_config.toJSON())); | ||
assert.strictEqual(_config.get("recurse"), true, JSON.stringify(_config.toJSON())); | ||
assert.strictEqual(_config.get("preset"), undefined); | ||
assert.deepEqual(_config.get("files"), ["music", "film", "documentary"]); | ||
assert.strictEqual(_thing.get("detailed"), true, JSON.stringify(_thing.toJSON())); | ||
assert.strictEqual(_thing.get("recurse"), true, JSON.stringify(_thing.toJSON())); | ||
assert.strictEqual(_thing.get("preset"), undefined); | ||
assert.deepEqual(_thing.get("files"), ["music", "film", "documentary"]); | ||
}); | ||
it("set(optionsArray) with a type Array", function(){ | ||
_config.define({ name:"one", type: Array }); | ||
_thing.define({ name:"one", type: Array }); | ||
_config.set(["--one", "test", 1, false]); | ||
assert.deepEqual(_config.get("one"), ["test"]); | ||
_thing.set(["--one", "test", 1, false]); | ||
assert.deepEqual(_thing.get("one"), ["test"]); | ||
_config.set(["--one", "test ,1 , false"]); | ||
assert.deepEqual(_config.get("one"), ["test", "1", "false"]); | ||
_thing.set(["--one", "test ,1 , false"]); | ||
assert.deepEqual(_thing.get("one"), ["test", "1", "false"]); | ||
}); | ||
it("set(optionsArray) with a `defaultOption` of type Array", function(){ | ||
_config.define({ name: "one", type: Array, defaultOption: true }); | ||
_config.set(["test", 1, false]); | ||
_thing.define({ name: "one", type: Array, defaultOption: true }); | ||
_thing.set(["test", 1, false]); | ||
assert.deepEqual(_config.get("one"), ["test", 1, false]); | ||
assert.deepEqual(_thing.get("one"), ["test", 1, false]); | ||
}); | ||
it("set(optionsArray) with a `defaultOption` of type 'string'", function(){ | ||
_config.define({ name: "one", type: "string", defaultOption: true }); | ||
_config.set(["test", 1, false]); | ||
_thing.define({ name: "one", type: "string", defaultOption: true }); | ||
_thing.set(["test", 1, false]); | ||
assert.strictEqual(_config.get("one"), "test"); | ||
assert.strictEqual(_thing.get("one"), "test"); | ||
}); | ||
it("set(optionsArray) with a `defaultOption` of type number", function(){ | ||
_config.define({ name: "one", type: "number", defaultOption: true }); | ||
_config.set([1, 4, 5]); | ||
_thing.define({ name: "one", type: "number", defaultOption: true }); | ||
_thing.set([1, 4, 5]); | ||
assert.strictEqual(_config.get("one"), 1); | ||
assert.strictEqual(_thing.get("one"), 1); | ||
}); | ||
@@ -279,15 +301,39 @@ | ||
describe("incorrect usage,", function(){ | ||
it("set(option, value) should throw on unregistered option", function(){ | ||
it("set(option, value) should emit 'error' on unregistered option", function(){ | ||
assert.throws(function(){ | ||
_config.set("yeah", "test"); | ||
_thing.set("yeah", "test"); | ||
}); | ||
assert.strictEqual(_thing.valid, false); | ||
assert.strictEqual(_thing._errors.length, 1); | ||
}); | ||
it("set(option, value) should emit error on unregistered option"); | ||
it("get(option) should throw on unregistered option", function(){ | ||
it("catching 'error' surpresses throw on bad set()", function(){ | ||
_thing.on("error", function(err){ | ||
assert.ok(err); | ||
}); | ||
assert.doesNotThrow(function(){ | ||
_thing.set("yeah", "test"); | ||
}); | ||
assert.strictEqual(_thing.valid, false); | ||
assert.strictEqual(_thing._errors.length, 1); | ||
}); | ||
it("set([--option, value]) should emit 'error' on unregistered option", function(){ | ||
assert.throws(function(){ | ||
_config.get("yeah", "test"); | ||
_thing.set(["--asdklfjlkd"]); | ||
}); | ||
assert.strictEqual(_thing.valid, false); | ||
assert.strictEqual(_thing._errors.length, 1); | ||
}); | ||
// it("get(option) should throw on unregistered option", function(){ | ||
// assert.throws(function(){ | ||
// _thing.get("yeah", "test"); | ||
// }); | ||
// assert.strictEqual(_thing.valid, false); | ||
// assert.strictEqual(_thing._errors.length, 1); | ||
// }); | ||
it("get(option) should return undefined on unregistered option", function(){ | ||
assert.strictEqual(_thing.get("yeah", "test"), undefined); | ||
}); | ||
}); | ||
@@ -297,9 +343,9 @@ }) | ||
it("should clone()", function(){ | ||
_config.define({ name: "one", type: "number", default: 1 }) | ||
_thing.define({ name: "one", type: "number", default: 1 }) | ||
.define({ name: "two", type: "number", default: 2 }); | ||
var config2 = _config.clone(); | ||
assert.notStrictEqual(_config, config2); | ||
assert.deepEqual(_.omit(_config.definition("one"), "config"), _.omit(config2.definition("one"), "config")); | ||
assert.deepEqual(_.omit(_config.definition("two"), "config"), _.omit(config2.definition("two"), "config")); | ||
var config2 = _thing.clone(); | ||
assert.notStrictEqual(_thing, config2); | ||
assert.deepEqual(_.omit(_thing.definition("one"), "config"), _.omit(config2.definition("one"), "config")); | ||
assert.deepEqual(_.omit(_thing.definition("two"), "config"), _.omit(config2.definition("two"), "config")); | ||
}); | ||
@@ -310,14 +356,14 @@ | ||
it("mixin(config)", function(){ | ||
_config.define({ name: "year", type: "number", default: 2013 }); | ||
_thing.define({ name: "year", type: "number", default: 2013 }); | ||
var config2 = new Thing().define({ name: "month", type: "string", default: "feb", alias: "m" }); | ||
var config3 = new Thing().define({ name: "day", type: "string", default: "Sunday", alias: "d" }) | ||
_config.mixIn(config2); | ||
_config.mixIn(config3); | ||
_thing.mixIn(config2); | ||
_thing.mixIn(config3); | ||
assert.strictEqual(_config.get("year"), 2013); | ||
assert.strictEqual(_config.get("month"), "feb"); | ||
assert.strictEqual(_config.get("day"), "Sunday"); | ||
assert.strictEqual(_config.get("m"), "feb"); | ||
assert.strictEqual(_config.get("d"), "Sunday"); | ||
assert.strictEqual(_thing.get("year"), 2013); | ||
assert.strictEqual(_thing.get("month"), "feb"); | ||
assert.strictEqual(_thing.get("day"), "Sunday"); | ||
assert.strictEqual(_thing.get("m"), "feb"); | ||
assert.strictEqual(_thing.get("d"), "Sunday"); | ||
}); | ||
@@ -328,28 +374,28 @@ | ||
it("mixin(config, groups)", function(){ | ||
_config.define({ name: "year", type: "number", default: 2013 }); | ||
_thing.define({ name: "year", type: "number", default: 2013 }); | ||
var config2 = new Thing().define({ name: "month", type: "string", default: "feb", alias: "m" }); | ||
var config3 = new Thing().define({ name: "day", type: "string", default: "Sunday", alias: "d" }) | ||
_config.mixIn(config2, "config2"); | ||
_config.mixIn(config3, ["config2", "config3"]); | ||
_thing.mixIn(config2, "config2"); | ||
_thing.mixIn(config3, ["config2", "config3"]); | ||
assert.strictEqual(_config.get("year"), 2013); | ||
assert.deepEqual(_config.definition("year").groups, []); | ||
assert.strictEqual(_config.get("month"), "feb"); | ||
assert.deepEqual(_config.definition("month").groups, ["config2"]); | ||
assert.strictEqual(_config.get("day"), "Sunday"); | ||
assert.deepEqual(_config.definition("day").groups, ["config2", "config3"]); | ||
assert.strictEqual(_thing.get("year"), 2013); | ||
assert.deepEqual(_thing.definition("year").groups, []); | ||
assert.strictEqual(_thing.get("month"), "feb"); | ||
assert.deepEqual(_thing.definition("month").groups, ["config2"]); | ||
assert.strictEqual(_thing.get("day"), "Sunday"); | ||
assert.deepEqual(_thing.definition("day").groups, ["config2", "config3"]); | ||
}); | ||
it("definition(option) should return correct def for full name and alias", function(){ | ||
_config.define({ name: "one", type: Array, default: [1,2], required: true, alias: "a" }); | ||
_thing.define({ name: "one", type: Array, default: [1,2], required: true, alias: "a" }); | ||
assert.strictEqual(_config.definition("one").type, Array); | ||
assert.strictEqual(_config.definition("a").type, Array); | ||
assert.deepEqual(_config.definition("one").default, [1,2]); | ||
assert.deepEqual(_config.definition("a").default, [1,2]); | ||
assert.strictEqual(_config.definition("one").required, true); | ||
assert.strictEqual(_config.definition("a").required, true); | ||
assert.strictEqual(_config.definition("one").alias, "a"); | ||
assert.strictEqual(_config.definition("a").alias, "a"); | ||
assert.strictEqual(_thing.definition("one").type, Array); | ||
assert.strictEqual(_thing.definition("a").type, Array); | ||
assert.deepEqual(_thing.definition("one").default, [1,2]); | ||
assert.deepEqual(_thing.definition("a").default, [1,2]); | ||
assert.strictEqual(_thing.definition("one").required, true); | ||
assert.strictEqual(_thing.definition("a").required, true); | ||
assert.strictEqual(_thing.definition("one").alias, "a"); | ||
assert.strictEqual(_thing.definition("a").alias, "a"); | ||
}); | ||
@@ -360,3 +406,3 @@ | ||
// set group after defining | ||
_config | ||
_thing | ||
.define({ name: "one", type: "number" }) | ||
@@ -368,3 +414,3 @@ .define({ name: "two", type: "number" }) | ||
// group during define | ||
_config | ||
_thing | ||
.define({ name: "four" }) | ||
@@ -379,16 +425,16 @@ .define("group2", [ | ||
var config2 = new Thing().define({ name: "seven" }); | ||
_config.mixIn(config2, "group4"); | ||
_thing.mixIn(config2, "group4"); | ||
// ungroup specific options | ||
_config.ungroup("group1", ["one", "two"]); | ||
_thing.ungroup("group1", ["one", "two"]); | ||
// ungroup all | ||
_config.ungroup("group2"); | ||
_thing.ungroup("group2"); | ||
// retrieve group | ||
_config.where({ group: "group3" }).toJSON(); | ||
_thing.where({ group: "group3" }).toJSON(); | ||
}); | ||
it("group(groupName, optionNameArray)", function(){ | ||
_config | ||
_thing | ||
.define({ name: "one", type: "number", alias: "1", default: 1 }) | ||
@@ -402,10 +448,10 @@ .define({ name: "two", type: "number", alias: "t", default: 2 }) | ||
assert.deepEqual(_config.where({ group: "everything" }).toJSON(), {one: 1, two:2, three:3 }); | ||
assert.deepEqual(_config.where({ group: "everything2" }).toJSON(), {one: 1, two:2, three:3 }); | ||
assert.deepEqual(_config.where({ group: "smallest" }).toJSON(), {one: 1 }); | ||
assert.deepEqual(_config.where({ group: "not the smallest" }).toJSON(), { two:2, three:3 }); | ||
assert.deepEqual(_thing.where({ group: "everything" }).toJSON(), {one: 1, two:2, three:3 }); | ||
assert.deepEqual(_thing.where({ group: "everything2" }).toJSON(), {one: 1, two:2, three:3 }); | ||
assert.deepEqual(_thing.where({ group: "smallest" }).toJSON(), {one: 1 }); | ||
assert.deepEqual(_thing.where({ group: "not the smallest" }).toJSON(), { two:2, three:3 }); | ||
}); | ||
it("group(groupName) groups all options", function(){ | ||
_config | ||
_thing | ||
.define({ name: "one", type: "number", alias: "1", default: 1 }) | ||
@@ -416,14 +462,14 @@ .define({ name: "two", type: "number", alias: "t", default: 2 }) | ||
assert.deepEqual(_config.where({ group: "everything" }).toJSON(), {one: 1, two:2, three:3 }); | ||
assert.deepEqual(_thing.where({ group: "everything" }).toJSON(), {one: 1, two:2, three:3 }); | ||
}) | ||
it("ungroup(groupName) should remove all options from groupName", function(){ | ||
_config | ||
_thing | ||
.define("group1", {name: "one"}) | ||
.define("group1", {name: "two"}) | ||
.define("group2", {name: "three"}); | ||
assert.deepEqual(_config.where({ group: "group1"}).options, ["one", "two"]); | ||
assert.deepEqual(_thing.where({ group: "group1"}).options, ["one", "two"]); | ||
_config.ungroup("group1"); | ||
assert.deepEqual(_config.where({ group: "group1"}).options, []); | ||
_thing.ungroup("group1"); | ||
assert.deepEqual(_thing.where({ group: "group1"}).options, []); | ||
@@ -433,3 +479,3 @@ }); | ||
it("ungroup(groupName, optionNameArray) should remove optionNames from groupName", function(){ | ||
_config | ||
_thing | ||
.define("group1", {name: "one"}) | ||
@@ -439,13 +485,13 @@ .define("group1", {name: "two"}) | ||
.define("group1", {name: "four"}); | ||
assert.deepEqual(_config.where({ group: "group1"}).options, ["one", "two", "four"]); | ||
assert.deepEqual(_thing.where({ group: "group1"}).options, ["one", "two", "four"]); | ||
_config.ungroup("group1", "one"); | ||
assert.deepEqual(_config.where({ group: "group1"}).options, ["two", "four"]); | ||
_thing.ungroup("group1", "one"); | ||
assert.deepEqual(_thing.where({ group: "group1"}).options, ["two", "four"]); | ||
_config.ungroup("group1", ["two", "four"]); | ||
assert.deepEqual(_config.where({ group: "group1"}).options, []); | ||
_thing.ungroup("group1", ["two", "four"]); | ||
assert.deepEqual(_thing.where({ group: "group1"}).options, []); | ||
}); | ||
it("where({group: groupName}) returns a config clone, with reduced options", function(){ | ||
_config | ||
it("where({group: groupName}) returns a Thing clone, with reduced properties", function(){ | ||
_thing | ||
.define({ name: "one", type: "number", alias: "1", default: 1 }) | ||
@@ -456,14 +502,12 @@ .define({ name: "two", type: "number", alias: "t", default: 2 }) | ||
assert.throws(function(){ | ||
_config.where({ group: "group" }).get("one"); | ||
}); | ||
assert.strictEqual(_config.where({ group: "group" }).get("two"), 2); | ||
assert.strictEqual(_config.where({ group: "group" }).get("three"), 3); | ||
assert.strictEqual(_config.get("one"), 1); | ||
assert.strictEqual(_config.get("two"), 2); | ||
assert.strictEqual(_config.get("three"), 3); | ||
assert.strictEqual(_thing.where({ group: "group" }).get("one"), undefined); | ||
assert.strictEqual(_thing.where({ group: "group" }).get("two"), 2); | ||
assert.strictEqual(_thing.where({ group: "group" }).get("three"), 3); | ||
assert.strictEqual(_thing.get("one"), 1); | ||
assert.strictEqual(_thing.get("two"), 2); | ||
assert.strictEqual(_thing.get("three"), 3); | ||
}); | ||
it("where({ name: {$ne: []}}) should exclude named options", function(){ | ||
_config | ||
_thing | ||
.define({ name: "one", type: "number", alias: "1", default: 1 }) | ||
@@ -474,8 +518,8 @@ .define({ name: "two", type: "number", alias: "t", default: 2 }) | ||
assert.throws(function(){ | ||
assert.strictEqual(_config.where({ name: { $ne: ["one", "two"] }}).get("one"), 1); | ||
}, null, JSON.stringify(_config.where({ name: { $ne: ["one", "two"] }}).toJSON())); | ||
assert.strictEqual(_thing.where({ name: { $ne: ["one", "two"] }}).get("one"), 1); | ||
}, null, JSON.stringify(_thing.where({ name: { $ne: ["one", "two"] }}).toJSON())); | ||
assert.throws(function(){ | ||
assert.strictEqual(_config.where({ name: { $ne: ["one", "two"] }}).get("two"), 2); | ||
assert.strictEqual(_thing.where({ name: { $ne: ["one", "two"] }}).get("two"), 2); | ||
}); | ||
assert.strictEqual(_config.get("three"), 3); | ||
assert.strictEqual(_thing.get("three"), 3); | ||
}); | ||
@@ -486,3 +530,3 @@ | ||
it("define(groupName, definitionArray) with groups", function(){ | ||
_config | ||
_thing | ||
.define({ name: "no group" }) | ||
@@ -514,6 +558,6 @@ .define("general", [ | ||
assert.deepEqual(_config.definition("no group").groups, []); | ||
assert.deepEqual(_config.definition("title").groups, ["source"], JSON.stringify(_config.definition("title"))); | ||
assert.deepEqual(_config.definition("start-at").groups, ["source"]); | ||
assert.deepEqual(_config.definition("stop-at").groups, ["source"]); | ||
assert.deepEqual(_thing.definition("no group").groups, []); | ||
assert.deepEqual(_thing.definition("title").groups, ["source"], JSON.stringify(_thing.definition("title"))); | ||
assert.deepEqual(_thing.definition("start-at").groups, ["source"]); | ||
assert.deepEqual(_thing.definition("stop-at").groups, ["source"]); | ||
}); | ||
@@ -520,0 +564,0 @@ |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
72484
1532
48
1