Skip to content

Commit 7d8c096

Browse files
Add trimstr/1 function (#3319)
1 parent de21386 commit 7d8c096

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,17 @@ sections:
17981798
input: '["fo", "foo", "barfoo", "foobar", "foob"]'
17991799
output: ['["fo","","bar","foobar","foob"]']
18001800

1801+
- title: "`trimstr(str)`"
1802+
body: |
1803+
1804+
Outputs its input with the given string removed at both ends, if it
1805+
starts or ends with it.
1806+
1807+
examples:
1808+
- program: '[.[]|trimstr("foo")]'
1809+
input: '["fo", "foo", "barfoo", "foobarfoo", "foob"]'
1810+
output: ['["fo","","bar","bar","b"]']
1811+
18011812
- title: "`trim`, `ltrim`, `rtrim`"
18021813
body: |
18031814

jq.1.prebuilt

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.jq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def fromdate: fromdateiso8601;
7676
def todate: todateiso8601;
7777
def ltrimstr($left): if startswith($left) then .[$left | length:] end;
7878
def rtrimstr($right): if endswith($right) then .[:$right | -length] end;
79+
def trimstr($val): ltrimstr($val) | rtrimstr($val);
7980
def match(re; mode): _match_impl(re; mode; false)|.[];
8081
def match($val): ($val|type) as $vt | if $vt == "string" then match($val; null)
8182
elif $vt == "array" and ($val | length) > 1 then match($val[0]; $val[1])

tests/jq.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,10 @@ split("")
15081508
["fo", "foo", "barfoo", "foobar", "foob"]
15091509
["fo","","bar","foobar","foob"]
15101510

1511+
[.[]|trimstr("foo")]
1512+
["fo", "foo", "barfoo", "foobarfoo", "foob"]
1513+
["fo","","bar","bar","b"]
1514+
15111515
[(index(","), rindex(",")), indices(",")]
15121516
"a,bc,def,ghij,klmno"
15131517
[1,13,[1,4,8,13]]

tests/man.test

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)