lib/Skilletor/JSON/OrderedHash.pm

filter: Statementstmt 87.0% 47 / 54 Branchbran 100% 14 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 88.6% 78 / 88 CC 9 SCAR 8.3
CC
9
Cov
89%
is_index_key
4.0
STORE
3.0
DELETE
3.0
CRAP 2.3
« /usr/local/lib/perl5/5.44.0/x86_64-linux/Errno.pm ↑ Summary blib/lib/Skilletor/Render/Loader.pm »
1 package Skilletor::JSON::OrderedHash;
2 # ABSTRACT: A tied hash that keeps a JavaScript object's property order
3  
4 709 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 709 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 709 our $VERSION = '0.008000';
8  
9 # A key is an array index – and so ordered before every other key, ascending by value –
10 # when it is the canonical decimal form of an integer 0 .. 2**32 - 2 (ECMA-262 §6.1.7).
11 703623 ▶sub is_index_key {
Subroutine is_index_key: called CC=4 SCAR=13.9
Pod documented
12 703623 my ( $class, $key ) = @_;
13 703623 ▶ return 0 unless defined $key && $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/ 703450 173 703623
Condition66%2/3
defined $key and $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/
!ll&&!rl&&r
0703450173
MC/DC50%1/2
defined $key and $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/
defined $key $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/
Truth table66%2/3
A and B
A= defined $keyB= $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/
ABresult
0X0
100
111
14 173 ▶ return $key <= 4294967294 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$key <= 4294967294 ? : 172 1 173
15 }
16  
17 454732 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=6.9
18 454732 my ( $class, @pairs ) = @_;
19 454732 my $self = bless { data => {}, order => [], index => {} }, $class;
20 454732 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 242284 454732 697016
21 242284 my ( $k, $v ) = splice @pairs, 0, 2;
22 242284 $self->STORE( $k, $v );
23 }
24 454732 return $self;
25 }
26  
27 963514 ▶sub FETCH { $_[0]{data}{ $_[1] } }
Subroutine FETCH: called CC=1 SCAR=0.0
28  
29 702024 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.0
30 702024 my ( $self, $key, $value ) = @_;
31 702024 $key = '' . $key;
32 702024 ▶ unless ( exists $self->{data}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{$key}) 650539 51485 702024
33 650539 ▶ if ( __PACKAGE__->is_index_key($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->is_index_key($key)) { } 154 650385 650539
34 154 $self->{index}{$key} = 1;
35 }
36 else {
37 650385 push @{ $self->{order} }, $key;
38 }
39 }
40 702024 $self->{data}{$key} = $value;
41 }
42  
43 458941 ▶sub EXISTS { exists $_[0]{data}{ $_[1] } }
Subroutine EXISTS: called CC=1 SCAR=0.0
44  
45 336 ▶sub DELETE {
Subroutine DELETE: called CC=3 SCAR=11.0
46 336 my ( $self, $key ) = @_;
47 336 ▶ return unless exists $self->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$key} 2 334 336
48 334 ▶ if ( delete $self->{index}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{'index'}{$key}) { } 1 333 334
49 # nothing else to do: index keys are ordered by value
50 }
51 else {
52 333 $self->{order} = [ grep { $_ ne $key } @{ $self->{order} } ];
53 }
54 334 return delete $self->{data}{$key};
55 }
56  
57 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
58 0 my ( $self ) = @_;
59 0 %{ $self->{data} } = ();
60 0 @{ $self->{order} } = ();
61 0 %{ $self->{index} } = ();
62 }
63  
64 418212 ▶sub keys_in_order {
Subroutine keys_in_order: called CC=1 SCAR=0.0
Pod documented
65 418212 my ( $self ) = @_;
66 418212 return ( ( sort { $a <=> $b } keys %{ $self->{index} } ), @{ $self->{order} } );
67 }
68  
69 337296 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
70 337296 my ( $self ) = @_;
71 337296 $self->{iter} = [ $self->keys_in_order ];
72 337296 return shift @{ $self->{iter} };
73 }
74  
75 288837 ▶sub NEXTKEY { shift @{ $_[0]{iter} } }
Subroutine NEXTKEY: called CC=1 SCAR=0.0
76  
77 24561 ▶sub SCALAR { scalar %{ $_[0]{data} } }
Subroutine SCALAR: called CC=1 SCAR=0.0
78  
79 709 1;
80  
81 __END__
« /usr/local/lib/perl5/5.44.0/x86_64-linux/Errno.pm ↑ Summary blib/lib/Skilletor/Render/Loader.pm »