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

Implement replace-all expression #2059

Closed
wants to merge 15 commits 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### ✨ Features and improvements
- Improve performance by sending style layers to worker thread before processing it on main thread to allow parallel processing ([#2131](https://github.com/maplibre/maplibre-gl-js/pull/2131))
- Implement `replace-all` expression. ([#2059](https://github.com/maplibre/maplibre-gl-js/pull/2059))
- *...Add new stuff here...*

### 🐞 Bug fixes
Expand Down
1 change: 1 addition & 0 deletions build/generate-style-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export type ExpressionSpecification =
| ['concat', ...(ExpressionInputType | ExpressionSpecification)[]] // at least two inputs required -> string
| ['downcase', string | ExpressionSpecification] // string
| ['is-supported-script', string | ExpressionSpecification] // boolean
| ['replace-all', string | ExpressionSpecification, string | ExpressionSpecification, string | ExpressionSpecification] // string
| ['resolved-locale', CollatorExpressionSpecification] // string
| ['upcase', string | ExpressionSpecification] // string
// Color
Expand Down
4 changes: 4 additions & 0 deletions src/style-spec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## main

* Implement `replace-all` expression. ([#2059](https://github.com/maplibre/maplibre-gl-js/pull/2059))

## 17.0.2

* Fix errors when running style-spec bin scripts and added missing help. Removed unnecessary script 'gl-style-composite'. ([#1971](https://github.com/maplibre/maplibre-gl-js/pull/1971))
Expand Down
5 changes: 5 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,11 @@ CompoundExpression.register(expressions, {
varargs(ValueType),
(ctx, args) => args.map(arg => valueToString(arg.evaluate(ctx))).join('')
],
'replace-all': [
StringType,
[StringType, StringType, StringType],
(ctx, [s, regex, replacement]) => s.evaluate(ctx).replaceAll(regex.evaluate(ctx), replacement.evaluate(ctx))
],
'resolved-locale': [
StringType,
[CollatorType],
Expand Down
9 changes: 9 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,15 @@
"macos": "0.9.0"
}
}
},
"replace-all": {
"doc": "Returns a new string with all matches of a pattern replaced by a replacement. The pattern is a string to match, and the replacement is a string to replace it with.",
"group": "String",
"sdk-support": {
"basic functionality": {
"js": "3.0.0"
}
}
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions test/integration/expression/tests/replace/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"expression": ["replace-all", "cats;dogs;ferrets", ";", " and "],
"inputs": [
[{}, {}, {}, {}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "string"
},
"outputs": ["cats and dogs and ferrets"]
}
}
27 changes: 27 additions & 0 deletions test/integration/expression/tests/replace/expr/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"expression": ["replace-all", ["get", "s"], ["get", "f"], ["get", "r"]],
"inputs": [
[{}, {"properties": {"s": "cats;dogs;ferrets", "f": ";", "r": " and "}}],
[{}, {"properties": {"s": "cats dogs ferrets ", "f": " ", "r": ""}}],
[{}, {"properties": {"s": "cats dogs ferrets", "f": "?", "r": ""}}],
[{}, {"properties": {"s": "cats dogs ferrets", "f": "", "r": ""}}],
[{}, {"properties": {"s": "", "f": "", "r": ""}}],
[{}, {"properties": {"s": "cats dogs ferrets", "f": "dogs", "r": "bats"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "string"
},
"outputs": [
"cats and dogs and ferrets",
"catsdogsferrets",
"cats dogs ferrets",
"cats dogs ferrets",
"",
"cats bats ferrets"
]
}
}