]> git.imager.perl.org - bse.git/commitdiff
add an expand method to arrays
authorTony Cook <tony@develop-help.com>
Tue, 6 Nov 2012 11:47:15 +0000 (22:47 +1100)
committerTony Cook <tony@develop-help.com>
Tue, 6 Nov 2012 11:47:15 +0000 (22:47 +1100)
site/cgi-bin/modules/Squirrel/Template/Expr/WrapArray.pm
t/020-templater/040-original.t

index 0b933fa5a622012f5cea077ed1c134ca5dd41182..455e48d53ed7440b9acfea7c8df368abbdf0b2de 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use base qw(Squirrel::Template::Expr::WrapBase);
 use Scalar::Util ();
 
-our $VERSION = "1.001";
+our $VERSION = "1.002";
 
 my $list_make_key = sub {
   my ($item, $field) = @_;
@@ -117,6 +117,23 @@ sub _do_unshift {
   return scalar(@{$self->[0]});
 }
 
+sub _do_expand {
+  my ($self, $args) = @_;
+
+  @$args == 0
+    or die [ error => "list.expand takes no parameters" ];
+
+  return 
+    [ map {
+      defined 
+       && ref
+         && !Scalar::Util::blessed($_)
+           && Scalar::Util::reftype($_) eq 'ARRAY'
+             ? @$_
+               : $_
+    } @{$self->[0]} ];
+}
+
 sub call {
   my ($self, $method, $args) = @_;
 
@@ -209,6 +226,12 @@ Remove the last element from the list and return that.
 Add the given elements to the start of the array.  returns the new
 size of the array.
 
+=item expand
+
+Return a new array with any contained arrays expanded one level.
+
+  [ [ [ 1 ], 2 ], 3 ].expand => [ [ 1 ], 2, 3 ]
+
 =back
 
 =head1 SEE ALSO
index 3724dd8beed243024a609ab41953ab4fff689063..a5ffd23049bd4b5436014d104047fd5d30dedc8b 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 # Basic tests for Squirrel::Template
 use strict;
-use Test::More tests => 155;
+use Test::More tests => 156;
 
 sub template_test($$$$;$$);
 
@@ -508,6 +508,9 @@ OUT
      [ '(10).ceil', 10 ],
      [ '(10.1).ceil', 11 ],
      [ '(-10.1).ceil', -10 ],
+
+     # WrapArray
+     [ '[ [ 1, 2 ], 3 ].expand.join(",")', "1,2,3" ],
     );
   for my $test (@expr_tests) {
     my ($expr, $result) = @$test;