simple-hasher 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple-hasher/version.rb +1 -1
- data/lib/simplehasher.rb +29 -0
- data/spec/simple-hasher_spec.rb +6 -6
- metadata +2 -2
- data/lib/simple-hasher.rb +0 -31
data/lib/simplehasher.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "./lib/simple-hasher/version"
|
2
|
+
|
3
|
+
module SimpleHasher
|
4
|
+
ALLOWED_CHARS = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
|
5
|
+
|
6
|
+
def self.encode(id)
|
7
|
+
length = ALLOWED_CHARS.length
|
8
|
+
|
9
|
+
while id > length -1
|
10
|
+
hash = ALLOWED_CHARS[id % length,1].concat( hash || "" )
|
11
|
+
id = (id / length).floor
|
12
|
+
end
|
13
|
+
|
14
|
+
ALLOWED_CHARS[id,1].concat(hash || "")
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.decode(hash)
|
18
|
+
length = ALLOWED_CHARS.length
|
19
|
+
size = hash.length - 1
|
20
|
+
array = hash.split('')
|
21
|
+
id = ALLOWED_CHARS.index(array.pop)
|
22
|
+
i = 0
|
23
|
+
array.each do |c|
|
24
|
+
id += ALLOWED_CHARS.index(c) * (length ** (size - i))
|
25
|
+
i += 1
|
26
|
+
end
|
27
|
+
id
|
28
|
+
end
|
29
|
+
end
|
data/spec/simple-hasher_spec.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
require './spec/spec_helper.rb'
|
2
|
-
require './lib/
|
2
|
+
require './lib/simplehasher.rb'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe SimpleHasher do
|
5
5
|
describe "encode" do
|
6
6
|
it 'should encode a single digit integer' do
|
7
7
|
num = 1
|
8
|
-
|
8
|
+
SimpleHasher.encode(num).should== SimpleHasher::ALLOWED_CHARS[num]
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'should encode an integer into a multi-character string' do
|
12
|
-
|
12
|
+
SimpleHasher.encode(5779).should== "LQv"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe 'decode' do
|
17
17
|
it 'should decode a string to a single digit integer' do
|
18
18
|
num = 1
|
19
|
-
|
19
|
+
SimpleHasher.decode('L').should== num
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'should encode an integer into a multi-character string' do
|
23
|
-
|
23
|
+
SimpleHasher.decode("LQv").should== 5779
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-hasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -40,8 +40,8 @@ files:
|
|
40
40
|
- LICENSE
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
|
-
- lib/simple-hasher.rb
|
44
43
|
- lib/simple-hasher/version.rb
|
44
|
+
- lib/simplehasher.rb
|
45
45
|
- simple-hasher.gemspec
|
46
46
|
- spec/simple-hasher_spec.rb
|
47
47
|
- spec/spec_helper.rb
|
data/lib/simple-hasher.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require "./lib/simple-hasher/version"
|
2
|
-
|
3
|
-
module Simple
|
4
|
-
module Hasher
|
5
|
-
ALLOWED_CHARS = "CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
|
6
|
-
|
7
|
-
def self.encode(id)
|
8
|
-
length = ALLOWED_CHARS.length
|
9
|
-
|
10
|
-
while id > length -1
|
11
|
-
hash = ALLOWED_CHARS[id % length,1].concat( hash || "" )
|
12
|
-
id = (id / length).floor
|
13
|
-
end
|
14
|
-
|
15
|
-
ALLOWED_CHARS[id,1].concat(hash || "")
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.decode(hash)
|
19
|
-
length = ALLOWED_CHARS.length
|
20
|
-
size = hash.length - 1
|
21
|
-
array = hash.split('')
|
22
|
-
id = ALLOWED_CHARS.index(array.pop)
|
23
|
-
i = 0
|
24
|
-
array.each do |c|
|
25
|
-
id += ALLOWED_CHARS.index(c) * (length ** (size - i))
|
26
|
-
i += 1
|
27
|
-
end
|
28
|
-
id
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|