line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Valiemon::Primitives; |
2
|
25
|
|
|
25
|
|
80
|
use strict; |
|
25
|
|
|
|
|
26
|
|
|
25
|
|
|
|
|
530
|
|
3
|
25
|
|
|
25
|
|
699
|
use warnings; |
|
25
|
|
|
|
|
27
|
|
|
25
|
|
|
|
|
416
|
|
4
|
25
|
|
|
25
|
|
71
|
use utf8; |
|
25
|
|
|
|
|
28
|
|
|
25
|
|
|
|
|
75
|
|
5
|
|
|
|
|
|
|
|
6
|
25
|
|
|
25
|
|
480
|
use Scalar::Util qw(looks_like_number); |
|
25
|
|
|
|
|
27
|
|
|
25
|
|
|
|
|
1834
|
|
7
|
25
|
|
|
25
|
|
8769
|
use Types::Serialiser; |
|
25
|
|
|
|
|
56446
|
|
|
25
|
|
|
|
|
593
|
|
8
|
25
|
|
|
25
|
|
10660
|
use Test::Deep qw(eq_deeply); |
|
25
|
|
|
|
|
141663
|
|
|
25
|
|
|
|
|
106
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
215
|
|
|
215
|
0
|
8740
|
my ($class, $options) = @_; |
12
|
215
|
|
100
|
|
|
1265
|
return bless { |
13
|
|
|
|
|
|
|
options => $options || +{}, |
14
|
|
|
|
|
|
|
}, $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub is_object { |
18
|
94
|
|
|
94
|
0
|
98
|
my ($self, $obj) = @_; |
19
|
94
|
100
|
100
|
|
|
559
|
(defined $obj && ref $obj eq 'HASH') ? 1 : 0; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub is_array { |
23
|
53
|
|
|
53
|
0
|
62
|
my ($self, $obj) = @_; |
24
|
53
|
100
|
100
|
|
|
345
|
(defined $obj && ref $obj eq 'ARRAY') ? 1 : 0; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub is_string { |
28
|
522
|
|
|
522
|
0
|
670
|
my ($self, $obj) = @_; |
29
|
522
|
100
|
100
|
|
|
2513
|
(defined $obj && ref $obj eq '') ? 1 : 0; # really? |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub is_number { |
33
|
537
|
|
|
537
|
0
|
597
|
my ($self, $obj) = @_; |
34
|
|
|
|
|
|
|
# avoid from JSON::Boolean treated as number. |
35
|
537
|
100
|
100
|
|
|
5455
|
(defined $obj && ref $obj eq '' && looks_like_number($obj)) ? 1 : 0; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub is_integer { |
39
|
262
|
|
|
262
|
0
|
444
|
my ($self, $obj) = @_; |
40
|
262
|
100
|
100
|
|
|
348
|
$self->is_number($obj) && $obj =~ qr/^-?\d+$/ ? 1 : 0; # TODO find more better way |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub is_boolean { |
44
|
269
|
|
|
269
|
0
|
290
|
my ($self, $obj) = @_; |
45
|
|
|
|
|
|
|
return $self->{options}->{use_json_boolean} |
46
|
269
|
100
|
|
|
|
521
|
? $self->is_boolean_json($obj) |
47
|
|
|
|
|
|
|
: $self->is_boolean_perl($obj) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub is_boolean_perl { # 1 or 0 |
51
|
140
|
|
|
140
|
0
|
322
|
my ($self, $obj) = @_; |
52
|
140
|
100
|
66
|
|
|
906
|
(defined $obj && looks_like_number($obj) && ($obj == 1 || $obj == 0)) ? 1 : 0; # TODO invalidate 0.0 |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub is_boolean_json { |
56
|
161
|
|
|
161
|
0
|
140
|
my ($self, $obj) = @_; |
57
|
161
|
100
|
100
|
|
|
400
|
return 1 if defined $obj && Types::Serialiser::is_bool($obj); |
58
|
139
|
50
|
66
|
|
|
752
|
return 1 if ref $obj eq 'SCALAR' && looks_like_number($$obj) && ($$obj == 1 || $$obj == 0); |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
59
|
135
|
|
|
|
|
283
|
return 0; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub is_null { |
63
|
23
|
|
|
23
|
0
|
28
|
my ($self, $obj) = @_; |
64
|
23
|
100
|
|
|
|
85
|
!defined($obj) ? 1 : 0; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# json schema core: 3.6 JSON value equality |
68
|
|
|
|
|
|
|
sub is_equal { |
69
|
212
|
|
|
212
|
0
|
733
|
my ($self, $a, $b) = @_; |
70
|
212
|
100
|
66
|
|
|
369
|
return 1 if !defined($a) && !defined($b); |
71
|
211
|
100
|
100
|
|
|
269
|
return 1 if $self->is_boolean($a) && $self->is_boolean($b) && $a == $b; |
|
|
|
100
|
|
|
|
|
72
|
205
|
100
|
100
|
|
|
268
|
return 1 if $self->is_string($a) && $self->is_string($b) && "$a" eq "$b"; # ah |
|
|
|
100
|
|
|
|
|
73
|
182
|
50
|
100
|
|
|
223
|
return 1 if $self->is_number($a) && $self->is_number($b) && $a == $b; |
|
|
|
66
|
|
|
|
|
74
|
182
|
100
|
|
|
|
315
|
return 1 if eq_deeply($a, $b); # array & object |
75
|
166
|
|
|
|
|
49906
|
return 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |