tokenizr 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b1183f4c1a2da91ae879a79932c918da7631b7b
4
+ data.tar.gz: 0dde0de7be6b41bd958e974473e25afc7bc7f211
5
+ SHA512:
6
+ metadata.gz: 1a57087714406a3a21daa565903be286e471eba1965f0f4d3f79a9c6251f3dd2e9ba54f18c4be3ce64a55f47e8c779d893903df821da295c1d6366486c737e6d
7
+ data.tar.gz: c96491b0bc2b10e23ba8045a1de47505544ddc25ffcd18da04cfb53f967bb250ca7c0f4049e5e618146455a0ddea3a1c397a616c03a96c1dbf6464be7f5e3049
data/LICENSE.TXT ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 arktisklada
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Tokenizr
2
+
3
+ Creates alpha tokens from numbers, and vice versa
4
+
5
+ ```
6
+ Tokenizr.encode(123) # => "5IbEL8X9e"
7
+
8
+ Tokenizr.decode("5IbEL8X9e") # => 123
9
+ ```
10
+
11
+ ## Contributing
12
+
13
+ 1. Create an issue
14
+ 2. Fork it ( https://github.com/arktisklada/tokenizr/fork )
15
+ 3. Create your feature branch (`git checkout -b my-new-feature`)
16
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
17
+ 5. Push to the branch (`git push origin my-new-feature`)
18
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Tokenizr'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ # t.libs << 'test'
27
+ t.pattern = 'test/*_test.rb'
28
+ t.verbose = true
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,3 @@
1
+ module Tokenizr
2
+ VERSION = "1.0.0"
3
+ end
data/lib/tokenizr.rb ADDED
@@ -0,0 +1,44 @@
1
+ module Tokenizr
2
+ INDEX = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
3
+ LENGTH = 15
4
+
5
+ def self.encode(number)
6
+ number = lengthen(number).to_i
7
+ ret = ''
8
+
9
+ start = (Math.log(number) / Math.log(INDEX.length).to_f).floor
10
+ start.downto(0).each do |i|
11
+ ret += INDEX[((number / Tokenizr.bcpow(INDEX.length, i).to_f).floor % INDEX.length)]
12
+ end
13
+
14
+ ret.reverse
15
+ end
16
+
17
+ def self.decode(string)
18
+ str = string.reverse
19
+ ret = 0
20
+
21
+ (0...str.length).each do |i|
22
+ ret += INDEX.index(str[i]) * (Tokenizr.bcpow(INDEX.length, (str.length - 1) - i))
23
+ end
24
+
25
+ ret.to_s[1..-1].to_i
26
+ end
27
+
28
+
29
+ private
30
+
31
+ def self.bcpow(a, b)
32
+ (a.to_f ** b.to_i).floor
33
+ end
34
+
35
+ def self.lengthen(number)
36
+ str = number.to_s
37
+ while str.length < LENGTH
38
+ str = "0" + str
39
+ end
40
+
41
+ ("1" + str).to_i
42
+ end
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ require 'tokenizr'
2
+ require 'minitest/autorun'
3
+
4
+ class TestTokenizr < MiniTest::Unit::TestCase
5
+ describe '#encode' do
6
+ it 'encodes the given number into a string' do
7
+ assert_equal Tokenizr.encode(123), "5IbEL8X9e"
8
+ assert_equal Tokenizr.encode(987654321), "VGh4Q9X9e"
9
+ end
10
+
11
+ it 'only uses alphanumeric characters' do
12
+ range = (1...1000000)
13
+ 1000.times do
14
+ assert_match /^[a-zA-Z0-9]+/, Tokenizr.encode(rand(range))
15
+ end
16
+ end
17
+
18
+ it 'ensures encoded strings are at least 9 characters long' do
19
+ assert_operator Tokenizr.encode(1).length, :>=, 9
20
+ range = (2...1000000)
21
+ 1000.times do
22
+ assert_operator Tokenizr.encode(rand(range)).length, :>=, 9
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#decode' do
28
+ it 'decodes the given string into a number' do
29
+ assert_equal Tokenizr.decode("5IbEL8X9e"), 123
30
+ assert_equal Tokenizr.decode("VGh4Q9X9e"), 987654321
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tokenizr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - arktisklada
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - mail@enorganik.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.TXT
49
+ - README.md
50
+ - Rakefile
51
+ - lib/tokenizr.rb
52
+ - lib/tokenizr/version.rb
53
+ - test/tokenizr_test.rb
54
+ homepage: https://github.com/arktisklada/tokenizr
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.5.0
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: ''
78
+ test_files:
79
+ - test/tokenizr_test.rb