-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (30 loc) · 773 Bytes
/
index.js
File metadata and controls
36 lines (30 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var props = require('component-props');
var unique = require('yields-uniq');
var cache = {};
function Expression(str) {
this.str = str;
this.props = unique(props(str));
this.fn = compile(str, this.props);
}
Expression.prototype.exec = function(scope, context){
scope = scope || {};
var args = scope ? values(scope, this.props) : [];
return this.fn.apply(context, args);
};
Expression.prototype.toString = function(){
return this.str;
};
function values(obj, keys) {
return keys.map(function(key){
return obj[key];
});
}
function compile(str, props){
if(cache[str]) return cache[str];
var args = props.slice();
args.push('return ' + str);
var fn = Function.apply(null, args);
cache[str] = fn;
return fn;
}
module.exports = Expression;