| 1 | package Skilletor::JSON::OrderedHash; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A tied hash that keeps a JavaScript object's property order | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 248 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 248 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 248 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 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 | 35008 | ▶ | sub is_index_key { | ||||||||||||||||||||||||||
|
Subroutine
is_index_key: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 35008 | my ( $class, $key ) = @_; | |||||||||||||||||||||||||||
| 13 | 35008 | ▶ | return 0 unless defined $key && $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and $key =~ /\A(?:0|[1-9][0-9]{0,9})\z/
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/
| |||||||||||||||||||||||||||||
| 14 | 57 | ▶ | return $key <= 4294967294 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 13710 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 18 | 13710 | my ( $class, @pairs ) = @_; | |||||||||||||||||||||||||||
| 19 | 13710 | my $self = bless { data => {}, order => [], index => {} }, $class; | |||||||||||||||||||||||||||
| 20 | 13710 | ▶ | while (@pairs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 2626 | my ( $k, $v ) = splice @pairs, 0, 2; | |||||||||||||||||||||||||||
| 22 | 2626 | $self->STORE( $k, $v ); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 13710 | return $self; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 26748 | ▶ | sub FETCH { $_[0]{data}{ $_[1] } } | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 22830 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 30 | 22830 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||
| 31 | 22830 | $key = '' . $key; | |||||||||||||||||||||||||||
| 32 | 22830 | ▶ | unless ( exists $self->{data}{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 22352 | ▶ | if ( __PACKAGE__->is_index_key($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 40 | $self->{index}{$key} = 1; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | 22312 | push @{ $self->{order} }, $key; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 22830 | $self->{data}{$key} = $value; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 16408 | ▶ | sub EXISTS { exists $_[0]{data}{ $_[1] } } | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 15 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 46 | 15 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 47 | 15 | ▶ | return unless exists $self->{data}{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 15 | ▶ | if ( delete $self->{index}{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | # nothing else to do: index keys are ordered by value | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | 14 | $self->{order} = [ grep { $_ ne $key } @{ $self->{order} } ]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 15 | 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 | 10336 | ▶ | sub keys_in_order { | ||||||||||||||||||||||||||
|
Subroutine
keys_in_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 10336 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 66 | 10336 | return ( ( sort { $a <=> $b } keys %{ $self->{index} } ), @{ $self->{order} } ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9440 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 9440 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 71 | 9440 | $self->{iter} = [ $self->keys_in_order ]; | |||||||||||||||||||||||||||
| 72 | 9440 | return shift @{ $self->{iter} }; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 13096 | ▶ | sub NEXTKEY { shift @{ $_[0]{iter} } } | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 139 | ▶ | sub SCALAR { scalar %{ $_[0]{data} } } | ||||||||||||||||||||||||||
|
Subroutine
SCALAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 248 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||