blib/lib/Data/Dump/JavaScript.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 81.2% 26 / 32 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 88.3% 129 / 146 CC 19 SCAR 22.4
CC
19
Cov
89%
_encode_value
20.7
_encode_array
4.0
_encode_object
3.0
CRAP 9.4
↑ Summary
1 package Data::Dump::JavaScript;
2 1 $Data::Dump::JavaScript::VERSION = '0.002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Pretty printing of data structures as JavaScript
11  
12  
13 1 our @EXPORT_OK = qw( dump_javascript false true );
14  
15 # Literal names
16 # Users may override Booleans with literal 0 or 1 if desired.
17 1 our($FALSE, $TRUE) = map { bless \(my $dummy = $_), 'Data::Dump::JavaScript::_Bool' } 0, 1;
18  
19 # Escaped special character map with u2028 and u2029
20 1 my %ESCAPE = (
21 '"' => '"',
22 '\\' => '\\',
23 '/' => '/',
24 'b' => "\x08",
25 'f' => "\x0c",
26 'n' => "\x0a",
27 'r' => "\x0d",
28 't' => "\x09",
29 'u2028' => "\x{2028}",
30 'u2029' => "\x{2029}"
31 );
32 1 my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
33  
34 1 for(0x00 .. 0x1f) {
35 32 my $packed = pack 'C', $_;
36 32 ▶ $REVERSE{$packed} = sprintf '\u%.4X', $_ unless defined $REVERSE{$packed};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $REVERSE{$packed} 27 5 32
37 }
38  
39 1 my $indent_level;
40 # standard and semi-standard JS default
41 1 my $indent_count = 2;
42  
43  
44 2 ▶sub false () {$FALSE} ## no critic (prototypes)
Subroutine false: called CC=1 SCAR=0.0
Pod documented
45  
46  
47 3 ▶sub true () {$TRUE} ## no critic (prototypes)
Subroutine true: called CC=1 SCAR=0.0
Pod documented
48  
49  
50 35 ▶sub dump_javascript {
Subroutine dump_javascript: called CC=1 SCAR=0.0
Pod documented
51 35 $indent_level = 0;
52 35 Encode::encode 'UTF-8', _encode_value(shift);
53 }
54  
55 7 ▶sub _encode_key {
Subroutine _encode_key: called CC=1 SCAR=0.0
56 7 my $str = shift;
57 7 $str =~ s!([\x00-\x1f\x{2028}\x{2029}\\"/])!$REVERSE{$1}!gs;
58 7 return "$str";
59 }
60  
61 33 ▶sub _encode_array {
Subroutine _encode_array: called CC=4 SCAR=13.9
62 33 my $str = '[';
63 $str .= "\n"
64 33 ▶ if scalar @{$_[0]} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$_[0];} > 1 7 26 33
65 33 $indent_level++;
66 33 ▶ $str .= join(",\n", map { scalar @{$_[0]} > 1 ? _get_indented(_encode_value($_)) : _encode_value($_) } @{$_[0]});
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0];} > 1 ? : 15 18 33
67 33 $indent_level--;
68 33 ▶ $str .= scalar @{$_[0]} > 1
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0];} > 1 ? : 7 26 33
69 ? "\n" . _get_indented("]")
70 : ']';
71 }
72  
73 8 ▶sub _encode_object {
Subroutine _encode_object: called CC=3 SCAR=11.0
74 8 my $object = shift;
75 8 my $str = '{';
76 8 ▶ $str .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
if keys %$object > 0 6 2 8
77 if keys %$object > 0;
78 8 $indent_level++;
79 8 my @pairs = map { _get_indented(_encode_key($_)) . ': ' . _encode_value($object->{$_}) }
80 sort keys %$object;
81 #$str .= join(",\n", @pairs) . "\n";
82 8 $str .= join(",\n", @pairs);
83 8 $indent_level--;
84 8 ▶ $str .= keys %$object > 0
Branch: 1 branch
BranchTrueFalseTotal
keys %$object > 0 ? : 6 2 8
85 ? "\n" . _get_indented("}")
86 : '}';
87 8 return $str;
88 }
89  
90 15 ▶sub _encode_string {
Subroutine _encode_string: called CC=1 SCAR=0.0
91 15 my $str = shift;
92 15 $str =~ s!([\x00-\x1f\x{2028}\x{2029}\\"/])!$REVERSE{$1}!gs;
93 15 return "'$str'";
94 }
95  
96 75 ▶sub _encode_value {
Subroutine _encode_value: called CC=14 SCAR=30.3
97 75 my $value = shift;
98  
99 # Reference
100 75 ▶ if (my $ref = ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) 46 29 75
101  
102 # Object
103 46 ▶ return _encode_object($value) if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 8 38 46
104  
105 # Array
106 38 ▶ return _encode_array($value) if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 33 5 38
107  
108 # True or false
109 5 ▶ return $$value ? 'true' : 'false' if $ref eq 'SCALAR';
Branch: 2 branches
BranchTrueFalseTotal
if $ref eq "SCALAR" 0 5 5
$$value ? : 0 0 0
110 5 ▶ return $value ? 'true' : 'false' if $ref eq 'Data::Dump::JavaScript::_Bool';
Branch: 2 branches
BranchTrueFalseTotal
if $ref eq "Data::Dump::JavaScript::_Bool" 5 0 5
$value ? : 3 2 5
111  
112 # Blessed reference with TO_JSON method
113 0 ▶ if (blessed $value && (my $sub = $value->can('TO_JSON'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and my $sub = $value->can("TO_JSON")) 0 0 0
Condition0%0/3
builtin::blessed($value) and my $sub = $value->can("TO_JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and my $sub = $value->can("TO_JSON")
builtin::blessed($value) my $sub = $value->can("TO_JSON")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= my $sub = $value->can("TO_JSON")
ABresult
0X0
100
111
114 0 return _encode_value($value->$sub);
115 }
116 }
117  
118 # Null
119 29 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 26 29
120  
121  
122 # Number (bitwise operators change behavior based on the internal value type)
123  
124 26 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0 11 15 26
Condition66%4/6
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
!ll&&!rl&&r
15011
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value
!ll&&!rl&&r
15011
MC/DC33%1/3
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
B::svref_2object(\$value)->FLAGS & 12288 0 + $value eq $value $value * 0 == 0
Truth table50%2/4
A and B and C
A= B::svref_2object(\$value)->FLAGS & 12288B= 0 + $value eq $valueC= $value * 0 == 0
ABCresult
0XX0
10X0
1100
1111
125 if B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK)
126 # filter out "upgraded" strings whose numeric form doesn't strictly match
127 && 0 + $value eq $value
128 # filter out inf and nan
129 && $value * 0 == 0;
130  
131 # String
132 15 return _encode_string($value);
133 }
134  
135 35 ▶sub _get_indented {
Subroutine _get_indented: called CC=1 SCAR=0.0
136 35 return ' ' x ( $indent_level * $indent_count ) . shift;
137 }
138  
139 # Emulate boolean type
140 package Data::Dump::JavaScript::_Bool;
141 1 $Data::Dump::JavaScript::_Bool::VERSION = '0.002';
142 1 ▶use overload '""' => sub { ${$_[0]} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
143  
144  
145 1 1;
146  
147 __END__
↑ Summary