Enforce types

This commit is contained in:
Tom MacWright 2012-05-08 15:43:37 -04:00
parent 262ef7e722
commit 6eff56e2e2

View File

@ -19,7 +19,7 @@ tree.Filter = function Filter(key, op, val, index, filename) {
this.val = val;
}
if (ops[this.op][1]) {
if (ops[this.op][1] == 'numeric') {
this.val = 1 * this.val;
}
@ -29,27 +29,31 @@ tree.Filter = function Filter(key, op, val, index, filename) {
// xmlsafe, numeric, suffix
var ops = {
'<': [' &lt; ', true],
'>': [' &gt; ', true],
'=': [' = ', false],
'!=': [' != ', false],
'<=': [' &lt;= ', true],
'>=': [' &gt;= ', true],
'=~': ['.match(', false, ')']
'<': [' &lt; ', 'numeric'],
'>': [' &gt; ', 'numeric'],
'=': [' = ', 'both'],
'!=': [' != ', 'both'],
'<=': [' &lt;= ', 'numeric'],
'>=': [' &gt;= ', 'numeric'],
'=~': ['.match(', 'string', ')']
};
tree.Filter.prototype.toXML = function(env) {
if (ops[this.op][1] && isNaN(this.val)) {
if (this.val.eval) this._val = this.val.eval(env);
if (this.key.eval) this._key = this.key.eval(env);
if (this._key) var key = this._key.toString(false);
if (this._val) var val = this._val.toString(this._val.is == 'string');
if (
(ops[this.op][1] == 'numeric' && isNaN(this.val)) ||
(ops[this.op][1] == 'string' && (val || this.val)[0] != "'")
) {
env.error({
message: 'Cannot use operator "' + this.op + '" with value ' + this.val,
index: this.index,
filename: this.filename
});
}
if (this.val.eval) this._val = this.val.eval(env);
if (this.key.eval) this._key = this.key.eval(env);
if (this._key) var key = this._key.toString(false);
if (this._val) var val = this._val.toString(this._val.is == 'string');
return '[' + (key || this.key) + ']' + ops[this.op][0] + '' + (val || this.val) + (ops[this.op][2] || '');
};