rand62 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/lib/rand62.rb +16 -0
- data/lib/rand62/core_ext.rb +29 -0
- data/lib/rand62/version.rb +3 -0
- data/rand62.gemspec +17 -0
- data/test/test_rand62.rb +47 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kenn Ejima
|
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,49 @@
|
|
1
|
+
# Rand62
|
2
|
+
|
3
|
+
Generates random alphanumeric characters.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
Rand62.safe(10)
|
7
|
+
=> "yTX35RzROS"
|
8
|
+
```
|
9
|
+
|
10
|
+
It has better space efficiency than `SecureRandom.uuid` or `SecureRandom.hex`. It's sexier than `SecureRandom.base64` or `SecureRandom.urlsafe_base64` as Rand62 doesn't contain any symbols.
|
11
|
+
|
12
|
+
If you care more about database efficiency than Ruby, and user-friendliness of the look of IDs, use Rand62.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'rand62'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install rand62
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
There are two methods: `fast` and `safe`.
|
31
|
+
|
32
|
+
* **safe** - It has less chance of collision, as internally it uses `SecureRandom`. Use this method until the performance of this method becomes a problem.
|
33
|
+
* **fast** - About 10x faster than `safe` on Mac, but be careful as internally it uses rand().
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Rand62.fast(10)
|
37
|
+
=> "sWCGqxY2kF"
|
38
|
+
|
39
|
+
Rand62.safe(10)
|
40
|
+
=> "yTX35RzROS"
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rand62.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rand62/version'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
class Rand62
|
5
|
+
CHARS = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def safe(n)
|
9
|
+
n.times.map{ CHARS[SecureRandom.random_number(62)] }.join
|
10
|
+
end
|
11
|
+
|
12
|
+
def fast(n)
|
13
|
+
n.times.map{ CHARS.sample(1) }.join
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rand62'
|
2
|
+
|
3
|
+
class String
|
4
|
+
BASE62_MAP = Hash[Rand62::CHARS.map.with_index{|c,i| [c, i] }]
|
5
|
+
|
6
|
+
def base62
|
7
|
+
i = 0
|
8
|
+
i_out = 0
|
9
|
+
self.split(//).reverse.each do |c|
|
10
|
+
place = 62 ** i
|
11
|
+
i_out += BASE62_MAP[c] * place
|
12
|
+
i += 1
|
13
|
+
end
|
14
|
+
i_out
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Integer
|
19
|
+
def base62
|
20
|
+
return '0' if (i = self) == 0
|
21
|
+
s = ''
|
22
|
+
while i > 0
|
23
|
+
s << Rand62::CHARS[i.modulo(62)]
|
24
|
+
i /= 62
|
25
|
+
end
|
26
|
+
s.reverse!
|
27
|
+
s
|
28
|
+
end
|
29
|
+
end
|
data/rand62.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rand62/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kenn Ejima"]
|
6
|
+
gem.email = ["kenn.ejima@gmail.com"]
|
7
|
+
gem.description = %q{Generates random alphanumeric characters}
|
8
|
+
gem.summary = %q{Generates random alphanumeric characters}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rand62"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rand62::VERSION
|
17
|
+
end
|
data/test/test_rand62.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rand62/core_ext'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class TestRand62 < Test::Unit::TestCase
|
6
|
+
def measure
|
7
|
+
start = Time.now
|
8
|
+
yield
|
9
|
+
Time.now - start
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_rand62
|
13
|
+
assert_match Rand62.safe(1000), /^[0-9a-zA-Z]+$/
|
14
|
+
assert_match Rand62.fast(1000), /^[0-9a-zA-Z]+$/
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_distribution
|
18
|
+
set = Set.new
|
19
|
+
rand62 = Rand62.safe(1000)
|
20
|
+
|
21
|
+
rand62.split(//).each do |char|
|
22
|
+
set.add(char)
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_equal 1000, rand62.size
|
26
|
+
assert_equal 62, set.size
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_core_ext
|
30
|
+
assert_equal 1000000000.base62, '15ftgG'
|
31
|
+
assert_equal '15ftgG'.base62, 1000000000
|
32
|
+
|
33
|
+
assert_equal '123abcABC'.base62, 225587272106046
|
34
|
+
assert_equal 225587272106046.base62, '123abcABC'
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_benchmark
|
38
|
+
n = 1000
|
39
|
+
|
40
|
+
fast = measure { Rand62.fast(n) }
|
41
|
+
safe = measure { Rand62.safe(n) }
|
42
|
+
|
43
|
+
puts "Rand62.fast(#{n}): #{fast}"
|
44
|
+
puts "Rand62.safe(#{n}): #{safe}"
|
45
|
+
assert fast < safe
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rand62
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kenn Ejima
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Generates random alphanumeric characters
|
15
|
+
email:
|
16
|
+
- kenn.ejima@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/rand62.rb
|
27
|
+
- lib/rand62/core_ext.rb
|
28
|
+
- lib/rand62/version.rb
|
29
|
+
- rand62.gemspec
|
30
|
+
- test/test_rand62.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.19
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Generates random alphanumeric characters
|
55
|
+
test_files:
|
56
|
+
- test/test_rand62.rb
|
57
|
+
has_rdoc:
|