Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support two-argument shorthand for "match" expression #6964

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/components/expression-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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 @@ -32,6 +32,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 @@ -2359,6 +2359,9 @@
"android": "6.0.0",
"ios": "4.0.0",
"macos": "0.7.0"
},
"two-argument shorthand": {
"js": "0.48.0"
}
}
},
Expand Down Expand Up @@ -2387,7 +2390,7 @@
}
},
"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": {
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
]
}
}