line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Valiemon::Primitives; |
2
|
25
|
|
|
25
|
|
175
|
use strict; |
|
25
|
|
|
|
|
51
|
|
|
25
|
|
|
|
|
724
|
|
3
|
25
|
|
|
25
|
|
123
|
use warnings; |
|
25
|
|
|
|
|
46
|
|
|
25
|
|
|
|
|
561
|
|
4
|
25
|
|
|
25
|
|
119
|
use utf8; |
|
25
|
|
|
|
|
54
|
|
|
25
|
|
|
|
|
134
|
|
5
|
|
|
|
|
|
|
|
6
|
25
|
|
|
25
|
|
832
|
use Scalar::Util qw(looks_like_number); |
|
25
|
|
|
|
|
64
|
|
|
25
|
|
|
|
|
1380
|
|
7
|
25
|
|
|
25
|
|
10504
|
use Types::Serialiser; |
|
25
|
|
|
|
|
76771
|
|
|
25
|
|
|
|
|
882
|
|
8
|
25
|
|
|
25
|
|
13266
|
use Test::Deep qw(eq_deeply); |
|
25
|
|
|
|
|
186012
|
|
|
25
|
|
|
|
|
157
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
228
|
|
|
228
|
0
|
33790
|
my ($class, $options) = @_; |
12
|
228
|
|
100
|
|
|
1714
|
return bless { |
13
|
|
|
|
|
|
|
options => $options || +{}, |
14
|
|
|
|
|
|
|
}, $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub is_object { |
18
|
98
|
|
|
98
|
0
|
226
|
my ($self, $obj) = @_; |
19
|
98
|
100
|
100
|
|
|
717
|
(defined $obj && ref $obj eq 'HASH') ? 1 : 0; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub is_array { |
23
|
53
|
|
|
53
|
0
|
121
|
my ($self, $obj) = @_; |
24
|
53
|
100
|
100
|
|
|
381
|
(defined $obj && ref $obj eq 'ARRAY') ? 1 : 0; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub is_string { |
28
|
527
|
|
|
527
|
0
|
2428
|
my ($self, $obj) = @_; |
29
|
527
|
100
|
100
|
|
|
2877
|
(defined $obj && ref $obj eq '') ? 1 : 0; # really? |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub is_number { |
33
|
538
|
|
|
538
|
0
|
2276
|
my ($self, $obj) = @_; |
34
|
|
|
|
|
|
|
# avoid from JSON::Boolean treated as number. |
35
|
538
|
100
|
100
|
|
|
6313
|
(defined $obj && ref $obj eq '' && looks_like_number($obj)) ? 1 : 0; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub is_integer { |
39
|
263
|
|
|
263
|
0
|
1967
|
my ($self, $obj) = @_; |
40
|
263
|
100
|
100
|
|
|
580
|
$self->is_number($obj) && $obj =~ qr/^-?\d+$/ ? 1 : 0; # TODO find more better way |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub is_boolean { |
44
|
343
|
|
|
343
|
0
|
778
|
my ($self, $obj) = @_; |
45
|
|
|
|
|
|
|
return $self->{options}->{use_json_boolean} |
46
|
343
|
100
|
|
|
|
1025
|
? $self->is_boolean_json($obj) |
47
|
|
|
|
|
|
|
: $self->is_boolean_perl($obj) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub is_boolean_perl { # 1 or 0 |
51
|
155
|
|
|
155
|
0
|
1705
|
my ($self, $obj) = @_; |
52
|
155
|
100
|
100
|
|
|
1218
|
(defined $obj && looks_like_number($obj) && ($obj == 1 || $obj == 0)) ? 1 : 0; # TODO invalidate 0.0 |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub is_boolean_json { |
56
|
220
|
|
|
220
|
0
|
397
|
my ($self, $obj) = @_; |
57
|
220
|
100
|
100
|
|
|
666
|
return 1 if defined $obj && Types::Serialiser::is_bool($obj); |
58
|
193
|
50
|
66
|
|
|
1148
|
return 1 if ref $obj eq 'SCALAR' && looks_like_number($$obj) && ($$obj == 1 || $$obj == 0); |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
59
|
189
|
|
|
|
|
603
|
return 0; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub is_null { |
63
|
22
|
|
|
22
|
0
|
58
|
my ($self, $obj) = @_; |
64
|
22
|
100
|
|
|
|
113
|
!defined($obj) ? 1 : 0; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# json schema core: 3.6 JSON value equality |
68
|
|
|
|
|
|
|
sub is_equal { |
69
|
213
|
|
|
213
|
0
|
2418
|
my ($self, $a, $b) = @_; |
70
|
213
|
100
|
100
|
|
|
523
|
return 1 if !defined($a) && !defined($b); |
71
|
212
|
100
|
100
|
|
|
505
|
return 1 if $self->is_boolean($a) && $self->is_boolean($b) && $a == $b; |
|
|
|
100
|
|
|
|
|
72
|
206
|
100
|
100
|
|
|
490
|
return 1 if $self->is_string($a) && $self->is_string($b) && "$a" eq "$b"; # ah |
|
|
|
100
|
|
|
|
|
73
|
182
|
50
|
100
|
|
|
431
|
return 1 if $self->is_number($a) && $self->is_number($b) && $a == $b; |
|
|
|
66
|
|
|
|
|
74
|
182
|
100
|
|
|
|
468
|
return 1 if eq_deeply($a, $b); # array & object |
75
|
166
|
|
|
|
|
81176
|
return 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |