From be926c4c8190343435664c99d7c8ae6464eec48f Mon Sep 17 00:00:00 2001 From: midnightconman Date: Wed, 8 May 2024 15:09:39 -0700 Subject: [PATCH 1/3] Added string.capitalize --- string.libsonnet | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/string.libsonnet b/string.libsonnet index 6655981..35fbdec 100644 --- a/string.libsonnet +++ b/string.libsonnet @@ -12,6 +12,14 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet'; // character and thereby will unlikely be in a valid key by accident. Only when we include it. local BelRune = std.char(7), + '#capitalize':: d.fn( + ||| + `capitalize` will uppercase the first letter of string. + |||, + [d.arg('str', d.T.string)] + ), + capitalize(str): std.asciiUpper(str[0]) + std.substr(str, 1, std.length(str) - 1), + '#splitEscape':: d.fn( ||| `split` works the same as `std.split` but with support for escaping the dividing From c8fdc612a54d73aa6a57dddd108b68117e3c3515 Mon Sep 17 00:00:00 2001 From: midnightconman Date: Wed, 8 May 2024 15:13:33 -0700 Subject: [PATCH 2/3] with possible string test --- test/string_test.jsonnet | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/string_test.jsonnet diff --git a/test/string_test.jsonnet b/test/string_test.jsonnet new file mode 100644 index 0000000..43a9435 --- /dev/null +++ b/test/string_test.jsonnet @@ -0,0 +1,19 @@ +local xtd = import '../main.libsonnet'; +local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet'; + +test.new(std.thisFile) ++ test.case.new( + name='empty', + test=test.expect.eq( + actual=xtd.string.capitalize(''), + expected='', + ) +) + ++ test.case.new( + name='abc', + test=test.expect.eq( + actual=xtd.string.capitalize('abc'), + expected='Abc', + ) +) From db4613f22de7a88d07cfbe7311bf61aeb72b2ff5 Mon Sep 17 00:00:00 2001 From: midnightconman Date: Wed, 8 May 2024 15:30:45 -0700 Subject: [PATCH 3/3] with passing tests --- string.libsonnet | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/string.libsonnet b/string.libsonnet index 35fbdec..4245c6e 100644 --- a/string.libsonnet +++ b/string.libsonnet @@ -18,7 +18,8 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet'; |||, [d.arg('str', d.T.string)] ), - capitalize(str): std.asciiUpper(str[0]) + std.substr(str, 1, std.length(str) - 1), + capitalize(str): if std.length(str) > 0 then + std.asciiUpper(str[0]) + std.substr(str, 1, std.length(str) - 1) else '', '#splitEscape':: d.fn( |||