Summary
The documentation for std.stripChars, std.lstripChars, and std.rstripChars currently only shows string arguments for the chars parameter. However, the reference implementation in stdlib/std.jsonnet inherently supports arrays because it uses std.member(chars, str[0]), which works with both strings and arrays.
Current behavior
When chars is an array, std.member checks if the character is an element of the array. This means:
- Single-character string elements are used as characters to strip
- Multi-character strings, numbers, booleans, null, objects, arrays are silently ignored (they never match a single character)
Examples
// Works with string (documented)
std.lstripChars("aaabcdef", "a") // "bcdef"
// Works with array (undocumented but functional)
std.lstripChars("forward", ["f", "o"]) // "rward"
std.stripChars("UwU Lel Stosh", ["h", "U", "s", {}, [], null, "w"]) // " Lel Sto"
std.lstripChars("123abc", [1, 2, 3]) // "123abc" (numbers ignored)
std.lstripChars("aabc", ["ab"]) // "aabc" (multi-char string ignored)
Cross-implementation verification
All major implementations support this behavior:
| Expression |
cpp-jsonnet |
go-jsonnet |
sjsonnet |
jrsonnet |
std.lstripChars("forward", ["f","o"]) |
"rward" |
"rward" |
"rward" |
"rward" |
std.rstripChars("cool just cool", ["o","l"]) |
"cool just c" |
"cool just c" |
"cool just c" |
"cool just c" |
std.stripChars("UwU Lel Stosh", ["h","U","s",{},[],null,"w"]) |
" Lel Sto" |
" Lel Sto" |
" Lel Sto" |
" Lel Sto" |
Proposal
Document the array parameter support in doc/ref/stdlib.html for std.stripChars, std.lstripChars, and std.rstripChars, noting that:
chars can be a string or an array
- When an array is provided, only single-character string elements are used as characters to strip
- Non-string and multi-character string elements are silently ignored
Summary
The documentation for
std.stripChars,std.lstripChars, andstd.rstripCharscurrently only shows string arguments for thecharsparameter. However, the reference implementation instdlib/std.jsonnetinherently supports arrays because it usesstd.member(chars, str[0]), which works with both strings and arrays.Current behavior
When
charsis an array,std.memberchecks if the character is an element of the array. This means:Examples
Cross-implementation verification
All major implementations support this behavior:
std.lstripChars("forward", ["f","o"])"rward""rward""rward""rward"std.rstripChars("cool just cool", ["o","l"])"cool just c""cool just c""cool just c""cool just c"std.stripChars("UwU Lel Stosh", ["h","U","s",{},[],null,"w"])" Lel Sto"" Lel Sto"" Lel Sto"" Lel Sto"Proposal
Document the array parameter support in
doc/ref/stdlib.htmlforstd.stripChars,std.lstripChars, andstd.rstripChars, noting that:charscan be a string or an array