Skip to content

Commit

Permalink
Support two-argument shorthand for "match" expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 13, 2018
1 parent f55e548 commit 6d1e5cf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docs/components/expression-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ const types = {
'label_n: InputType | [InputType, InputType, ...], output_n: OutputType, ...',
'default: OutputType'
]
}, {
type: 'boolean',
parameters: [
'input: InputType (number or string)',
'label: InputType'
]
}],
var: [{
type: 'the type of the bound expression',
Expand Down
2 changes: 2 additions & 0 deletions src/style-spec/expression/definitions/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Match implements Expression {
}

static parse(args: Array<mixed>, context: ParsingContext) {
if (args.length === 3)
args = args.concat(true, false);
if (args.length < 5)
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
if (args.length % 2 !== 1)
Expand Down
5 changes: 4 additions & 1 deletion src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2353,14 +2353,17 @@
}
},
"match": {
"doc": "Selects the output whose label value matches the input value, or the fallback value if no match is found. The input can be any expression (e.g. `[\"get\", \"building_type\"]`). Each label must either be a single literal value or an array of literal values (e.g. `\"a\"` or `[\"c\", \"b\"]`), and those values must be all strings or all numbers. (The values `\"1\"` and `1` cannot both be labels in the same match expression.) If the input type does not match the type of the labels, the result will be the fallback value.",
"doc": "Selects the output whose label value matches the input value, or the fallback value if no match is found. The input can be any expression (e.g. `[\"get\", \"building_type\"]`). Each label must either be a single literal value or an array of literal values (e.g. `\"a\"` or `[\"c\", \"b\"]`), and those values must be all strings or all numbers. (The values `\"1\"` and `1` cannot both be labels in the same match expression.) If the input type does not match the type of the labels, the result will be the fallback value. A two-argument shorthand form is also supported, `[\"match\", input, label]`, which is equivalent to `[\"match\", input, label, true, false]`.",
"group": "Decision",
"sdk-support": {
"basic functionality": {
"js": "0.41.0",
"android": "6.0.0",
"ios": "4.0.0",
"macos": "0.7.0"
},
"two-argument shorthand": {
"js": "0.48.0"
}
}
},
Expand Down
32 changes: 25 additions & 7 deletions test/integration/expression-tests/match/arity-2/test.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
{
"expression": ["match", "x", "y"],
"inputs": [[{}, {}]],
"expression": ["match", ["get", "x"], [1, 2]],
"inputs": [
[{}, {"properties": {"x": 0}}],
[{}, {"properties": {"x": 1}}],
[{}, {"properties": {"x": 2}}],
[{}, {"properties": {"x": "1"}}]
],
"expected": {
"compiled": {
"result": "error",
"errors": [
{"key": "", "error": "Expected at least 4 arguments, but found only 2."}
]
}
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"outputs": [
false,
true,
true,
false
],
"serialized": [
"match",
["get", "x"],
[1, 2],
true,
false
]
}
}

0 comments on commit 6d1e5cf

Please sign in to comment.