Skip to content

Commit 4d7f8cb

Browse files
Add string.strReplaceMulti (#38)
* Add `string.strReplaceMulti` * make docs
1 parent ed544d7 commit 4d7f8cb

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

docs/string.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local string = import "github.com/jsonnet-libs/xtd/string.libsonnet"
1313
## Index
1414

1515
* [`fn splitEscape(str, c, escape='\\')`](#fn-splitescape)
16+
* [`fn strReplaceMulti(str, replacements)`](#fn-strreplacemulti)
1617

1718
## Fields
1819

@@ -24,3 +25,18 @@ splitEscape(str, c, escape='\\')
2425

2526
`split` works the same as `std.split` but with support for escaping the dividing
2627
string `c`.
28+
29+
30+
### fn strReplaceMulti
31+
32+
```ts
33+
strReplaceMulti(str, replacements)
34+
```
35+
36+
`strReplaceMulti` replaces multiple substrings in a string.
37+
38+
Example:
39+
```jsonnet
40+
strReplaceMulti('hello world', [['hello', 'goodbye'], ['world', 'universe']])
41+
// 'goodbye universe'
42+
```

string.libsonnet

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,30 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
3232
c,
3333
)
3434
),
35+
36+
'#strReplaceMulti':: d.fn(
37+
|||
38+
`strReplaceMulti` replaces multiple substrings in a string.
39+
40+
Example:
41+
```jsonnet
42+
strReplaceMulti('hello world', [['hello', 'goodbye'], ['world', 'universe']])
43+
// 'goodbye universe'
44+
```
45+
|||,
46+
[
47+
d.arg('str', d.T.string),
48+
d.arg('replacements', d.T.array),
49+
]
50+
),
51+
strReplaceMulti(str, replacements):
52+
assert std.isString(str) : 'str must be a string';
53+
assert std.isArray(replacements) : 'replacements must be an array';
54+
assert std.all([std.isArray(r) && std.length(r) == 2 && std.isString(r[0]) && std.isString(r[1]) for r in replacements]) : 'replacements must be an array of arrays of strings';
55+
std.foldl(
56+
function(acc, replacement)
57+
std.strReplace(acc, replacement[0], replacement[1]),
58+
replacements,
59+
str,
60+
),
3561
}

test/string_test.jsonnet

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local string = import '../string.libsonnet';
2+
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';
3+
4+
test.new(std.thisFile)
5+
6+
+ test.case.new(
7+
name='strReplaceMulti',
8+
test=test.expect.eq(
9+
actual=string.strReplaceMulti('hello world', [['hello', 'goodbye'], ['world', 'universe']]),
10+
expected='goodbye universe',
11+
)
12+
)
13+
+ test.case.new(
14+
name='strReplaceMulti - chained',
15+
test=test.expect.eq(
16+
actual=string.strReplaceMulti('hello world', [['hello', 'goodbye'], ['goodbye', 'hi again']]),
17+
expected='hi again world',
18+
)
19+
)
20+
+ test.case.new(
21+
name='strReplaceMulti - not found',
22+
test=test.expect.eq(
23+
actual=string.strReplaceMulti('hello world', [['first', 'second'], ['third', 'fourth']]),
24+
expected='hello world',
25+
)
26+
)
27+
+ test.case.new(
28+
name='strReplaceMulti - empty replacements',
29+
test=test.expect.eq(
30+
actual=string.strReplaceMulti('hello world', []),
31+
expected='hello world',
32+
)
33+
)

0 commit comments

Comments
 (0)