-
Notifications
You must be signed in to change notification settings - Fork 0
/
libprefix-node.js
61 lines (48 loc) · 1.07 KB
/
libprefix-node.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = function(){
return {
operators: operators,
process: process,
};
}
/**
* Declare Operators
*/
var operators = {};
operators.and = function (){
for(var i = 0; i < arguments.length; i+=1){
if(arguments[i] == 0) return 0;
}
return 1;
};
operators.or = function (){
for(var i = 0; i < arguments.length; i+=1){
if(arguments[i] == 1) return 1;
}
return 0;
};
operators.not = function(){
return 1 - arguments[0];
}
operators["*"] = operators.and;
operators["+"] = operators.or;
operators["!"] = operators.not;
/**
* Parse & Exec Function
*/
function process(expression, context, operators){
var expression_pattern = /\([\w+*/!]+ (\w+ ?)+\)/g;
var e;
while(e = expression.match(expression_pattern), e){
e = e[0];
var stack = e.replace("(","").replace(")","").split(" ");
var operator = operators[stack.shift()];
stack = stack.map(function(el){
var value = parseInt(el);
if(!isNaN(value))
return value;
return context[el];
});
expression = expression.replace(e, operator.apply(context, stack));
}
return parseInt(expression);
}