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 "regex" expression that returns an array of match groups or… #6228

Closed
wants to merge 4 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
46 changes: 45 additions & 1 deletion src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,51 @@ CompoundExpression.register(expressions, {
StringType,
varargs(StringType),
(ctx, args) => args.map(arg => arg.evaluate(ctx)).join('')
]
],
'regex-test': {
type: BooleanType,
overloads: [
[
[StringType, StringType],
(ctx, [r, s]) => RegExp(r.evaluate(ctx)).test(s.evaluate(ctx))
], [
[StringType, StringType, StringType],
(ctx, [r, f, s]) => RegExp(r.evaluate(ctx), f.evaluate(ctx)).test(s.evaluate(ctx))
]
]
},
'regex-replace': {
type: StringType,
overloads: [
[
[StringType, StringType, StringType],
(ctx, [r, n, s]) => s.evaluate(ctx).replace(RegExp(r.evaluate(ctx)), n.evaluate(ctx))
], [
[StringType, StringType, StringType, StringType],
(ctx, [r, f, n, s]) => s.evaluate(ctx).replace(RegExp(r.evaluate(ctx), f.evaluate(ctx)), n.evaluate(ctx))
]
]
},
'regex-match': {
type: array(StringType),
overloads: [
[
[StringType, StringType],
(ctx, [r, s]) => {
const m = RegExp(r.evaluate(ctx)).exec(s.evaluate(ctx));
/* Slice will make a new array without the extra attributes of the match object */
return (m !== null) ? m.slice() : null;
}
], [
[StringType, StringType, StringType],
(ctx, [r, f, s]) => {
const m = RegExp(r.evaluate(ctx), f.evaluate(ctx)).exec(s.evaluate(ctx));
/* Slice will make a new array without the extra attributes of the match object */
return (m !== null) ? m.slice() : null;
}
]
]
}
});

module.exports = expressions;
24 changes: 24 additions & 0 deletions test/integration/expression-tests/regex-match/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"expression": ["regex-match", ["get", "r"], ["get", "s"]],
"inputs": [
[{}, {"properties": {"r": ".*", "s": "some string"}}],
[{}, {"properties": {"r": "me stri", "s": "some string"}}],
[{}, {"properties": {"r": "(.*) string", "s": "some string"}}],
[{}, {"properties": {"r": "([eosm]{4}) (string)", "s": "some string"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<string>"
},
"outputs": [
["some string"],
["me stri"],
["some string", "some"],
["some string", "some", "string"]
],
"serialized": ["regex-match", ["get", "r"], ["get", "s"]]
}
}
24 changes: 24 additions & 0 deletions test/integration/expression-tests/regex-replace/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"expression": ["regex-replace", ["get", "r"], ["get", "n"], ["get", "s"]],
"inputs": [
[{}, {"properties": {"r": "ome", "n": "uch a", "s": "some string"}}],
[{}, {"properties": {"r": "some string", "n": "other text", "s": "some string"}}],
[{}, {"properties": {"r": "ME", "n": "ch a", "s": "some string"}}],
[{}, {"properties": {"r": "string", "n": "text", "s": "some string"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "string"
},
"outputs": [
"such a string",
"other text",
"some string",
"some text"
],
"serialized": ["regex-replace", ["get", "r"], ["get", "n"], ["get", "s"]]
}
}
24 changes: 24 additions & 0 deletions test/integration/expression-tests/regex-test/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"expression": ["regex-test", ["get", "r"], ["get", "s"]],
"inputs": [
[{}, {"properties": {"r": ".*", "s": "some string"}}],
[{}, {"properties": {"r": "(.*) string", "s": "some string"}}],
[{}, {"properties": {"r": "NO.*MATCH", "s": "some string"}}],
[{}, {"properties": {"r": "SOME string", "s": "some string"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"outputs": [
true,
true,
false,
false
],
"serialized": ["regex-test", ["get", "r"], ["get", "s"]]
}
}