to-function
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,4 +5,4 @@ { | ||
"description": "Convert property access strings into functions", | ||
"version": "1.0.0", | ||
"keywords": ["utility"], | ||
"version": "1.1.0", | ||
"keywords": ["utility", "function"], | ||
"dependencies": {}, | ||
@@ -9,0 +9,0 @@ "development": {}, |
1.1.0 / 2012-11-20 | ||
================== | ||
* add js expression support | ||
* add strict equality default | ||
* add regexp support | ||
1.0.0 / 2012-11-08 | ||
@@ -3,0 +10,0 @@ ================== |
54
index.js
@@ -11,4 +11,2 @@ | ||
* | ||
* TODO: consider compiling to functions. | ||
* | ||
* @param {Mixed} obj | ||
@@ -20,9 +18,11 @@ * @return {Function} | ||
function toFunction(obj) { | ||
switch (typeof obj) { | ||
case 'function': | ||
switch ({}.toString.call(obj)) { | ||
case '[object Function]': | ||
return obj; | ||
case 'string': | ||
case '[object String]': | ||
return stringToFunction(obj); | ||
case '[object RegExp]': | ||
return regexpToFunction(obj); | ||
default: | ||
throw new TypeError('invalid callback "' + obj + '"'); | ||
return defaultToFunction(obj); | ||
} | ||
@@ -32,2 +32,30 @@ } | ||
/** | ||
* Default to strict equality. | ||
* | ||
* @param {Mixed} val | ||
* @return {Function} | ||
* @api private | ||
*/ | ||
function defaultToFunction(val) { | ||
return function(obj){ | ||
return val === obj; | ||
} | ||
} | ||
/** | ||
* Convert `re` to a function. | ||
* | ||
* @param {RegExp} re | ||
* @return {Function} | ||
* @api private | ||
*/ | ||
function regexpToFunction(re) { | ||
return function(obj){ | ||
return re.test(obj); | ||
} | ||
} | ||
/** | ||
* Convert property `str` to a function. | ||
@@ -41,15 +69,3 @@ * | ||
function stringToFunction(str) { | ||
var props = str.split('.'); | ||
return function(obj){ | ||
for (var i = 0; i < props.length; ++i) { | ||
if (null == obj) return; | ||
var name = props[i]; | ||
if ('function' == typeof obj[name]) { | ||
obj = obj[name](); | ||
} else { | ||
obj = obj[name]; | ||
} | ||
} | ||
return obj; | ||
} | ||
return new Function('_', 'return _.' + str); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Convert property access strings into functions", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"keywords": ["utility"], | ||
@@ -8,0 +8,0 @@ "dependencies": {}, |
@@ -1,2 +0,1 @@ | ||
# to-function | ||
@@ -20,4 +19,11 @@ | ||
# License | ||
## Converts | ||
- strings to expressions, for ex `foo.bar()` becomes `return _.foo.bar()` | ||
- regexps to `re.test(val)` calls | ||
- functions to themselves | ||
- defaults to `===` equality | ||
## License | ||
MIT |
@@ -16,13 +16,14 @@ | ||
it('should ignore undefined properties', function(){ | ||
var fn = toFunction('name.first'); | ||
assert(null == fn({})); | ||
}) | ||
it('should invoke getter-style functions', function(){ | ||
var user = { attrs: { name: 'tj' }}; | ||
user.name = function(){ return this.attrs.name }; | ||
var fn = toFunction('name'); | ||
var fn = toFunction('name()'); | ||
assert('tj' == fn(user)); | ||
}) | ||
it('should support js expressions', function(){ | ||
var fn = toFunction('age > 18'); | ||
assert(true === fn({ age: 20 })); | ||
assert(false === fn({ age: 18 })); | ||
}) | ||
}) | ||
@@ -35,2 +36,20 @@ | ||
}) | ||
}) | ||
describe('toFunction(regexp)', function(){ | ||
it('should .test the value', function(){ | ||
var fn = toFunction(/^tob/); | ||
assert(false === fn({})); | ||
assert(false === fn('luna')); | ||
assert(true === fn('tobi')); | ||
}) | ||
}) | ||
describe('toFunction(other)', function(){ | ||
it('should default to === equality', function(){ | ||
var fn = toFunction(null); | ||
assert(true == fn(null)); | ||
assert(false == fn(0)); | ||
assert(false == fn()); | ||
}) | ||
}) |
4018
111
29