diff --git a/src/ol/expr/expression.js b/src/ol/expr/expression.js index 18508f29cf..dda7330ea6 100644 --- a/src/ol/expr/expression.js +++ b/src/ol/expr/expression.js @@ -95,6 +95,7 @@ ol.expr.lib = {}; * @enum {string} */ ol.expr.functions = { + CONCAT: 'concat', EXTENT: 'extent', FID: 'fid', GEOMETRY_TYPE: 'geometryType', @@ -108,6 +109,22 @@ ol.expr.functions = { }; +/** + * Concatenate strings. All provided arguments will be cast to string and + * concatenated. + * @param {...string} var_args Strings to concatenate. + * @return {string} All input arguments concatenated as strings. + * @this {ol.Feature} + */ +ol.expr.lib[ol.expr.functions.CONCAT] = function(var_args) { + var str = ''; + for (var i = 0, ii = arguments.length; i < ii; ++i) { + str += String(arguments[i]); + } + return str; +}; + + /** * Determine if a feature's extent intersects the provided extent. * @param {number} minX Minimum x-coordinate value. diff --git a/test/spec/ol/expr/expression.test.js b/test/spec/ol/expr/expression.test.js index 1245b9bd9d..9b396897e4 100644 --- a/test/spec/ol/expr/expression.test.js +++ b/test/spec/ol/expr/expression.test.js @@ -628,6 +628,52 @@ describe('ol.expr.lib', function() { var parse = ol.expr.parse; var evaluate = ol.expr.evaluateFeature; + describe('concat()', function() { + var feature = new ol.Feature({ + str: 'bar', + num: 42, + bool: false, + nul: null + }); + + it('concatenates strings', function() { + expect(evaluate(parse('concat(str, "after")'), feature)) + .to.be('barafter'); + expect(evaluate(parse('concat("before", str)'), feature)) + .to.be('beforebar'); + expect(evaluate(parse('concat("a", str, "b")'), feature)) + .to.be('abarb'); + }); + + it('concatenates numbers as strings', function() { + expect(evaluate(parse('concat(num, 0)'), feature)) + .to.be('420'); + expect(evaluate(parse('concat(0, num)'), feature)) + .to.be('042'); + expect(evaluate(parse('concat(42, 42)'), feature)) + .to.be('4242'); + expect(evaluate(parse('concat(str, num)'), feature)) + .to.be('bar42'); + }); + + it('concatenates booleans as strings', function() { + expect(evaluate(parse('concat(bool, "foo")'), feature)) + .to.be('falsefoo'); + expect(evaluate(parse('concat(true, str)'), feature)) + .to.be('truebar'); + expect(evaluate(parse('concat(true, false)'), feature)) + .to.be('truefalse'); + }); + + it('concatenates nulls as strings', function() { + expect(evaluate(parse('concat(nul, "foo")'), feature)) + .to.be('nullfoo'); + expect(evaluate(parse('concat(str, null)'), feature)) + .to.be('barnull'); + }); + + }); + describe('extent()', function() { var nw = new ol.Feature({