10000 Support negative length in slice filter. · twigjs/twig.js@8abe3ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 8abe3ef

Browse files
Support negative length in slice filter.
1 parent 9db42d1 commit 8abe3ef

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/twig.filters.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,13 @@ module.exports = function (Twig) {
644644
// Default to start of string
645645
const start = params[0] || 0;
646646
// Default to length of string
647-
const length = params.length > 1 ? params[1] : value.length;
647+
let length = params.length > 1 ? params[1] : value.length;
648648
// Handle negative start values
649649
const startIndex = start >= 0 ? start : Math.max(value.length + start, 0);
650+
// Handle negative length values
651+
if (length < 0) {
652+
length = value.length - startIndex + length;
653+
}
650654

651655
if (Twig.lib.is('Array', value)) {
652656
const output = [];

0 commit comments

Comments
 (0)
0