File Coverage

blib/lib/Data/UUID/Concise.pm
Criterion Covered Total %
statement 57 57 100.0
branch n/a
condition n/a
subroutine 14 14 100.0
pod 2 2 100.0
total 73 73 100.0


line stmt bran cond sub pod time code
1 3     3   32645 use strictures 1;
  3         25  
  3         93  
2              
3             package Data::UUID::Concise;
4              
5 3     3   316 use 5.010;
  3         15  
  3         120  
6 3     3   3188 use utf8;
  3         29  
  3         16  
7 3     3   3020 use open qw(:std :utf8);
  3         4146  
  3         19  
8 3     3   53302 use charnames qw(:full :short);
  3         122480  
  3         20  
9              
10 3     3   3534 use Moo;
  3         55868  
  3         22  
11 3     3   8498 use MooX::Types::MooseLike::Base qw(:all);
  3         18779  
  3         1276  
12              
13 3     3   29 use Carp;
  3         7  
  3         174  
14 3     3   1667 use Data::UUID;
  3         1961  
  3         210  
15 3     3   2704 use List::MoreUtils qw[ uniq ];
  3         3090  
  3         198  
16 3     3   4417 use Math::BigInt;
  3         69335  
  3         20  
17              
18             our $VERSION = '0.121240'; # VERSION
19              
20             # ABSTRACT: Encode UUIDs to be more concise or communicable
21             # ENCODING: utf-8
22              
23             has 'alphabet' => (
24             is => 'rw',
25             isa => Str,
26             default => sub {
27             '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
28             },
29             );
30              
31             around 'alphabet' => sub {
32             my ( $next, $self, @rest ) = @_;
33             return $self->$next unless @rest;
34              
35             my ( $alphabet_candidate ) = @rest;
36             return $self->$next( _normalize_alphabet( $alphabet_candidate ) );
37             };
38              
39             sub _normalize_alphabet
40             {
41 1     1   2 my ( $alphabet_candidate ) = @_;
42              
43 1         9 my @symbols = split //, $alphabet_candidate;
44 1         7 my @decruftified_symbols = uniq sort { $a cmp $b } @symbols;
  55         71  
45 1         5 my $decruftified_alphabet = join '', @decruftified_symbols;
46              
47 1         24 return $decruftified_alphabet;
48             }
49              
50             sub encode
51             {
52 301     301 1 5708 my ( $self, $uuid ) = @_;
53              
54 301         515 my $output = '';
55 301         45317 my $numeric =
56             Math::BigInt->new( ( Data::UUID->new )->to_hexstring( $uuid ) );
57 301         154636 my $alphabet_length = length( $self->alphabet );
58              
59 301         2652 while ( $numeric->is_positive ) {
60 6622         859103 my $index = $numeric->copy->bmod( $alphabet_length );
61 6622         1014625 $output .= substr( $self->alphabet, $index, 1 );
62 6622         142752 $numeric->bdiv( $alphabet_length );
63             }
64              
65 301         53678 return $output;
66             }
67              
68             sub decode
69             {
70 1     1 1 3 my ( $self, $string ) = @_;
71              
72 1         4 my $numeric = Math::BigInt->new;
73 1         39 my @characters = split //, $string;
74 1         23 my $alphabet_length = length( $self->alphabet );
75              
76 1         7 for my $character ( @characters ) {
77 22         2276 my $value = index $self->alphabet, $character;
78 22         157 $numeric = $numeric->bmul( $alphabet_length );
79 22         2071 $numeric = $numeric->badd( $value );
80             }
81              
82 1         280 return ( Data::UUID->new )->from_hexstring( $numeric->as_hex );
83             }
84              
85             1;
86              
87             __END__