occamsrazor
Advanced tools
Comparing version 2.2.2 to 3.0.0
Changelog | ||
2.3.0 (unreleased) | ||
================== | ||
- added registry feature | ||
- fixed tests | ||
- updated docs | ||
- added bower.json | ||
2.2.2 (2013/06/10) | ||
@@ -23,3 +31,3 @@ ================== | ||
- documentation partial rewrite | ||
- documentation: partial rewrite | ||
- added anythingvalidator | ||
@@ -26,0 +34,0 @@ |
@@ -11,5 +11,5 @@ /****************************************************************************** | ||
* GPL license/MIT license | ||
* 19 Apr 2013 | ||
* 6 Sep 2015 | ||
* | ||
* version 2.2.1 | ||
* version 3.0.0 | ||
******************************************************************************/ | ||
@@ -27,10 +27,38 @@ | ||
} | ||
//returns a validator (function that returns a score) | ||
var chain = function () { | ||
var validators = arguments; | ||
var closure = function (obj) { | ||
var isAnything = function (obj){ | ||
return true; | ||
}; | ||
var shortcut_validators = { | ||
match: function (s){ | ||
if (typeof s !== 'string' && !(typeof s === 'object' && 'test' in s)){ | ||
throw new Error("The match argument must be a string or a regular expression"); | ||
} | ||
return function (obj){ | ||
var str = obj.toString(); | ||
if (typeof s === 'string'){ | ||
return str === s; //it is a string | ||
} | ||
else if (typeof s === 'object' && 'test' in s){ | ||
return s.test(str); | ||
} | ||
}; | ||
}, | ||
has: function (attr){ | ||
return function (obj){return attr in obj;}; | ||
}, | ||
isPrototypeOf: function (proto){ | ||
return function (obj){return proto.isPrototypeOf(obj);}; | ||
} | ||
}; | ||
var _validator = function (funcs){ | ||
var k; | ||
funcs = funcs || [isAnything]; | ||
var v = function (obj){ | ||
var i, score, total = 0; | ||
for (i = 0; i < validators.length; i++) { | ||
score = validators[i](obj); | ||
for (i = 0; i < funcs.length; i++) { | ||
score = funcs[i](obj); | ||
if (!score) { | ||
@@ -43,24 +71,23 @@ return null; | ||
}; | ||
return closure; | ||
}; | ||
var stringValidator = function (s){ | ||
if (typeof s !== 'string' && !(typeof s === 'object' && 'test' in s)){ | ||
throw new Error("A stringValidator argument must be a string or a regular expression"); | ||
v.chain = function (func){ | ||
return _validator(funcs.concat(func)); | ||
}; | ||
// shortcut validators | ||
for (k in shortcut_validators){ | ||
v[k] = (function (f){ | ||
return function (){ | ||
var args = Array.prototype.slice.call(arguments); | ||
return v.chain(f.apply(null, args)); | ||
}; | ||
}(shortcut_validators[k])); | ||
} | ||
var closure = function (obj){ | ||
var str = obj.toString(); | ||
if (typeof s === 'string'){ | ||
return str === s; //it is a string | ||
} | ||
else if (typeof s === 'object' && 'test' in s){ | ||
return s.test(str); | ||
} | ||
}; | ||
return closure; | ||
return v; | ||
}; | ||
var isAnything = function (obj){ | ||
return true; | ||
//returns a validator (function that returns a score) | ||
var validator = function (){ | ||
return _validator(); | ||
}; | ||
@@ -70,3 +97,3 @@ | ||
function closure(){ | ||
var newobj, out, | ||
var newobj, out, | ||
New = function (){}; | ||
@@ -84,3 +111,3 @@ New.prototype = Constructor.prototype; | ||
}; | ||
//convert an array in form [1,2,3] into a string "ABC" (easily sortable) | ||
@@ -119,3 +146,3 @@ var score_array_to_str = function (score) { | ||
var add = function (functions, func, validators) { | ||
var i; | ||
var i; | ||
if (typeof func !== 'function') { | ||
@@ -133,6 +160,6 @@ throw new Error("The last argument MUST be a function"); | ||
if (validators[i] === null){ | ||
validators[i] = isAnything; | ||
validators[i] = validator(); | ||
} | ||
else if (typeof validators[i] !== 'function'){ | ||
validators[i] = stringValidator(validators[i]); | ||
validators[i] = validator().match(validators[i]); | ||
} | ||
@@ -174,3 +201,3 @@ } | ||
getScore = compute_score(args); | ||
//decorate | ||
//decorate | ||
for (i = 0; i < functions.length; i++) { | ||
@@ -258,10 +285,41 @@ func = functions[i].func; | ||
// registries | ||
var _registries = (window || global); | ||
if(!_registries._occamsrazor_registries){ | ||
_registries._occamsrazor_registries = {}; | ||
} | ||
_registries = _registries._occamsrazor_registries; | ||
var registry = function (registry_name){ | ||
registry_name = registry_name || "default"; | ||
var adapter = function (_registry){ | ||
return function (function_name){ | ||
if ( !( function_name in _registry) ){ | ||
_registry[function_name] = occamsrazor(); | ||
} | ||
return _registry[function_name]; | ||
}; | ||
}; | ||
if ( !( registry_name in _registries) ){ | ||
_registries[registry_name] = {}; | ||
} | ||
return adapter(_registries[registry_name]); | ||
}; | ||
//public methods | ||
occamsrazor.chain = chain; | ||
occamsrazor.stringValidator = stringValidator; | ||
occamsrazor.validator = validator; | ||
occamsrazor.shortcut_validators = shortcut_validators; | ||
occamsrazor.adapters = occamsrazor; | ||
occamsrazor.isAnything = isAnything; | ||
occamsrazor.registry = registry; | ||
// undocumented but tested (private use) | ||
occamsrazor.wrapConstructor = wrapConstructor; | ||
// Expose occamsrazor as an AMD module | ||
@@ -281,2 +339,1 @@ if (typeof define === "function" && define.amd) { | ||
}()); | ||
{ | ||
"name": "occamsrazor", | ||
"version": "2.2.2", | ||
"version": "3.0.0", | ||
"description": "A plugin system for Javascript", | ||
@@ -17,3 +17,5 @@ "main": "occamsrazor.js", | ||
"keywords": [ | ||
"plugin", | ||
"plugins", | ||
"components", | ||
"registry", | ||
"adapters" | ||
@@ -20,0 +22,0 @@ ], |
@@ -11,11 +11,11 @@ /* | ||
setup: function (){ | ||
this.is_instrument = function (obj){ | ||
this.is_instrument = occamsrazor.validator().chain(function (obj){ | ||
return 'instrument_name' in obj; | ||
}; | ||
}); | ||
this.is_guitar = occamsrazor.chain(this.is_instrument, function (obj){ | ||
this.is_guitar = this.is_instrument.chain( function (obj){ | ||
return 'nStrings' in obj; | ||
}); | ||
this.is_electricguitar = occamsrazor.chain(this.is_guitar, function (obj){ | ||
this.is_electricguitar = this.is_guitar.chain(function (obj){ | ||
return 'ampli' in obj; | ||
@@ -38,3 +38,3 @@ }); | ||
instrument_name : 'electric guitar', | ||
nStrings : 6, | ||
nStrings : 6, | ||
ampli : 'marshall' | ||
@@ -82,3 +82,3 @@ }; | ||
}); | ||
test("Execute all functions (just one result)", function() { | ||
@@ -132,5 +132,5 @@ var results = this.player.all(this.guitar); | ||
setup: function (){ | ||
this.is_number = function (obj){ | ||
this.is_number = occamsrazor.validator().chain(function (obj){ | ||
return typeof obj === 'number' && ! isNaN(obj); | ||
}; | ||
}); | ||
@@ -153,15 +153,15 @@ this.sum = occamsrazor() | ||
testing | ||
testing | ||
*/ | ||
module( "Pubsub and default stringvalidator", { | ||
setup: function (){ | ||
this.is_instrument = function (obj){ | ||
this.is_instrument = occamsrazor.validator().chain(function (obj){ | ||
return 'instrument_name' in obj; | ||
}; | ||
}); | ||
this.is_guitar = occamsrazor.chain(this.is_instrument, function (obj){ | ||
this.is_guitar = this.is_instrument.chain(function (obj){ | ||
return 'nStrings' in obj; | ||
}); | ||
this.is_electricguitar = occamsrazor.chain(this.is_guitar, function (obj){ | ||
this.is_electricguitar = this.is_guitar.chain( function (obj){ | ||
return 'ampli' in obj; | ||
@@ -178,3 +178,3 @@ }); | ||
instrument_name : 'electric guitar', | ||
nStrings : 6, | ||
nStrings : 6, | ||
ampli : 'marshall' | ||
@@ -221,3 +221,3 @@ }; | ||
var is_hello = occamsrazor.stringValidator('hello'); | ||
var is_hello = occamsrazor.validator().match('hello'); | ||
ok(!!is_hello('hello'), 'string validator ok'); | ||
@@ -230,3 +230,3 @@ ok(!is_hello('nothello'), 'string validator ko'); | ||
var is_hello = occamsrazor.stringValidator(/hello/); | ||
var is_hello = occamsrazor.validator().match(/hello/); | ||
ok(!!is_hello('hello'), 'string validator ok'); | ||
@@ -241,7 +241,7 @@ ok(!!is_hello('ishello'), 'string validator ok'); | ||
test("stringValidator using string", function() { | ||
var hello = occamsrazor().add(function (){ | ||
return "hello " + this; | ||
}); | ||
equal(hello.apply('world!'), 'hello world!', 'This works correctly!'); | ||
@@ -254,5 +254,5 @@ | ||
test("isAnything works", function() { | ||
ok(occamsrazor.isAnything({}), 'isAnything validator ok'); | ||
ok(occamsrazor.validator()({}), 'isAnything validator ok'); | ||
}); | ||
@@ -265,3 +265,3 @@ | ||
}); | ||
equals(works('anything'),'anything', 'used isAnything instead of null'); | ||
@@ -277,6 +277,6 @@ | ||
}); | ||
equals(Constructor('anything').x,'anything', 'returns an object'); | ||
equals(new Constructor('anything').x,'anything', 'returns an object'); | ||
}); | ||
@@ -289,5 +289,5 @@ | ||
Constructor.prototype.number = 10; | ||
var WrappedConstructor = occamsrazor.wrapConstructor(Constructor); | ||
var obj = WrappedConstructor('5'); | ||
@@ -302,11 +302,11 @@ equals(obj.x,5, 'test args'); | ||
equals(obj.constructor,Constructor, 'test constructor'); | ||
}); | ||
test("adding a constructor function with addnew", function() { | ||
test("adding a constructor function with addNew", function() { | ||
var Constructor = occamsrazor().addnew(function (x){ | ||
var Constructor = occamsrazor().addNew(function (x){ | ||
this.x = x; | ||
}); | ||
equals(Constructor('anything').x,'anything', 'returns an object'); | ||
@@ -317,3 +317,3 @@ equals(new Constructor('anything').x,'anything', 'returns an object'); | ||
test("checking prototype and constructor with addnew", function() { | ||
test("checking prototype and constructor with addNew", function() { | ||
var Constructor = function (x){ | ||
@@ -323,5 +323,5 @@ this.x = x; | ||
Constructor.prototype.number = 10; | ||
var WrappedConstructor = occamsrazor().addnew(Constructor); | ||
var WrappedConstructor = occamsrazor().addNew(Constructor); | ||
var obj = WrappedConstructor('5'); | ||
@@ -331,4 +331,22 @@ equals(obj.x,5, 'test args'); | ||
equals(obj.constructor,Constructor, 'test constructor'); | ||
}); | ||
module( "registry" ); | ||
test("testing empty registry", function() { | ||
var test = occamsrazor.registry('main')('test'); | ||
ok(test instanceof Function); | ||
}); | ||
test("testing full registry", function() { | ||
var test1 = occamsrazor.registry('main')('test').add(function (){return 'ok'}), | ||
test2 = occamsrazor.registry('main')('test'); | ||
equals(test2(), 'ok'); | ||
}); | ||
test("testing default registry", function() { | ||
var test1 = occamsrazor.registry()('test').add(function (){return 'ok'}), | ||
test2 = occamsrazor.registry()('test'); | ||
equals(test2(), 'ok'); | ||
}); |
Sorry, the diff of this file is not supported yet
101905
11
2125
530