From 84025c2ce09f377e7ae2919a3157705bdf2435bb Mon Sep 17 00:00:00 2001 From: Brian Sperlongano Date: Thu, 12 Jan 2023 21:38:39 -0500 Subject: [PATCH] Add split/join --- build/generate-style-spec.ts | 2 ++ .../expression/definitions/index.ts | 10 ++++++++ src/style-spec/reference/v8.json | 18 +++++++++++++++ .../expression/tests/join/basic/test.json | 13 +++++++++++ .../expression/tests/join/coercion/test.json | 23 +++++++++++++++++++ .../expression/tests/split/basic/test.json | 13 +++++++++++ .../expression/tests/split/coercion/test.json | 23 +++++++++++++++++++ 7 files changed, 102 insertions(+) create mode 100644 test/integration/expression/tests/join/basic/test.json create mode 100644 test/integration/expression/tests/join/coercion/test.json create mode 100644 test/integration/expression/tests/split/basic/test.json create mode 100644 test/integration/expression/tests/split/coercion/test.json diff --git a/build/generate-style-spec.ts b/build/generate-style-spec.ts index 2d0ed9a0b71..d2a3ccd6cff 100644 --- a/build/generate-style-spec.ts +++ b/build/generate-style-spec.ts @@ -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 diff --git a/src/style-spec/expression/definitions/index.ts b/src/style-spec/expression/definitions/index.ts index 3767fd9069b..cefd48b7306 100644 --- a/src/style-spec/expression/definitions/index.ts +++ b/src/style-spec/expression/definitions/index.ts @@ -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], diff --git a/src/style-spec/reference/v8.json b/src/style-spec/reference/v8.json index 6cfa6349690..e308064aa9d 100644 --- a/src/style-spec/reference/v8.json +++ b/src/style-spec/reference/v8.json @@ -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" + } + } } } }, diff --git a/test/integration/expression/tests/join/basic/test.json b/test/integration/expression/tests/join/basic/test.json new file mode 100644 index 00000000000..23440266024 --- /dev/null +++ b/test/integration/expression/tests/join/basic/test.json @@ -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"] + } +} diff --git a/test/integration/expression/tests/join/coercion/test.json b/test/integration/expression/tests/join/coercion/test.json new file mode 100644 index 00000000000..5ed85aa5adc --- /dev/null +++ b/test/integration/expression/tests/join/coercion/test.json @@ -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", + "" + ] + } +} diff --git a/test/integration/expression/tests/split/basic/test.json b/test/integration/expression/tests/split/basic/test.json new file mode 100644 index 00000000000..938015e6c62 --- /dev/null +++ b/test/integration/expression/tests/split/basic/test.json @@ -0,0 +1,13 @@ +{ + "expression": ["split", ";", "a;b;c"], + "inputs": [[{}, {}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [["a", "b", "c"]] + } +} diff --git a/test/integration/expression/tests/split/coercion/test.json b/test/integration/expression/tests/split/coercion/test.json new file mode 100644 index 00000000000..19b0723a6ef --- /dev/null +++ b/test/integration/expression/tests/split/coercion/test.json @@ -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" + }, + "outputs": [ + ["a", "b", "c"], + ["a", "b"], + ["a"], + [""] + ] + } +}