From 3b480cdfd5f0c44e4cc06b0aeb160c4512953ff3 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 13 Sep 2018 16:40:38 -0700 Subject: [PATCH] Auto-convert format arguments to strings --- .../expression/definitions/formatted.js | 4 +- .../{formatted => format}/basic/test.json | 0 .../format/coercion/test.json | 50 +++++++++++++++++++ .../{formatted => format}/to-string/test.json | 0 4 files changed, 52 insertions(+), 2 deletions(-) rename test/integration/expression-tests/{formatted => format}/basic/test.json (100%) create mode 100644 test/integration/expression-tests/format/coercion/test.json rename test/integration/expression-tests/{formatted => format}/to-string/test.json (100%) diff --git a/src/style-spec/expression/definitions/formatted.js b/src/style-spec/expression/definitions/formatted.js index d6e517479b1..ef4a74b86c3 100644 --- a/src/style-spec/expression/definitions/formatted.js +++ b/src/style-spec/expression/definitions/formatted.js @@ -1,7 +1,7 @@ // @flow import { NumberType, ValueType, FormattedType, array, StringType } from '../types'; - +import { toString } from '../values'; import type { Expression } from '../expression'; import type EvaluationContext from '../evaluation_context'; @@ -101,7 +101,7 @@ export class FormatExpression implements Expression { return new Formatted( this.sections.map(section => new FormattedSection( - section.text.evaluate(ctx) || "", + toString(section.text.evaluate(ctx)), section.scale ? section.scale.evaluate(ctx) : null, section.font ? section.font.evaluate(ctx).join(',') : null ) diff --git a/test/integration/expression-tests/formatted/basic/test.json b/test/integration/expression-tests/format/basic/test.json similarity index 100% rename from test/integration/expression-tests/formatted/basic/test.json rename to test/integration/expression-tests/format/basic/test.json diff --git a/test/integration/expression-tests/format/coercion/test.json b/test/integration/expression-tests/format/coercion/test.json new file mode 100644 index 00000000000..e7570b146ab --- /dev/null +++ b/test/integration/expression-tests/format/coercion/test.json @@ -0,0 +1,50 @@ +{ + "expression": [ + "format", + ["get", "a"], + {}, + ["get", "1"], + {}, + ["get", "true"], + {} + ], + "inputs": [[{}, {"properties": {"a": "a", "1": 1, "true": true}}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": false, + "isZoomConstant": true, + "type": "formatted" + }, + "outputs": [ + { + "sections": [ + { + "text": "a", + "scale": null, + "fontStack": null + }, + { + "text": "1", + "scale": null, + "fontStack": null + }, + { + "text": "true", + "scale": null, + "fontStack": null + } + ] + } + ], + "serialized": [ + "format", + ["get", "a"], + {}, + ["get", "1"], + {}, + ["get", "true"], + {} + ] + } +} diff --git a/test/integration/expression-tests/formatted/to-string/test.json b/test/integration/expression-tests/format/to-string/test.json similarity index 100% rename from test/integration/expression-tests/formatted/to-string/test.json rename to test/integration/expression-tests/format/to-string/test.json