-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstring.libsonnet
44 lines (40 loc) · 1.29 KB
/
string.libsonnet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
{
'#': d.pkg(
name='string',
url='github.com/jsonnet-libs/xtd/string.libsonnet',
help='`string` implements helper functions for processing strings.',
),
// BelRune is a string of the Ascii character BEL which made computers ring in ancient times.
// We use it as "magic" char to temporarily replace an escaped string as it is a non printable
// 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): if std.length(str) > 0 then
std.asciiUpper(str[0]) + std.substr(str, 1, std.length(str) - 1) else '',
'#splitEscape':: d.fn(
|||
`split` works the same as `std.split` but with support for escaping the dividing
string `c`.
|||,
[
d.arg('str', d.T.string),
d.arg('c', d.T.string),
d.arg('escape', d.T.string, default='\\'),
]
),
splitEscape(str, c, escape='\\'):
std.map(
function(i)
std.strReplace(i, BelRune, escape + c),
std.split(
std.strReplace(str, escape + c, BelRune),
c,
)
),
}