Add a concat function for concatenating strings
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user