Skip to content

Commit

Permalink
Auto-convert concat arguments to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 13, 2018
1 parent 9e37467 commit f42af5b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
39 changes: 19 additions & 20 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// @flow

import { NumberType, StringType, BooleanType, ColorType, ObjectType, ValueType, ErrorType, CollatorType, array, toString } from '../types';
import {
type Type,
NumberType,
StringType,
BooleanType,
ColorType,
ObjectType,
ValueType,
ErrorType,
CollatorType,
array,
toString as typeToString
} from '../types';

import { typeOf, Color, validateRGBA } from '../values';
import { typeOf, Color, validateRGBA, toString as valueToString } from '../values';
import CompoundExpression from '../compound_expression';
import RuntimeError from '../runtime_error';
import Let from './let';
Expand All @@ -25,10 +37,9 @@ import {
GreaterThanOrEqual
} from './comparison';
import { CollatorExpression } from './collator';
import { Formatted, FormatExpression } from './formatted';
import { FormatExpression } from './formatted';
import Length from './length';

import type { Type } from '../types';
import type { Varargs } from '../compound_expression';
import type { ExpressionRegistry } from '../expression';

Expand Down Expand Up @@ -108,24 +119,12 @@ CompoundExpression.register(expressions, {
'typeof': [
StringType,
[ValueType],
(ctx, [v]) => toString(typeOf(v.evaluate(ctx)))
(ctx, [v]) => typeToString(typeOf(v.evaluate(ctx)))
],
'to-string': [
StringType,
[ValueType],
(ctx, [v]) => {
v = v.evaluate(ctx);
const type = typeof v;
if (v === null) {
return '';
} else if (type === 'string' || type === 'number' || type === 'boolean') {
return String(v);
} else if (v instanceof Color || v instanceof Formatted) {
return v.toString();
} else {
return JSON.stringify(v);
}
}
(ctx, [v]) => valueToString(v.evaluate(ctx))
],
'to-boolean': [
BooleanType,
Expand Down Expand Up @@ -544,8 +543,8 @@ CompoundExpression.register(expressions, {
],
'concat': [
StringType,
varargs(StringType),
(ctx, args) => args.map(arg => arg.evaluate(ctx)).join('')
varargs(ValueType),
(ctx, args) => args.map(arg => valueToString(arg.evaluate(ctx))).join('')
],
'resolved-locale': [
StringType,
Expand Down
14 changes: 14 additions & 0 deletions src/style-spec/expression/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import assert from 'assert';

import Color from '../util/color';
import { Collator } from './definitions/collator';
import { Formatted } from './definitions/formatted';
import { NullType, NumberType, StringType, BooleanType, ColorType, ObjectType, ValueType, CollatorType, array } from './types';

import type { Type } from './types';
Expand Down Expand Up @@ -97,4 +98,17 @@ export function typeOf(value: Value): Type {
}
}

export function toString(value: Value) {
const type = typeof value;
if (value === null) {
return '';
} else if (type === 'string' || type === 'number' || type === 'boolean') {
return String(value);
} else if (value instanceof Color || value instanceof Formatted) {
return value.toString();
} else {
return JSON.stringify(value);
}
}

export { Color, Collator };
2 changes: 1 addition & 1 deletion src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,7 @@
}
},
"concat": {
"doc": "Returns a `string` consisting of the concatenation of the inputs. If any inputs are `formatted`, returns a `formatted` with default formatting options for all unformatted inputs.",
"doc": "Returns a `string` consisting of the concatenation of the inputs. Each input is converted to a string as if by `to-string`.",
"group": "String",
"sdk-support": {
"basic functionality": {
Expand Down
14 changes: 14 additions & 0 deletions test/integration/expression-tests/concat/coercion/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"expression": ["concat", ["get", "a"], ["get", "1"], ["get", "true"]],
"inputs": [[{}, {"properties": {"a": "a", "1": 1, "true": true}}]],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "string"
},
"outputs": ["a1true"],
"serialized": ["concat", ["get", "a"], ["get", "1"], ["get", "true"]]
}
}

0 comments on commit f42af5b

Please sign in to comment.