File Coverage

blib/lib/Data/LnArray.pm
Criterion Covered Total %
statement 198 210 94.2
branch 45 52 86.5
condition 9 19 47.3
subroutine 41 43 95.3
pod 36 40 90.0
total 329 364 90.3


line stmt bran cond sub pod time code
1             package Data::LnArray;
2 36     36   4251256 use strict;
  36         82  
  36         1402  
3 36     36   393 no warnings;
  36         91  
  36         1836  
4 36     36   225 use base 'Import::Export';
  36         67  
  36         18867  
5             our $VERSION = '0.06';
6              
7             our %EX = (
8             arr => [qw/all/],
9             );
10              
11             sub arr {
12 1     1 1 185819 Data::LnArray->new(@_);
13             }
14              
15             sub new {
16 54     54 0 6639361 my $class = shift;
17 54         340 bless [@_], __PACKAGE__;
18             }
19              
20             sub get {
21 2     2 1 9 my ($self, $index) = @_;
22 2         20 return $self->[$index];
23             }
24              
25             sub set {
26 1     1 1 6 my ($self, $index, $val) = @_;
27 1         6 $self->[$index] = $val;
28 1         3 return $self;
29             }
30              
31             sub length {
32 23     23 1 6958 my ($self) = shift;
33              
34 23         49 scalar @{$self};
  23         156  
35             }
36              
37             sub retrieve {
38 6     6 0 32 my ($self) = shift;
39 6         20 return @{$self};
  6         35  
40             }
41              
42             sub from {
43 5     5 1 11644 my ($self) = shift;
44              
45 5         14 my ( $data, $code ) = @_;
46 5         13 my $ref = ref $data;
47             my @data
48             = !$ref ? split //, $data
49 2         4 : $ref eq 'ARRAY' ? @{$data}
50 5 100       22 : do {
    100          
51 2 100       18 die 'currently cannot handle' unless $data->{length};
52 1         6 0 .. $data->{length} - 1;
53             };
54 4 100       13 return $self->new( $code ? map { $code->($_) } @data : @data );
  8         29  
55             }
56              
57             sub isArray {
58 0     0 1 0 my ($self) = shift;
59              
60 0         0 my ($data) = @_;
61 0   0     0 my $ref = ref $data || "";
62 0 0       0 $ref eq 'ARRAY' ? \1 : \0;
63             }
64              
65             sub of {
66 1     1 1 8 my ($self) = shift;
67              
68 1         4 return $self->new(@_);
69             }
70              
71             sub copyWithin {
72 4     4 1 22 my ($self) = shift;
73              
74 4         10 my ( $target, $start, $end ) = @_;
75 4         12 my $length = $self->length;
76              
77 4 100       23 my $to
78             = $target < 0
79             ? $self->mmax( $length + $target, 0 )
80             : $self->mmin( $target, $length );
81              
82 4 100       16 my $from
83             = $start < 0
84             ? $self->mmax( $length + $start, 0 )
85             : $self->mmin( $start, $length );
86              
87 4 100       13 $end = defined $end ? $end : $length;
88 4 100       13 my $final
89             = $end < 0
90             ? $self->mmax( $length + $end, 0 )
91             : $self->mmin( $end, $length );
92              
93 4         13 my $count = $self->mmin( $final - $from, $length - $to );
94              
95 4         8 my $direction = 1;
96              
97 4 100 66     18 if ( $from < $to && $to < ( $from + $count ) ) {
98 1         3 $direction = -1;
99 1         3 $from += $count - 1;
100 1         7 $to += $count - 1;
101             }
102              
103 4         12 while ( $count > 0 ) {
104 5         11 $self->[$to] = $self->[$from];
105 5         9 $from += $direction;
106 5         7 $to += $direction;
107 5         12 $count--;
108             }
109              
110 4         13 return $self;
111             }
112              
113             sub fill {
114 3     3 1 19 my ($self) = shift;
115              
116 3         10 my ( $target, $start, $end ) = @_;
117 3         12 my $length = $self->length;
118              
119 3 100       19 my $from
120             = $start < 0
121             ? $self->mmax( $length + $start, 0 )
122             : $self->mmin( $start, $length );
123              
124 3 100       10 $end = defined $end ? $end : $length - 1;
125 3 100       11 my $final
126             = $end < 0
127             ? $self->mmax( $length + $end, 0 )
128             : $self->mmin( $end, $length );
129 3         10 while ( $from <= $final ) {
130 4         11 $self->[$from] = $target;
131 4         8 $from++;
132             }
133              
134 3         10 return $self;
135             }
136              
137             sub pop {
138 1     1 1 8 my ($self) = shift;
139              
140 1         2 pop @{$self};
  1         11  
141             }
142              
143             sub push {
144 1     1 1 8 my ($self) = shift;
145              
146 1         2 push @{$self}, @_;
  1         16  
147             }
148              
149             sub reverse {
150 2     2 1 9 my ($self) = shift;
151              
152 2         4 return $self->new( reverse @{$self} );
  2         17  
153             }
154              
155             sub shift {
156 1     1 1 7 my ($self) = shift;
157              
158 1         3 shift @{$self};
  1         10  
159             }
160              
161             sub sort {
162 1     1 1 13 my ($self) = shift;
163              
164 1         2 my $sort = shift;
165 1         3 my @array = grep { ref $_ ne 'CODE' } sort $sort, @{$self};
  5         15  
  1         13  
166 1         4 $self->new(@array);
167             }
168              
169             sub splice {
170 3     3 1 19 my ($self) = shift;
171              
172 3         8 my ( $offset, $length, $target ) = @_;
173 3 100       10 if ( defined $target ) {
174 2         4 splice @{$self}, $offset, $length, $target;
  2         13  
175             }
176             else {
177 1         2 splice @{$self}, $offset, $length;
  1         4  
178             }
179 3         16 return $self;
180             }
181              
182             sub unshift {
183 1     1 1 5 my ($self) = shift;
184              
185 1         3 my ($target) = @_;
186 1         2 unshift @{$self}, $target;
  1         26  
187 1         8 return $self;
188             }
189              
190             sub concat {
191 1     1 1 5 my ($self) = shift;
192              
193 1         2 my ($array) = @_;
194 1         2 push @{$self}, @{$array};
  1         6  
  1         3  
195 1         6 return $self;
196             }
197              
198             sub filter {
199 1     1 1 8 my ($self) = shift;
200              
201 1         2 my $grep = shift;
202 1         1 my @new;
203 1         2 for ( @{$self} ) {
  1         7  
204 4 100       10 if ( $grep->($_) ) {
205 3         8 push @new, $_;
206             }
207             }
208 1         3 return $self->new(@new);
209             }
210              
211             sub includes {
212 1     1 1 5 my ($self) = shift;
213              
214 1         2 my @match = grep { $_[0] =~ m/$_/ } @{$self};
  4         28  
  1         6  
215 1 50       5 scalar @match ? \1 : \0;
216             }
217              
218             sub indexOf {
219 1     1 1 9 my ($self) = shift;
220              
221 1         2 my $i = 0;
222 1         99 for ( @{$self} ) {
  1         12  
223 1 50       11 return $i if $_ eq $_[0];
224 0         0 $i++;
225             }
226 0         0 return -1;
227             }
228              
229             sub join {
230 2     2 1 66 my ($self) = shift;
231              
232 2         3 join $_[0], @{$self};
  2         21  
233             }
234              
235             sub lastIndexOf {
236 1     1 1 7 my ($self) = shift;
237              
238 1         3 for ( my $i = $self->length - 1; $i >= 0; $i-- ) {
239 3 100       10 return $i if $self->[$i] eq $_[0];
240             }
241             }
242              
243             sub slice {
244 1     1 1 8 my ($self) = shift;
245              
246 1         4 my ( $begin, $end ) = @_;
247 1         2 my @de = @{$self};
  1         11  
248 1         5 return $self->new( @de[ $begin .. $end ] );
249             }
250              
251             sub toString {
252 1     1 1 5 my ($self) = shift;
253              
254 1         4 return $self->join(',');
255             }
256              
257             sub toLocaleString {
258 0     0 1 0 my ($self) = shift;
259              
260 0         0 die 'TODO DateTime';
261             }
262              
263             sub entries {
264 1     1 1 8 my ($self) = shift;
265              
266 1         2 my %entries;
267 1         6 for ( my $i = $self->length - 1; $i >= 0; $i-- ) {
268 4         16 $entries{$i} = $self->[$i];
269             }
270 1         9 return %entries;
271             }
272              
273             sub every {
274 2     2 1 1191 my ($self) = shift;
275              
276 2         5 my $cb = shift;
277 2         4 for ( @{$self} ) {
  2         13  
278 6 100       28 return \0 unless $cb->($_);
279             }
280 1         5 return \1;
281             }
282              
283             sub find {
284 2     2 1 542 my ($self) = shift;
285              
286 2         3 my $cb = shift;
287 2         3 for ( @{$self} ) {
  2         7  
288 5 100       14 return $_ if $cb->($_);
289             }
290 1         3 return;
291             }
292              
293             sub findIndex {
294 2     2 1 561 my ($self) = shift;
295              
296 2         2 my $cb = shift;
297 2         3 my $i = 0;
298 2         2 for ( @{$self} ) {
  2         9  
299 5 100       7 return $i if $cb->($_);
300 4         11 $i++;
301             }
302 1         3 return;
303             }
304              
305             sub forEach {
306 1     1 1 9 my ($self) = shift;
307              
308 1         2 my ($code) = @_;
309 1         2 my @out;
310 1         7 for (@$self) {
311 4         13 push @out, $code->($_);
312             }
313 1         7 return @out;
314             }
315              
316             sub keys {
317 1     1 1 8 my ($self) = shift;
318              
319 1         5 return 0 .. $self->length - 1;
320             }
321              
322             sub map {
323 1     1 1 9 my ($self) = shift;
324              
325 1         2 my ( $cb, @new ) = (shift);
326 1         1 for ( @{$self} ) {
  1         6  
327 4         10 push @new, $cb->($_);
328             }
329 1         4 return $self->new(@new);
330             }
331              
332             sub reduce {
333 2     2 1 14 my ($self) = shift;
334              
335 2         8 my ( $cb, $reduced ) = ( shift, shift );
336 2         3 for ( @{$self} ) {
  2         14  
337 8         27 $reduced = $cb->( $reduced, $_ );
338             }
339 2         14 return $reduced;
340             }
341              
342             sub reduceRight {
343 1     1 1 14 my ($self) = shift;
344              
345 1         5 my $rev = $self->reverse;
346 1         4 return $rev->reduce(@_);
347             }
348              
349             sub some {
350 2     2 1 1263 my ($self) = shift;
351              
352 2         5 my ($cb) = (shift);
353 2         3 for ( @{$self} ) {
  2         13  
354 5 100       22 return \1 if $cb->($_);
355             }
356 1         6 return \0;
357             }
358              
359             sub values {
360 1     1 1 7 my ($self) = shift;
361 1         1 return @{$self};
  1         9  
362             }
363              
364             sub mmax {
365 5     5 0 13 my ($self) = shift;
366 5         12 my $caller = caller();
367 5         10 my @allowed = qw//;
368 5 50 33     20 unless ( $caller eq __PACKAGE__ || grep { $_ eq $caller } @allowed ) {
  0         0  
369 0         0 die "cannot call private method mmax from $caller";
370             }
371 5 50 50     40 $_[ ( $_[0] || 0 ) < ( $_[1] || 0 ) ] || 0;
      50        
372             }
373              
374             sub mmin {
375 17     17 0 35 my ($self) = shift;
376 17         30 my $caller = caller();
377 17         57 my @allowed = qw//;
378 17 50 33     54 unless ( $caller eq __PACKAGE__ || grep { $_ eq $caller } @allowed ) {
  0         0  
379 0         0 die "cannot call private method mmin from $caller";
380             }
381 17 100 100     78 $_[ ( $_[0] || 0 ) > ( $_[1] || 0 ) ] || 0;
      50        
382             }
383              
384             1;
385              
386             =head1 NAME
387              
388             Data::LnArray - The great new Data::LnArray!
389              
390             =head1 VERSION
391              
392             Version 0.06
393              
394             =cut
395              
396             =head1 SYNOPSIS
397              
398             use Data::LnArray;
399              
400             my $foo = Data::LnArray->new(qw/last night in paradise/);
401            
402              
403             $foo->push('!');
404              
405             ...
406              
407             use Data::LnArray qw/all/;
408              
409             my $okay = arr(qw/one two three/);
410              
411             =head1 Exports
412              
413             =head2 arr
414              
415             Shorthand for generating a new Data::LnArray Object.
416              
417             my $dlna = arr(qw/.../);
418              
419             $dlna->$method;
420              
421              
422             =head1 SUBROUTINES/METHODS
423              
424             =head2 get
425              
426             Returns the value of the passed index
427              
428             $foo->get(0);
429              
430             =head2 set
431              
432             Sets the value of the passed index.
433              
434             $foo->set(0, 'patience');
435              
436             =head2 length
437              
438             Returns an Integer that represents the length of the array.
439              
440             $foo->length;
441              
442             =head2 from
443              
444             Creates a new Data::LnArray instance from a string, array reference or hash reference.
445              
446             Data::LnArray->from(qw/foo/); # ['f', 'o', 'o']
447            
448             $foo->from([qw/one two three four/]); # ['one', 'two', 'three', 'four']
449            
450             $foo->from([qw/1 2 3/], sub { $_ + $_ }); # [2, 4, 6]
451              
452             $foo->from({length => 5}, sub { $_ + $_ }); # [0, 2, 4, 6, 8]
453              
454             =head2 isArray
455              
456             Returns a boolean, true if value is an array or false otherwise.
457              
458             $foo->isArray($other);
459              
460             =head2 of
461              
462             Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
463              
464             my $new = $array->of(qw/one two three four/);
465              
466             =head2 copyWithin
467              
468             Copies a sequence of array elements within the array.
469              
470             my $foo = Data::LnArray->new(qw/one two three four/);
471             my $bar = $foo->copyWithin(0, 2, 3); # [qw/three four three four/];
472              
473             ...
474              
475             my $foo = Data::LnArray->new(1, 2, 3, 4, 5);
476             my $bar = $array->copyWithin(-2, -3, -1); # [1, 2, 3, 3, 4]
477              
478             =head2 fill
479              
480             Fills all the elements of an array from a start index to an end index with a static value.
481              
482             my $foo = Data::LnArray->new(1, 2, 3, 4, 5);
483             $foo->fill(0, 2) # 0, 0, 0, 4, 5
484              
485             =head2 pop
486              
487             Removes the last element from an array and returns that element.
488              
489             $foo->pop;
490              
491             =head2 push
492              
493             Adds one or more elements to the end of an array, and returns the new length of the array.
494              
495             $foo->push(@new);
496              
497             =head2 reverse
498              
499             Reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)
500              
501             $foo->reverse;
502              
503             =head2 shift
504              
505             Removes the first element from an array and returns that element.
506              
507             $foo->shift;
508              
509             =head2 sort
510              
511             Sorts the elements of an array in place and returns the array.
512              
513             $foo->sort(sub {
514             $a <=> $b
515             });
516              
517             =head2 splice
518              
519             Adds and/or removes elements from an array.
520              
521             $foo->splice(0, 1, 'foo');
522              
523             =head2 unshift
524              
525             Adds one or more elements to the front of an array, and returns the new length of the array.
526              
527             $foo->unshift;
528              
529             =head2 concat
530              
531             Returns a new array that is this array joined with other array(s) and/or value(s).
532              
533             $foo->concat($bar);
534              
535             =head2 filter
536              
537             Returns a new array containing all elements of the calling array for which the provided filtering callback returns true.
538              
539             $foo->filter(sub {
540             $_ eq 'one'
541             });
542              
543             =head2 includes
544              
545             Determines whether the array contains the value to find, returning true or false as appropriate.
546              
547             $foo->includes('one');
548              
549             =head2 indexOf
550              
551             Returns the first (least) index of an element within the array equal to search string, or -1 if none is found.
552              
553             $foo->indexOf('one');
554              
555             =head2 join
556              
557             Joins all elements of an array into a string.
558              
559             $foo->join('|');
560              
561             =head2 lastIndexOf
562              
563             Returns the last (greatest) index of an element within the array equal to search string, or -1 if none is found.
564              
565             $foo->lastIndexOf('two');
566              
567             =head2 slice
568              
569             Extracts a section of the calling array and returns a new array.
570              
571             $foo->slice(0, 2);
572              
573             =head2 toString
574              
575             Returns a string representing the array and its elements.
576              
577             $foo->toString;
578              
579             =head2 toLocaleString
580              
581             Returns a localized string representing the array and its elements. Overrides the Object.prototype.toLocaleString() method.
582              
583             TODO
584              
585             =head2 entries()
586              
587             Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
588              
589             $foo->entries;
590             # {
591             # 0 => 'one',
592             # 1 => 'two'
593             # }
594              
595             =head2 every
596              
597             Returns true if every item in this array satisfies the testing callback.
598              
599             $foo->every(sub { ... });
600              
601             =head2 find
602              
603             Returns the found item in the array if some item in the array satisfies the testing callbackFn, or undefined if not found.
604              
605             $foo->find(sub { ... });
606              
607             =head2 findIndex
608              
609             Returns the found index in the array, if an item in the array satisfies the testing callback, or -1 if not found.
610              
611             $foo->findIndex(sub { ... });
612              
613             =head2 forEach
614              
615             Calls a callback for each element in the array.
616              
617             $foo->forEach(sub { ... });
618              
619             =head2 keys
620              
621             Returns a new Array that contains the keys for each index in the array.
622              
623             $foo->keys();
624              
625             =head2 map
626              
627             Returns a new array containing the results of calling the callback on every element in this array.
628              
629             my %hash = $foo->map(sub { ... });
630              
631             =head2 reduce
632              
633             Apply a callback against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
634              
635             my $str = $foo->reduce(sub { $_[0] + $_[1] });
636              
637             =head2 reduceRight
638              
639             Apply a callback against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
640              
641             my $str = $foo->reduceRight(sub { ... });
642              
643             =head2 some
644              
645             Returns true if at least one element in this array satisfies the provided testing callback.
646              
647             my $bool = $foo->some(sub { ... });
648              
649             =head2 values
650              
651             Returns the raw Array(list) of the Data::LnArray Object.
652              
653             my @values = $foo->values;
654              
655             =head1 AUTHOR
656              
657             LNATION, C<< >>
658              
659             =head1 BUGS
660              
661             Please report any bugs or feature requests to C, or through
662             the web interface at L. I will be notified, and then you'll
663             automatically be notified of progress on your bug as I make changes.
664              
665             =head1 SUPPORT
666              
667             You can find documentation for this module with the perldoc command.
668              
669             perldoc Data::LnArray
670              
671             You can also look for information at:
672              
673             =over 2
674              
675             =item * RT: CPAN's request tracker (report bugs here)
676              
677             L
678              
679             =item * Search CPAN
680              
681             L
682              
683             =back
684              
685             =head1 ACKNOWLEDGEMENTS
686              
687             MDN Array
688             L
689              
690             =head1 LICENSE AND COPYRIGHT
691              
692             This software is Copyright (c) 2020->2025 by LNATION.
693              
694             This is free software, licensed under:
695              
696             The Artistic License 2.0 (GPL Compatible)
697              
698             =cut