shortuuid 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/README.md +25 -13
- data/lib/shortuuid.rb +15 -4
- data/lib/shortuuid/version.rb +1 -1
- data/shortuuid.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9c37e30262bfa1797992cf06f6396ed074a22ac2d3c129935a616ca8206d1fb
|
4
|
+
data.tar.gz: a7c799ee1b68fac269a43c2647fb1a6e9b4d2a2e5ae3d1d5446928dd57c1eb3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64bb7deea0727eca9d12da918c63933637b43b961573f04af0d3553b1fac7c5ee211e04f574d7107d618ba379b1aa8da5759fd2244828117a5b0e4b222c74dc5
|
7
|
+
data.tar.gz: ab8dec350cd401767a11ef16d5773986c3d29b9ed0164902b5dcad64c2f6074d3e99cb86014118426063b73d1ddc34dce60d143e7e2de38e05d5de4315bad058
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,29 @@
|
|
1
|
-
# ShortUUID
|
1
|
+
# ShortUUID
|
2
2
|
|
3
|
-
|
3
|
+
Encode and decode UUIDs into any alphabet. Can either be used to improve space efficiency by using any alphabet with more than 16 characters, or any other alphabet for fun or profit.
|
4
4
|
|
5
|
-
|
5
|
+
The default alphabet is a URL safe base62, as defined by
|
6
|
+
|
7
|
+
`DEFAULT_BASE62 = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z).freeze`
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```
|
12
|
+
irb(main):001:0> id = SecureRandom.uuid
|
13
|
+
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"
|
14
|
+
|
15
|
+
irb(main):003:0> short_id = ShortUUID.shorten id
|
16
|
+
=> "2CvPdpytrcURpSLoPxYb30"
|
17
|
+
|
18
|
+
irb(main):005:0> ShortUUID.expand short_id
|
19
|
+
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"
|
20
|
+
|
21
|
+
irb(main):007:0> vowel_id = ShortUUID.shorten id, "AEIOU".chars
|
22
|
+
=> "EOOIAUUEOUUIOIIEUUUAOOUIOOUUAOIIIEAUAOUAAAOEOOEOAUAUUEE"
|
23
|
+
|
24
|
+
irb(main):009:0> ShortUUID.expand vowel_id, "AEIOU".chars
|
25
|
+
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"
|
26
|
+
```
|
6
27
|
|
7
28
|
## Installation
|
8
29
|
|
@@ -20,19 +41,10 @@ Or install it yourself as:
|
|
20
41
|
|
21
42
|
$ gem install shortuuid
|
22
43
|
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
44
|
|
33
45
|
## Contributing
|
34
46
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sudhirj/shortuuid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
48
|
|
37
49
|
|
38
50
|
## License
|
data/lib/shortuuid.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'shortuuid/version'
|
2
4
|
|
3
5
|
module ShortUUID
|
4
|
-
DEFAULT_BASE62 = %w
|
6
|
+
DEFAULT_BASE62 = %w[0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
|
5
7
|
|
6
8
|
def self.shorten(uuid, alphabet = DEFAULT_BASE62)
|
7
9
|
return nil unless uuid && !uuid.empty?
|
@@ -9,20 +11,21 @@ module ShortUUID
|
|
9
11
|
convert_decimal_to_alphabet(decimal_value, alphabet)
|
10
12
|
end
|
11
13
|
|
12
|
-
def self.convert_decimal_to_alphabet(decimal, alphabet)
|
14
|
+
def self.convert_decimal_to_alphabet(decimal, alphabet = DEFAULT_BASE62)
|
13
15
|
alphabet = alphabet.to_a
|
14
16
|
radix = alphabet.length
|
15
17
|
i = decimal.to_i
|
16
18
|
out = []
|
19
|
+
return alphabet[0] if i.zero?
|
17
20
|
loop do
|
18
|
-
break if i
|
21
|
+
break if i.zero?
|
19
22
|
out.unshift(alphabet[i % radix])
|
20
23
|
i /= radix
|
21
24
|
end
|
22
25
|
out.join
|
23
26
|
end
|
24
27
|
|
25
|
-
def self.convert_alphabet_to_decimal(word, alphabet)
|
28
|
+
def self.convert_alphabet_to_decimal(word, alphabet = DEFAULT_BASE62)
|
26
29
|
num = 0
|
27
30
|
radix = alphabet.length
|
28
31
|
word.chars.to_a.reverse.each_with_index do |char, index|
|
@@ -31,6 +34,14 @@ module ShortUUID
|
|
31
34
|
num
|
32
35
|
end
|
33
36
|
|
37
|
+
def self.encode(number, alphabet = DEFAULT_BASE62)
|
38
|
+
convert_decimal_to_alphabet(number, alphabet)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.decode(word, alphabet = DEFAULT_BASE62)
|
42
|
+
convert_alphabet_to_decimal(word, alphabet)
|
43
|
+
end
|
44
|
+
|
34
45
|
def self.expand(short_uuid, alphabet = DEFAULT_BASE62)
|
35
46
|
return nil unless short_uuid && !short_uuid.empty?
|
36
47
|
decimal_value = convert_alphabet_to_decimal(short_uuid, alphabet)
|
data/lib/shortuuid/version.rb
CHANGED
data/shortuuid.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Sudhir Jonathan']
|
10
10
|
spec.email = ['sudhir.j@gmail.com']
|
11
11
|
|
12
|
-
spec.summary = 'Quickly and easily convert UUIDs to space efficient
|
12
|
+
spec.summary = 'Quickly and easily convert UUIDs to space efficient Base62 strings.'
|
13
13
|
spec.homepage = 'https://github.com/sudhirj/shortuuid'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shortuuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sudhir Jonathan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
93
|
+
rubygems_version: 2.7.6
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
|
-
summary: Quickly and easily convert UUIDs to space efficient
|
96
|
+
summary: Quickly and easily convert UUIDs to space efficient Base62 strings.
|
97
97
|
test_files: []
|