Skip to content

Commit f252a59

Browse files
committed
fix: document function substring and change it into a standalone function
1 parent 5608f43 commit f252a59

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

docs/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ <h1>JSON Query</h1>
179179
<a href="https://github.com/jsonquerylang/jsonquery/blob/main/reference/functions.md#split"
180180
target="_blank"><code>split(text, separator)</code></a>
181181
</li>
182+
<li>
183+
<a href="https://github.com/jsonquerylang/jsonquery/blob/main/reference/functions.md#substring"
184+
target="_blank"><code>substring(text, start, end)</code></a>
185+
</li>
182186
<li>
183187
<a href="https://github.com/jsonquerylang/jsonquery/blob/main/reference/functions.md#uniq"
184188
target="_blank"><code>uniq()</code></a>
@@ -480,7 +484,7 @@ <h3 class="title">Debugger</h3>
480484
</div>
481485

482486
<script type="module">
483-
import { jsonquery, stringify, parse, compile } from 'https://cdn.jsdelivr.net/npm/@jsonquerylang/jsonquery@4/lib/jsonquery.js'
487+
import { jsonquery, stringify, parse, compile } from '../lib/jsonquery.js'
484488
import { Formatter, FracturedJsonOptions } from 'https://cdn.jsdelivr.net/npm/fracturedjsonjs/+esm'
485489

486490
window.jsonquery = { jsonquery, stringify, parse, compile }

reference/functions.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,32 @@ jsonquery(data, 'split("a,b,c", ",")')
531531
// ["a", "b", "c"]
532532
```
533533

534+
## substring
535+
536+
Extract a substring from a string. When `end` is not provided, the length of the string will be used as `end`.
537+
538+
```text
539+
substring(text, start)
540+
substring(text, start, end)
541+
```
542+
543+
Examples:
544+
545+
```js
546+
const events = [
547+
{"type": "start", "time": "2024-11-06 23:14:00" },
548+
{"type": "initialize", "time": "2025-11-08 09:00:00" },
549+
{"type": "end", "time": "2025-11-24 10:27:00" }
550+
]
551+
552+
jsonquery(events, 'map(substring(.time, 0, 10))')
553+
// [
554+
// "2024-11-06",
555+
// "2025-11-08",
556+
// "2025-11-24"
557+
// ]
558+
```
559+
534560
## uniq
535561

536562
Create a copy of an array where all duplicates are removed.

src/functions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ export const functions: FunctionBuildersMap = {
210210
separator !== undefined ? text.split(separator) : text.trim().split(/\s+/)
211211
),
212212

213-
substring: (start: number, end: number) => (data: string) => data.slice(Math.max(start, 0), end),
213+
substring: buildFunction((text: string, start: number, end?: number) =>
214+
text.slice(Math.max(start, 0), end)
215+
),
214216

215217
uniq:
216218
() =>

test-suite/compile.test.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -543,42 +543,42 @@
543543
"category": "substring",
544544
"description": "should get a substring of a string with a start index",
545545
"input": "123456",
546-
"query": ["substring", 3],
546+
"query": ["substring", ["get"], 3],
547547
"output": "456"
548548
},
549549
{
550550
"category": "substring",
551551
"description": "should get a substring of a string with a start and end index",
552-
"input": "123456",
553-
"query": ["substring", 2, 4],
552+
"input": { "value": "123456" },
553+
"query": ["substring", ["get", "value"], 2, 4],
554554
"output": "34"
555555
},
556556
{
557557
"category": "substring",
558558
"description": "should get a substring of a string with a start exceeding the string length",
559-
"input": "123456",
560-
"query": ["substring", 10],
559+
"input": null,
560+
"query": ["substring", "123456", 10],
561561
"output": ""
562562
},
563563
{
564564
"category": "substring",
565565
"description": "should get a substring of a string with an end exceeding the string length",
566-
"input": "123456",
567-
"query": ["substring", 0, 10],
566+
"input": null,
567+
"query": ["substring", "123456", 0, 10],
568568
"output": "123456"
569569
},
570570
{
571571
"category": "substring",
572572
"description": "should get a substring of a string with an end index smaller than the start index",
573-
"input": "123456",
574-
"query": ["substring", 3, 0],
573+
"input": null,
574+
"query": ["substring", "123456", 3, 0],
575575
"output": ""
576576
},
577577
{
578578
"category": "substring",
579579
"description": "should get a substring of a string with a negative start index",
580-
"input": "123456",
581-
"query": ["substring", -2],
580+
"input": null,
581+
"query": ["substring", "123456", -2],
582582
"output": "123456"
583583
},
584584

0 commit comments

Comments
 (0)