Skip to content

Commit

Permalink
Implement split/join
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeLonewolf committed Jan 13, 2023
1 parent 5927b8a commit d440432
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### ✨ Features and improvements
- Add `setiClusterOptions` to update cluster properties of the added sources: fixing these issues ([#429](https://github.com/maplibre/maplibre-gl-js/issues/429)) and ([1384](https://github.com/maplibre/maplibre-gl-js/issues/1384))
- Add types for `workerOptions` and `_options` in `geojson_source.ts`
- Add `split` and `join` expressions ([#2064](https://github.com/maplibre/maplibre-gl-js/pull/2064))
- *...Add new stuff here...*
### 🐞 Bug fixes
- *...Add new stuff here...*
Expand Down
2 changes: 2 additions & 0 deletions build/generate-style-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ export type ExpressionSpecification =
| ['concat', ...(ExpressionInputType | ExpressionSpecification)[]] // at least two inputs required -> string
| ['downcase', string | ExpressionSpecification] // string
| ['is-supported-script', string | ExpressionSpecification] // boolean
| ['join', string, (string | ExpressionSpecification)[]] // string
| ['resolved-locale', CollatorExpressionSpecification] // string
| ['split', string, string | ExpressionSpecification] // string[]
| ['upcase', string | ExpressionSpecification] // string
// Color
| ['rgb', number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification] // color
Expand Down
10 changes: 10 additions & 0 deletions src/style-spec/expression/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,16 @@ CompoundExpression.register(expressions, {
varargs(ValueType),
(ctx, args) => args.map(arg => valueToString(arg.evaluate(ctx))).join('')
],
'split': [
array(StringType),
[StringType, StringType],
(ctx, [delim, s]) => s.evaluate(ctx).split(delim.evaluate(ctx))
],
'join': [
StringType,
[StringType, array(StringType)],
(ctx, [delim, arr]) => arr.evaluate(ctx).join(delim.evaluate(ctx))
],
'resolved-locale': [
StringType,
[CollatorType],
Expand Down
18 changes: 18 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,24 @@
"macos": "0.9.0"
}
}
},
"split": {
"doc": "Returns an array of strings split by the specified delimiter string",
"group": "String",
"sdk-support": {
"basic functionality": {
"js": "3.0.0"
}
}
},
"join": {
"doc": "Returns a string formed from joining the elements of an array with a specified delimiter in between",
"group": "String",
"sdk-support": {
"basic functionality": {
"js": "3.0.0"
}
}
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions test/integration/expression/tests/join/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"expression": ["join", ";", ["literal", ["a", "b", "c"]]],
"inputs": [[{}, {}]],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "string"
},
"outputs": ["a;b;c"]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression/tests/join/coercion/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": ["join", "/", ["get", "a"]],
"inputs": [
[{}, {"properties": {"a": ["a", "b", "c"]}}],
[{}, {"properties": {"a": ["a", "b"]}}],
[{}, {"properties": {"a": ["a"]}}],
[{}, {"properties": {"a": [""]}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "string"
},
"outputs": [
"a/b/c",
"a/b",
"a",
""
]
}
}
13 changes: 13 additions & 0 deletions test/integration/expression/tests/split/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"expression": ["split", ";", "a;b;c"],
"inputs": [[{}, {}, {}]],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "array<string>"
},
"outputs": [["a", "b", "c"]]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression/tests/split/coercion/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": ["split", "/", ["get", "s"]],
"inputs": [
[{}, {"properties": {"s": "a/b/c"}}],
[{}, {"properties": {"s": "a/b"}}],
[{}, {"properties": {"s": "a"}}],
[{}, {"properties": {"s": ""}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<string>"
},
"outputs": [
["a", "b", "c"],
["a", "b"],
["a"],
[""]
]
}
}

0 comments on commit d440432

Please sign in to comment.