Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled customized "separators" and updated documentation accordingly #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions lib/Hashids.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ has alphabet => (

has chars => ( is => 'rwp', init_arg => undef, default => sub { [] } );

has seps => (
has separators => (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can retain seps here and just remove its init_arg option so users can set it at object construction, e.g. Hashids->new( seps => 'aeiouy' ):

has seps => (
    is  => 'rwp',
    isa => sub {
        local $_ = shift;
        croak "$_ must not have spaces" if /\s/;
    },
    default => sub {
        my @seps = qw(c f h i s t u);
        join '' => @seps, map {uc} @seps;
    },
);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with using "seps" rather than the longer "separators" keyword. I also like your suggestion for this preference because, now that I think of it, it can prevent problems resulting from a somewhat common misspelling that comes out as "seperators."

is => 'rwp',
init_arg => undef,
default => sub {
my @seps = qw(c f h i s t u);
[ @seps, map {uc} @seps ];
isa => sub {
local $_ = shift;
croak "$_ must not have spaces" if /\s/;
},
default => sub { 'cfhistuCFHISTU' }
);

has guards => ( is => 'rwp', init_arg => undef, default => sub { [] } );
Expand All @@ -58,14 +58,15 @@ sub BUILD {
if length $self->salt > length $self->alphabet;

my @alphabet = split // => $self->alphabet;
my ( @seps, @guards );
my @seps = split // => $self->separators;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the above, you won't need to split here...

my @guards;

my $sepDiv = 3.5;
my $guardDiv = 12;

# seps should contain only chars present in alphabet;
# alphabet should not contain seps
for my $sep ( @{ $self->seps } ) {
# separators should contain only chars present in alphabet;
# alphabet should not contain separators
for my $sep ( @seps ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and instead do the split here.

push @seps, $sep if any {/$sep/} @alphabet;
@alphabet = grep { !/$sep/ } @alphabet;
}
Expand All @@ -89,7 +90,7 @@ sub BUILD {
: splice @alphabet, 0, $guardCount;

$self->_set_chars( \@alphabet );
$self->_set_seps( \@seps );
$self->_set_separators( \@seps );
$self->_set_guards( \@guards );
}

Expand Down Expand Up @@ -152,8 +153,8 @@ sub encode {

if ( $i + 1 < @$num ) {
$n %= ord($last) + $i;
my $sepsIndex = $n % @{ $self->seps };
push @res, $self->seps->[$sepsIndex];
my $sepsIndex = $n % @{ $self->separators };
push @res, $self->separators->[$sepsIndex];
}
}

Expand Down Expand Up @@ -205,7 +206,7 @@ sub decode {
my $lottery = substr $hash, 0, 1;
$hash = substr $hash, 1;

my $sep = join '|', @{ $self->seps };
my $sep = join '|', @{ $self->separators };
@hash = grep { $_ ne '' } split /$sep/ => $hash;

my @alphabet = @{ $self->chars };
Expand Down Expand Up @@ -278,7 +279,8 @@ Make a new Hashids object. This constructor accepts a few options:
my $hashids = Hashids->new(
salt => 'this is my salt',
alphabet => 'abcdefghijklmnop',
minHashLength => 8
minHashLength => 8,
seperators => 'aeiouy'
);

=over
Expand All @@ -301,6 +303,12 @@ spaces, and only has unique characters.
Minimum hash length. Use this to control how long the generated hash
string should be.

=item separators

Separator set to use. This is optional as Hashids comes with a default
set of separators characters. Should you choose to supply custom
separators (which can be an empty ''), make sure there are no spaces.

=back

You can also construct with just a single argument for the salt, leaving
Expand Down Expand Up @@ -387,6 +395,10 @@ Zak B. Elep E<lt>[email protected]<gt>
Original Hashids JavaScript library written by L<Ivan
Akimov|http://twitter.com/ivanakimov>

=head1 CONTRIBUTORS

Randolf Richardson E<lt>[email protected]<gt>

=head1 THANKS

Props to L<Jofell Gallardo|http://twitter.com/jofell> for pointing this
Expand Down