Skip to content

Commit 57da10d

Browse files
KetchupBombsbarzowski
authored andcommitted
splitLimitR in standard library
1 parent 997ecd9 commit 57da10d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,18 @@ local html = import 'html.libsonnet';
382382
},
383383
],
384384
},
385+
{
386+
name: 'splitLimitR',
387+
params: ['str', 'c', 'maxsplits'],
388+
availableSince: 'upcoming',
389+
description: 'As <code>std.splitLimit(str, c, maxsplits)</code> but will split from right to left.',
390+
examples: [
391+
{
392+
input: @'std.splitLimitR("/_foo/_bar", "/_", 1)',
393+
output: std.splitLimitR('/_foo/_bar', '/_', 1),
394+
},
395+
],
396+
},
385397
{
386398
name: 'strReplace',
387399
params: ['str', 'from', 'to'],

doc/ref/stdlib.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,35 @@ <h4 id="splitLimit">
833833
</div>
834834
</div>
835835

836+
<div class="hgroup">
837+
<div class="hgroup-inline">
838+
<div class="panel">
839+
<h4 id="splitLimitR">
840+
std.splitLimitR(str, c, maxsplits)
841+
</h4>
842+
</div>
843+
<div style="clear: both"></div>
844+
</div>
845+
</div>
846+
<div class="hgroup">
847+
<div class="hgroup-inline">
848+
<div class="panel">
849+
<p>
850+
<em>
851+
Available in upcoming release.
852+
</em>
853+
</p>
854+
<p>
855+
As <code>std.splitLimit(str, c, maxsplits)</code> but will split from right to left.
856+
</p>
857+
<p>
858+
Example: <code>std.splitLimitR("/_foo/_bar", "/_", 1)</code> yields <code>[ "/_foo", "bar" ]</code>.
859+
</p>
860+
</div>
861+
<div style="clear: both"></div>
862+
</div>
863+
</div>
864+
836865
<div class="hgroup">
837866
<div class="hgroup-inline">
838867
<div class="panel">

stdlib/std.jsonnet

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ limitations under the License.
132132
aux(idx + 1, ret, val + str[idx]);
133133
aux(0, [], ''),
134134

135+
splitLimitR(str, c, maxsplits)::
136+
assert std.isString(str) : 'str.splitLimitR first parameter must be a String, got ' + std.type(str);
137+
assert std.isString(c) : 'str.splitLimitR second parameter must be a String, got ' + std.type(c);
138+
assert std.length(c) >= 1 : 'std.splitLimitR second parameter must have length 1 or greater, got ' + std.length(c);
139+
assert std.isNumber(maxsplits) : 'str.splitLimitR third parameter must be a Number, got ' + std.type(maxsplits);
140+
if maxsplits == -1 then
141+
std.splitLimit(str, c, -1)
142+
else
143+
local revStr(str) = std.join('', std.reverse(str));
144+
std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),
145+
135146
strReplace(str, from, to)::
136147
assert std.isString(str);
137148
assert std.isString(from);

test_suite/stdlib.jsonnet

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ std.assertEqual(std.splitLimit('/foo/', '/', 1), ['', 'foo/']) &&
488488
std.assertEqual(std.splitLimit('foo/_bar', '/_', 1), ['foo', 'bar']) &&
489489
std.assertEqual(std.splitLimit('/_foo/_', '/_', 1), ['', 'foo/_']) &&
490490

491+
std.assertEqual(std.splitLimitR('foo/bar', '/', 1), ['foo', 'bar']) &&
492+
std.assertEqual(std.splitLimitR('/foo/', '/', 1), ['/foo', '']) &&
493+
std.assertEqual(std.splitLimitR('/foo/', '/', -1), ['', 'foo', '']) &&
494+
std.assertEqual(std.splitLimitR('foo/_bar', '/_', 1), ['foo', 'bar']) &&
495+
std.assertEqual(std.splitLimitR('/_foo/_', '/_', 1), ['/_foo', '']) &&
496+
std.assertEqual(std.splitLimitR('/_foo/_', '/_', -1), ['', 'foo', '']) &&
497+
491498
local some_toml = {
492499
key: 'value',
493500
simple: { t: 5 },

0 commit comments

Comments
 (0)