instead, add a trimWhitespace option to OpenLayers.String.numericIf as discussed with @ahocevar

This commit is contained in:
Bart van den Eijnden
2012-03-19 13:07:49 +01:00
parent 7a769830fe
commit 94bd37031d
3 changed files with 14 additions and 6 deletions
+5 -1
View File
@@ -189,12 +189,16 @@ OpenLayers.String = {
* *
* Parameters: * Parameters:
* value - {String} * value - {String}
* trimWhitespace - {Boolean}
* *
* Returns: * Returns:
* {Number|String} a Number if the passed value is a number, a String * {Number|String} a Number if the passed value is a number, a String
* otherwise. * otherwise.
*/ */
numericIf: function(value) { numericIf: function(value, trimWhitespace) {
if (trimWhitespace === true) {
value = value.replace(/^\s*|\s*$/g, "");
}
return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value;
} }
+3 -3
View File
@@ -180,18 +180,18 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
}, },
"Literal": function(node, obj) { "Literal": function(node, obj) {
obj.value = OpenLayers.String.numericIf( obj.value = OpenLayers.String.numericIf(
this.getChildValue(node).replace(this.regExes.trimSpace, "")); this.getChildValue(node), true);
}, },
"PropertyName": function(node, filter) { "PropertyName": function(node, filter) {
filter.property = this.getChildValue(node); filter.property = this.getChildValue(node);
}, },
"LowerBoundary": function(node, filter) { "LowerBoundary": function(node, filter) {
filter.lowerBoundary = OpenLayers.String.numericIf( filter.lowerBoundary = OpenLayers.String.numericIf(
this.readers.ogc._expression.call(this, node).replace(this.regExes.trimSpace, "")); this.readers.ogc._expression.call(this, node), true);
}, },
"UpperBoundary": function(node, filter) { "UpperBoundary": function(node, filter) {
filter.upperBoundary = OpenLayers.String.numericIf( filter.upperBoundary = OpenLayers.String.numericIf(
this.readers.ogc._expression.call(this, node).replace(this.regExes.trimSpace, "")); this.readers.ogc._expression.call(this, node), true);
}, },
"Intersects": function(node, obj) { "Intersects": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS); this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS);
+6 -2
View File
@@ -220,9 +220,10 @@
{value: " 3", expect: " 3"}, {value: " 3", expect: " 3"},
{value: "1e", expect: "1e"}, {value: "1e", expect: "1e"},
{value: "1+e", expect: "1+e"}, {value: "1+e", expect: "1+e"},
{value: "1-e", expect: "1-e"} {value: "1-e", expect: "1-e"},
{value: " 27 ", expect: " 27 "}
]; ];
t.plan(cases.length); t.plan(cases.length + 1);
var func = OpenLayers.String.numericIf; var func = OpenLayers.String.numericIf;
var obj, val, got, exp; var obj, val, got, exp;
@@ -233,6 +234,9 @@
got = func(val); got = func(val);
t.eq(got, exp, "'" + val + "' returns " + exp); t.eq(got, exp, "'" + val + "' returns " + exp);
} }
// test the trimWhitespace option
t.eq(OpenLayers.String.numericIf(" 27 ", true), 27, "if trimWhitespace is true, we expect to get a Number");
} }