hashids 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +10 -0
  2. data/lib/hashids.rb +18 -14
  3. data/spec/hashids_spec.rb +4 -0
  4. metadata +2 -2
data/README.md CHANGED
@@ -184,6 +184,16 @@ hashids.encrypt 5 #=> a5
184
184
 
185
185
  ## Changelog
186
186
 
187
+ **0.0.3**
188
+
189
+ - Default salt (Allows for `Hashids.new.encrypt(91) #=> "kBy"`)
190
+ - Further tweaking of the private methods (`tr/delete` over `gsub`, `scan` over `split`)
191
+
192
+ **0.0.2**
193
+
194
+ - Minitest required if RUBY_VERSION < 1.9.3
195
+ - Using scan over split where appropriate
196
+
187
197
  **0.0.1**
188
198
 
189
199
  - First commit (Heavily based on the [CoffeeScript version](https://github.com/ivanakimov/hashids.coffee))
data/lib/hashids.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class Hashids
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  DEFAULT_ALPHABET = "xcS4F6h89aUbideAI7tkynuopqrXCgTE5GBKHLMjfRsz"
6
6
  PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
7
7
 
@@ -43,6 +43,10 @@ class Hashids
43
43
  raise Hashids::MinLengthError, "The min length must be a Fixnum"
44
44
  end
45
45
 
46
+ unless @min_length >= 0
47
+ raise Hashids::MinLengthError, "The min length must be 0 or more"
48
+ end
49
+
46
50
  unless @alphabet.kind_of?(String)
47
51
  raise Hashids::AlphabetError, "The alphabet must be a String"
48
52
  end
@@ -64,7 +68,7 @@ class Hashids
64
68
  break if char.nil?
65
69
 
66
70
  @seps << char
67
- @alphabet.gsub!(char, ' ')
71
+ @alphabet.tr!(char, ' ')
68
72
  end
69
73
 
70
74
  [0, 4, 8, 12].each do |index|
@@ -76,7 +80,7 @@ class Hashids
76
80
  end
77
81
  end
78
82
 
79
- @alphabet.gsub!(' ', '')
83
+ @alphabet.delete!(' ')
80
84
  @alphabet = consistent_shuffle(@alphabet, @salt)
81
85
  end
82
86
 
@@ -95,7 +99,7 @@ class Hashids
95
99
 
96
100
  ret += lottery_char = lottery[0]
97
101
 
98
- alphabet = lottery_char + alphabet.gsub(lottery_char, '')
102
+ alphabet = "#{lottery_char}#{alphabet.delete(lottery_char)}"
99
103
  end
100
104
 
101
105
  alphabet = consistent_shuffle(alphabet, "#{lottery_char.ord & 12345}#{salt}")
@@ -120,7 +124,7 @@ class Hashids
120
124
 
121
125
  if ret.length < min_length
122
126
  guard_index = (guard_index + ret.length) % @guards.length
123
- guard = @guards[guard_index]
127
+ guard = @guards[guard_index]
124
128
 
125
129
  ret += guard
126
130
  end
@@ -128,10 +132,10 @@ class Hashids
128
132
 
129
133
  while ret.length < min_length
130
134
  pad_array = [alphabet[1].ord, alphabet[0].ord]
131
- pad_left = encode(pad_array, alphabet, salt)
135
+ pad_left = encode(pad_array, alphabet, salt)
132
136
  pad_right = encode(pad_array, alphabet, pad_array.join(''))
133
137
 
134
- ret = pad_left + ret + pad_right
138
+ ret = "#{pad_left}#{ret}#{pad_right}"
135
139
  excess = ret.length - min_length
136
140
 
137
141
  ret = ret[(excess/2), min_length] if excess > 0
@@ -150,7 +154,7 @@ class Hashids
150
154
  lottery_char = ""
151
155
 
152
156
  @guards.each do |guard|
153
- hash = hash.gsub(guard, ' ')
157
+ hash = hash.tr(guard, ' ')
154
158
  end
155
159
 
156
160
  hash_split = hash.split(' ')
@@ -158,7 +162,7 @@ class Hashids
158
162
  hash = hash_split[[3,2].include?(hash_split.length) ? 1 : 0]
159
163
 
160
164
  @seps.each do |sep|
161
- hash = hash.gsub(sep, ' ')
165
+ hash = hash.tr(sep, ' ')
162
166
  end
163
167
 
164
168
  hash_array = hash.split(' ')
@@ -167,8 +171,8 @@ class Hashids
167
171
  if sub_hash.length > 0
168
172
  if i == 0
169
173
  lottery_char = hash[0]
170
- sub_hash = sub_hash[1..-1]
171
- alphabet = lottery_char + @alphabet.gsub(lottery_char, "")
174
+ sub_hash = sub_hash[1..-1]
175
+ alphabet = lottery_char + @alphabet.delete(lottery_char)
172
176
  end
173
177
 
174
178
  if alphabet.length > 0 && lottery_char.length > 0
@@ -195,7 +199,7 @@ class Hashids
195
199
  sorting_array = []
196
200
 
197
201
  salt_array << "" if salt_array.empty?
198
- salt_array.each { |char| sorting_array << char.ord }
202
+ salt_array.each { |char| sorting_array << (char.empty?? 0 : char.ord) }
199
203
 
200
204
  sorting_array.each_with_index do |int,i|
201
205
  add = true
@@ -235,7 +239,7 @@ class Hashids
235
239
  hash = ""
236
240
 
237
241
  while number > 0
238
- hash = alphabet[number % alphabet.length] + hash
242
+ hash = "#{alphabet[number % alphabet.length]}#{hash}"
239
243
  number = number / alphabet.length
240
244
  end
241
245
 
@@ -245,7 +249,7 @@ class Hashids
245
249
  def unhash(hash, alphabet)
246
250
  number = 0
247
251
 
248
- hash.split('').each_with_index do |char, i|
252
+ hash.scan(@chars_regex).each_with_index do |char, i|
249
253
  if pos = alphabet.index(char)
250
254
  number += pos * alphabet.length ** (hash.length - i - 1)
251
255
  end
data/spec/hashids_spec.rb CHANGED
@@ -22,6 +22,10 @@ describe Hashids do
22
22
  }
23
23
 
24
24
  describe "setup" do
25
+ it "has a default salt" do
26
+ Hashids.new.encrypt(1,2,3).must_equal "katKSA"
27
+ end
28
+
25
29
  it "has a default alphabet" do
26
30
  Hashids::DEFAULT_ALPHABET.must_equal default_alphabet
27
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashids
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  segments:
50
50
  - 0
51
- hash: 2124593182474944467
51
+ hash: -2635611476242162703
52
52
  requirements: []
53
53
  rubyforge_project:
54
54
  rubygems_version: 1.8.24