-
Notifications
You must be signed in to change notification settings - Fork 6
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
randolf
wants to merge
1
commit into
zakame:master
Choose a base branch
from
randolf:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,13 +33,13 @@ has alphabet => ( | |
|
||
has chars => ( is => 'rwp', init_arg => undef, default => sub { [] } ); | ||
|
||
has seps => ( | ||
has separators => ( | ||
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 { [] } ); | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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 ); | ||
} | ||
|
||
|
@@ -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]; | ||
} | ||
} | ||
|
||
|
@@ -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 }; | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 itsinit_arg
option so users can set it at object construction, e.g.Hashids->new( seps => 'aeiouy' )
:There was a problem hiding this comment.
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."