Skip to content

Commit

Permalink
added array support for slice operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Gupta authored and Amit Gupta committed Nov 22, 2024
1 parent a0067ea commit 0db70bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
35 changes: 32 additions & 3 deletions lib/Template/Liquid/Filters.pm
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ sub size {
=head2 C<slice>
String Processing:
Returns a substring of 1 character beginning at the index specified by the
first argument. An optional second argument specifies the length of the
substring to be returned.
Expand All @@ -852,12 +853,40 @@ end of the string:
{{ "Liquid" | slice: -3, 2 }} => ui
Array Processing:
Also works with arrays
=cut
sub slice {
my ($x, $pos, $len) = @_;
$len = 1 unless defined $len;
substr $x, $pos, $len;
my ($x, $pos, $len) = @_;
$len = 1 unless defined $len;
if(ref $x eq "ARRAY") {
my @array = @$x;
# Handle negative indices
$pos = scalar(@array) + $pos if $pos < 0;
# If pos is out of bounds, return an empty list
return () if $pos < 0 || $pos >= scalar(@array);
# If len is undefined, take everything from pos
$len = scalar(@array) - $pos unless defined $len;
# Ensure len does not exceed array bounds
$len = scalar(@array) - $pos if $pos + $len > scalar(@array);
# Slice and return the result
# print STDERR Dumper(@array[$pos .. $pos + $len - 1]);
# return array reference
return [ @array[$pos .. $pos + $len - 1] ];
}
# return string
substr $x, $pos, $len;
}
=head2 C<sort>
Expand Down
34 changes: 33 additions & 1 deletion t/0100_filters/0101_standard_filters.t
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ is( Template::Liquid->parse(
q[{{ my_array.size }} => 4]
);

# slice
# slice string
is(Template::Liquid->parse(q[{{ "Liquid" | slice: 0 }}])->render(),
'L', q[{{ "Liquid" | slice: 0 }} => L]);
is(Template::Liquid->parse(q[{{ "Liquid" | slice: 2 }}])->render(),
Expand All @@ -616,6 +616,38 @@ is(Template::Liquid->parse(q[{{ "Liquid" | slice: 2, 5 }}])->render(),
is(Template::Liquid->parse(q[{{ "Liquid" | slice: -3, 2 }}])->render(),
'ui', q[{{ "Liquid" | slice: -3, 2 }} => ui]);

# slice array
is( Template::Liquid->parse(
q[{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 0 }}]
)->render(),
'zebra',
'{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 0 }}'
);
is( Template::Liquid->parse(
q[{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{% assign first = my_array | slice: 0 %}{{ first }}]
)->render(),
'zebra',
'{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{% assign first = my_array | slice: 0 %}{{ first }}'
);
is( Template::Liquid->parse(
q[{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 0,2 }}]
)->render(),
'zebraoctopus',
'{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 0,2 }}'
);
is( Template::Liquid->parse(
q[{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 1,3 }}]
)->render(),
'octopusgiraffeSally Snake',
'{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: 1,3 }}'
);
is( Template::Liquid->parse(
q[{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: -2,2 }}]
)->render(),
'giraffeSally Snake',
'{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}{{ my_array | slice: -2,2 }}'
);

# sort
note 'For this next test, C<array> is defined as C<[10,62,14,257,65,32]>';
is( Template::Liquid->parse(q[{{array | sort | join: ', ' }}])
Expand Down

0 comments on commit 0db70bd

Please sign in to comment.