Add trimstr/1 function (#3319)

This commit is contained in:
Gabriel Marin authored and GitHub committed 2025-05-21 18:46:04 +09:00
1 parent de21386681
commit 7d8c096e48
5 files changed
+35

No files matched your search

+11
View File
@@ -1798,6 +1798,17 @@ sections:
input: '["fo", "foo", "barfoo", "foobar", "foob"]' input: '["fo", "foo", "barfoo", "foobar", "foob"]'
output: ['["fo","","bar","foobar","foob"]'] output: ['["fo","","bar","foobar","foob"]']
- title: "`trimstr(str)`"
body: |
Outputs its input with the given string removed at both ends, if it
starts or ends with it.
examples:
- program: '[.[]|trimstr("foo")]'
input: '["fo", "foo", "barfoo", "foobarfoo", "foob"]'
output: ['["fo","","bar","bar","b"]']
- title: "`trim`, `ltrim`, `rtrim`" - title: "`trim`, `ltrim`, `rtrim`"
body: | body: |
Generated
+15
View File
@@ -1959,6 +1959,21 @@ jq \'[\.[]|rtrimstr("foo")]\'
. .
.IP "" 0 .IP "" 0
. .
.SS "trimstr(str)"
Outputs its input with the given string removed at both ends, if it starts or ends with it\.
.
.IP "" 4
.
.nf
jq \'[\.[]|trimstr("foo")]\'
["fo", "foo", "barfoo", "foobarfoo", "foob"]
=> ["fo","","bar","bar","b"]
.
.fi
.
.IP "" 0
.
.SS "trim, ltrim, rtrim" .SS "trim, ltrim, rtrim"
\fBtrim\fR trims both leading and trailing whitespace\. \fBtrim\fR trims both leading and trailing whitespace\.
. .
+1
View File
@@ -76,6 +76,7 @@ def fromdate: fromdateiso8601;
def todate: todateiso8601; def todate: todateiso8601;
def ltrimstr($left): if startswith($left) then .[$left | length:] end; def ltrimstr($left): if startswith($left) then .[$left | length:] end;
def rtrimstr($right): if endswith($right) then .[:$right | -length] end; def rtrimstr($right): if endswith($right) then .[:$right | -length] end;
def trimstr($val): ltrimstr($val) | rtrimstr($val);
def match(re; mode): _match_impl(re; mode; false)|.[]; def match(re; mode): _match_impl(re; mode; false)|.[];
def match($val): ($val|type) as $vt | if $vt == "string" then match($val; null) def match($val): ($val|type) as $vt | if $vt == "string" then match($val; null)
elif $vt == "array" and ($val | length) > 1 then match($val[0]; $val[1]) elif $vt == "array" and ($val | length) > 1 then match($val[0]; $val[1])
+4
View File
@@ -1508,6 +1508,10 @@ split("")
["fo", "foo", "barfoo", "foobar", "foob"] ["fo", "foo", "barfoo", "foobar", "foob"]
["fo","","bar","foobar","foob"] ["fo","","bar","foobar","foob"]
[.[]|trimstr("foo")]
["fo", "foo", "barfoo", "foobarfoo", "foob"]
["fo","","bar","bar","b"]
[(index(","), rindex(",")), indices(",")] [(index(","), rindex(",")), indices(",")]
"a,bc,def,ghij,klmno" "a,bc,def,ghij,klmno"
[1,13,[1,4,8,13]] [1,13,[1,4,8,13]]
+4
View File
@@ -617,6 +617,10 @@ combinations(2)
["fo", "foo", "barfoo", "foobar", "foob"] ["fo", "foo", "barfoo", "foobar", "foob"]
["fo","","bar","foobar","foob"] ["fo","","bar","foobar","foob"]
[.[]|trimstr("foo")]
["fo", "foo", "barfoo", "foobarfoo", "foob"]
["fo","","bar","bar","b"]
trim, ltrim, rtrim trim, ltrim, rtrim
" abc " " abc "
"abc" "abc"