Skip to content

Commit 7bcff0f

Browse files
committed
feat: turn function text | split(separator) into a standalone function split(text, separator)
1 parent 8104582 commit 7bcff0f

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

reference/functions.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,25 @@ jsonquery(data, 'map(.name) | join(", ")')
510510

511511
## split
512512

513-
Divide a string into an array substrings, separated by a separator.
513+
Divide a string into an array substrings, separated by a separator. When no separator is provided, the string is split in words separated by one or multiple whitespaces, and the words are trimmed so they do not contain whitespace at the start or the end. When the separator is an empty string, the text will be split into a list with its individual characters.
514514

515515
```text
516-
split()
517-
split(separator)
516+
split(text)
517+
split(text, separator)
518518
```
519519

520-
Example:
520+
Examples:
521521

522522
```js
523-
const data = "hi there how are you doing?"
524-
jsonquery(data, 'split(" ")')
523+
const data = {
524+
"message": "hi there how are you doing?"
525+
}
526+
jsonquery(data, 'split(.message)')
525527
// ["hi", "there", "how", "are", "you", "doing?"]
528+
529+
530+
jsonquery(data, 'split("a,b,c", ",")')
531+
// ["a", "b", "c"]
526532
```
527533

528534
## uniq

src/functions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,9 @@ export const functions: FunctionBuildersMap = {
206206
(data: T[]) =>
207207
data.join(separator),
208208

209-
split: (separator?: string) =>
210-
separator !== undefined
211-
? (data: string) => data.split(separator)
212-
: (data: string) => data.trim().split(/\s+/),
209+
split: buildFunction((text: string, separator?: string) =>
210+
separator !== undefined ? text.split(separator) : text.trim().split(/\s+/)
211+
),
213212

214213
substring: (start: number, end: number) => (data: string) => data.slice(Math.max(start, 0), end),
215214

test-suite/compile.test.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,30 +505,37 @@
505505

506506
{
507507
"category": "split",
508-
"description": "should split a string",
509-
"input": "start with a b c",
510-
"query": ["split"],
508+
"description": "should split a string (1)",
509+
"input": null,
510+
"query": ["split", "start with a b c"],
511+
"output": ["start", "with", "a", "b", "c"]
512+
},
513+
{
514+
"category": "split",
515+
"description": "should split a string (2)",
516+
"input": { "message": "start with a b c" },
517+
"query": ["split", ["get", "message"]],
511518
"output": ["start", "with", "a", "b", "c"]
512519
},
513520
{
514521
"category": "split",
515522
"description": "should split a string with multiple whitespaces between the words",
516-
"input": " \n\n\t start with a b \n\r\t c \n\n\t ",
517-
"query": ["split"],
523+
"input": null,
524+
"query": ["split", " \n\n\t start with a b \n\r\t c \n\n\t "],
518525
"output": ["start", "with", "a", "b", "c"]
519526
},
520527
{
521528
"category": "split",
522529
"description": "should split a string by individual characters",
523-
"input": "abc",
524-
"query": ["split", ""],
530+
"input": null,
531+
"query": ["split", "abc", ""],
525532
"output": ["a", "b", "c"]
526533
},
527534
{
528535
"category": "split",
529536
"description": "should split a string with a separator",
530-
"input": "a,b,c",
531-
"query": ["split", ","],
537+
"input": null,
538+
"query": ["split", "a,b,c", ","],
532539
"output": ["a", "b", "c"]
533540
},
534541

0 commit comments

Comments
 (0)