diff --git a/src/style-spec/expression/definitions/index.js b/src/style-spec/expression/definitions/index.js index e547731870d..6a4e6205416 100644 --- a/src/style-spec/expression/definitions/index.js +++ b/src/style-spec/expression/definitions/index.js @@ -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; diff --git a/test/integration/expression-tests/regex-match/basic/test.json b/test/integration/expression-tests/regex-match/basic/test.json new file mode 100644 index 00000000000..417a1e5445f --- /dev/null +++ b/test/integration/expression-tests/regex-match/basic/test.json @@ -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" + }, + "outputs": [ + ["some string"], + ["me stri"], + ["some string", "some"], + ["some string", "some", "string"] + ], + "serialized": ["regex-match", ["get", "r"], ["get", "s"]] + } +} diff --git a/test/integration/expression-tests/regex-replace/basic/test.json b/test/integration/expression-tests/regex-replace/basic/test.json new file mode 100644 index 00000000000..6333ba973e6 --- /dev/null +++ b/test/integration/expression-tests/regex-replace/basic/test.json @@ -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"]] + } +} diff --git a/test/integration/expression-tests/regex-test/basic/test.json b/test/integration/expression-tests/regex-test/basic/test.json new file mode 100644 index 00000000000..f662532a9aa --- /dev/null +++ b/test/integration/expression-tests/regex-test/basic/test.json @@ -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"]] + } +}