Skip to content

Document that std.stripChars/lstripChars/rstripChars accept array as chars parameter #1325

Description

@He-Pin

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:

  1. chars can be a string or an array
  2. When an array is provided, only single-character string elements are used as characters to strip
  3. Non-string and multi-character string elements are silently ignored

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions