simplify filter.val
This commit is contained in:
parent
7c0b33d946
commit
dc03272d34
11
bin/messc
11
bin/messc
@ -80,9 +80,14 @@ fs.readFile(input, 'utf-8', function (e, data) {
|
||||
local_data_dir: path.dirname(input),
|
||||
}).render(data, function(err, output) {
|
||||
if (err) {
|
||||
err.forEach(function(e) {
|
||||
mess.writeError(e, options);
|
||||
});
|
||||
if (Array.isArray(err)) {
|
||||
err.forEach(function(e) {
|
||||
mess.writeError(e, options);
|
||||
});
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
|
||||
process.exit(1);
|
||||
} else {
|
||||
if (!options.benchmark) {
|
||||
|
@ -807,7 +807,7 @@ mess.Parser = function Parser(env) {
|
||||
if ((op = $(this.entities.comparison)) &&
|
||||
(val = $(this.entities.quoted) || $(/^[\w-\.]+/))) {
|
||||
if (! $(']')) return;
|
||||
return new tree.Filter(key, op, val);
|
||||
return new tree.Filter(key, op, val, memo);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -1,10 +1,19 @@
|
||||
(function(tree) {
|
||||
|
||||
tree.Filter = function Filter(key, op, val) {
|
||||
this.key = key;
|
||||
tree.Filter = function Filter(key, op, val, index) {
|
||||
this.key = key instanceof tree.Quoted ? key.value : key;
|
||||
this.op = op;
|
||||
this.val = val;
|
||||
this.id = key + op + val;
|
||||
this.val = val instanceof tree.Quoted ? val.value : val;
|
||||
if (op !== '=' && op !== '!=') {
|
||||
this.val = 1*this.val;
|
||||
if (isNaN(this.val)) {
|
||||
throw {
|
||||
message: 'Cannot use operator "' + op + '" with value ' + val,
|
||||
index: index
|
||||
};
|
||||
}
|
||||
}
|
||||
this.id = this.key + this.op + this.val;
|
||||
};
|
||||
|
||||
|
||||
@ -28,12 +37,8 @@ var opNegate = {
|
||||
|
||||
|
||||
tree.Filter.prototype.toXML = function(env) {
|
||||
if (this.val.is) {
|
||||
var value = this.val.toString((this.val.is == 'string'));
|
||||
} else {
|
||||
var value = this.val;
|
||||
}
|
||||
|
||||
if (typeof this.val === 'number') var value = this.val;
|
||||
else var value = '\'' + ('' + this.val).replace(/'/g, '\'') + '\'';
|
||||
return '[' + this.key + '] ' + opXML[this.op] + ' ' + value;
|
||||
};
|
||||
|
||||
@ -74,7 +79,7 @@ tree.Filter.sound = function(filters) {
|
||||
|
||||
switch (filter.op) {
|
||||
case '=':
|
||||
value = filters[id].val.toString();
|
||||
value = filters[id].val;
|
||||
if ('=' in byKey[key] && byKey[key]['='] != value) return false;
|
||||
if ('!=' in byKey[key] && byKey[key]['!='].indexOf(value) >= 0) return false;
|
||||
number = parseInt(value, 10);
|
||||
@ -86,14 +91,14 @@ tree.Filter.sound = function(filters) {
|
||||
break;
|
||||
|
||||
case '!=':
|
||||
value = filters[id].val.toString();
|
||||
value = filters[id].val;
|
||||
if ('=' in byKey[key] && byKey[key]['='] == value) return false;
|
||||
if (!('!=' in byKey[key])) byKey[key]['!='] = [];
|
||||
byKey[key]['!='].push(value);
|
||||
break;
|
||||
|
||||
case '>':
|
||||
number = parseInt(filters[id].val.toString(), 10);
|
||||
number = parseInt(filters[id].val, 10);
|
||||
if ('=' in byKey[key] && byKey[key]['='] <= number) return false;
|
||||
if ('<' in byKey[key] && byKey[key]['<'] <= number) return false;
|
||||
if ('<=' in byKey[key] && byKey[key]['<='] <= number) return false;
|
||||
@ -101,7 +106,7 @@ tree.Filter.sound = function(filters) {
|
||||
break;
|
||||
|
||||
case '>=':
|
||||
number = parseInt(filters[id].val.toString(), 10);
|
||||
number = parseInt(filters[id].val, 10);
|
||||
if ('=' in byKey[key] && byKey[key]['='] < number) return false;
|
||||
if ('<' in byKey[key] && byKey[key]['<'] <= number) return false;
|
||||
if ('<=' in byKey[key] && byKey[key]['<='] < number) return false;
|
||||
@ -109,7 +114,7 @@ tree.Filter.sound = function(filters) {
|
||||
break;
|
||||
|
||||
case '<':
|
||||
number = parseInt(filters[id].val.toString(), 10);
|
||||
number = parseInt(filters[id].val, 10);
|
||||
if ('=' in byKey[key] && byKey[key]['='] >= number) return false;
|
||||
if ('>' in byKey[key] && byKey[key]['>'] >= number) return false;
|
||||
if ('>=' in byKey[key] && byKey[key]['>='] >= number) return false;
|
||||
@ -117,7 +122,7 @@ tree.Filter.sound = function(filters) {
|
||||
break;
|
||||
|
||||
case '<=':
|
||||
number = parseInt(filters[id].val.toString(), 10);
|
||||
number = parseInt(filters[id].val, 10);
|
||||
if ('=' in byKey[key] && byKey[key]['='] > number) return false;
|
||||
if ('>' in byKey[key] && byKey[key]['>'] >= number) return false;
|
||||
if ('>=' in byKey[key] && byKey[key]['>='] > number) return false;
|
||||
@ -133,7 +138,7 @@ tree.Filter.sound = function(filters) {
|
||||
// Only simplifies sound filters.
|
||||
tree.Filter.simplify = function(filters) {
|
||||
// Shortcut for single-filter filters.
|
||||
if (Object.keys(filters).length == 1) filters;
|
||||
if (Object.keys(filters).length == 1) return filters;
|
||||
|
||||
var byKey = {};
|
||||
var filter, key, value;
|
||||
@ -164,69 +169,68 @@ tree.Filter.simplify = function(filters) {
|
||||
break;
|
||||
|
||||
case '>':
|
||||
value = parseInt(filters[id].val.toString(), 10);
|
||||
value = parseInt(filters[id].val, 10);
|
||||
n = key + '>=';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() > value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val > value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
delete byKey[n];
|
||||
}
|
||||
|
||||
n = key + '>';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() >= value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val >= value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
}
|
||||
byKey[n] = id;
|
||||
break;
|
||||
|
||||
case '>=':
|
||||
value = parseInt(filters[id].val.toString(), 10);
|
||||
value = parseInt(filters[id].val, 10);
|
||||
n = key + '>';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() >= value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val >= value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
delete byKey[n];
|
||||
}
|
||||
|
||||
n = key + '>=';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() > value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val > value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
}
|
||||
byKey[n] = id;
|
||||
break;
|
||||
|
||||
|
||||
case '<':
|
||||
value = parseInt(filters[id].val.toString(), 10);
|
||||
value = parseInt(filters[id].val, 10);
|
||||
n = key + '<=';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() < value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val < value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
delete byKey[n];
|
||||
}
|
||||
|
||||
n = key + '<';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() <= value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val <= value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
}
|
||||
byKey[n] = id;
|
||||
break;
|
||||
|
||||
case '<=':
|
||||
value = parseInt(filters[id].val.toString(), 10);
|
||||
value = parseInt(filters[id].val, 10);
|
||||
n = key + '<';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() <= value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val <= value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
delete byKey[n];
|
||||
}
|
||||
|
||||
n = key + '<=';
|
||||
if (n in byKey) {
|
||||
if (filters[byKey[n]].val.toString() < value) { delete filters[id]; continue; }
|
||||
if (filters[byKey[n]].val < value) { delete filters[id]; continue; }
|
||||
else delete filters[byKey[n]];
|
||||
}
|
||||
byKey[n] = id;
|
||||
@ -268,7 +272,7 @@ tree.Filter.mergable = function(f1, f2) {
|
||||
if (filter1.key !== filter2.key) return;
|
||||
|
||||
// Simple case:
|
||||
if (filter1.val.toString() == filter2.val.toString()) {
|
||||
if (filter1.val == filter2.val) {
|
||||
var op1 = filter1.op, op2 = filter2.op;
|
||||
if ((op1 === '>=' && op2 === '<') || (op1 === '<' && op2 === '>=') ||
|
||||
(op1 === '<=' && op2 === '>') || (op1 === '>' && op2 === '<=')) {
|
||||
|
Loading…
Reference in New Issue
Block a user