Skip to content

Commit 8f1e262

Browse files
authored
Add std.trim for string (#1067)
* Add std.trim for string
1 parent 1fc6e49 commit 8f1e262

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ local html = import 'html.libsonnet';
371371
Returns true if the the given string is of zero length.
372372
|||,
373373
},
374+
{
375+
name: 'trim',
376+
params: ['str'],
377+
availableSince: 'upcoming',
378+
description: |||
379+
Returns a copy of string after eliminating leading and trailing whitespaces.
380+
|||,
381+
},
374382
{
375383
name: 'equalsIgnoreCase',
376384
params: ['str1', 'str2'],

stdlib/std.jsonnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,4 +1759,6 @@ limitations under the License.
17591759
for k in std.objectFields(obj)
17601760
if k != key
17611761
},
1762+
1763+
trim(str):: std.stripChars(str, ' \t\n\f\r\u0085\u00A0'),
17621764
}

test_suite/stdlib.jsonnet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,4 +1575,10 @@ std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&
15751575

15761576
std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&
15771577

1578+
std.assertEqual(std.trim('already trimmed string'), 'already trimmed string') &&
1579+
std.assertEqual(std.trim(' string with spaces on both ends '), 'string with spaces on both ends') &&
1580+
std.assertEqual(std.trim('string with newline character at end\n'), 'string with newline character at end') &&
1581+
std.assertEqual(std.trim('string with tabs at end\t\t'), 'string with tabs at end') &&
1582+
std.assertEqual(std.trim('string with other special whitespaces at end\f\r\u0085\u00A0'), 'string with carriage return at end') &&
1583+
15781584
true

0 commit comments

Comments
 (0)