add shuffle to the array wrapper class
authorTony Cook <tony@develop-help.com>
Sun, 27 Oct 2013 02:32:57 +0000 (13:32 +1100)
committerTony Cook <tony@develop-help.com>
Sun, 27 Oct 2013 02:32:57 +0000 (13:32 +1100)
site/cgi-bin/modules/Squirrel/Template/Expr/WrapArray.pm
t/020-templater/040-original.t

index 88de687a6c9f0b3101f7ce11d66ecc7a0c677de3..5b1a2223229686a376749742247f5c8120e05cb6 100644 (file)
@@ -2,8 +2,9 @@ package Squirrel::Template::Expr::WrapArray;
 use strict;
 use base qw(Squirrel::Template::Expr::WrapBase);
 use Scalar::Util ();
+use List::Util ();
 
-our $VERSION = "1.006";
+our $VERSION = "1.007";
 
 my $list_make_key = sub {
   my ($item, $field) = @_;
@@ -57,6 +58,15 @@ sub _do_reverse {
   return [ reverse @{$self->[0]} ];
 }
 
+sub _do_shuffle {
+  my ($self, $args) = @_;
+
+  @$args == 0
+    or die [ error => "list.shuffle takes no parameters" ];
+
+  return [ List::Util::shuffle(@{$self->[0]}) ];
+}
+
 sub _do_join {
   my ($self, $args) = @_;
 
index 546a3e51c4bb314cec881110b7f6fbe13d1c61e9..c0bdee8d68012f809182ddbf52c49b1de502c30d 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 # Basic tests for Squirrel::Template
 use strict;
-use Test::More tests => 184;
+use Test::More tests => 185;
 use HTML::Entities;
 
 sub template_test($$$$;$$);
@@ -602,6 +602,7 @@ OUT
      [ '[ [ 1, 2 ], 3 ].expand.join(",")', "1,2,3" ],
      [ '[ 1, 2 ].is_list', 1 ],
      [ '[ 1, 2 ].is_hash', 0 ],
+     [ '[ 1 .. 5 ].shuffle.size', 5 ],
 
      # WrapHash
      [ '{ "foo": 1 }.is_list', 0 ],