ulid 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +1 -2
- data/README.md +5 -18
- data/lib/ulid/generator.rb +7 -5
- data/lib/ulid/version.rb +1 -1
- data/spec/lib/ulid_spec.rb +9 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a2e09c0725c50d9cdce66e66ca46f2c7fbdcf5b
|
4
|
+
data.tar.gz: 203d9b4b4c466561af9733512d1b3c118e77b851
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e94a295f1b71498c1c21381f99e9b3ec888ba2a51acee99618985eecc35f6e94eff14baae6a459c54088bea252833ba0a11ff66f9b663fb9ac4a614b1260a52f
|
7
|
+
data.tar.gz: fc48c4ab0975c5d4a72d36c013bef2ebf3910574d42f2cea42a0e781ca7cfb6b3f60bff217e79b110bfcf38edc7f1dccd5c8e3520c460c6fb73432a8e3851658
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -42,7 +42,7 @@ gem install ulid
|
|
42
42
|
```ruby
|
43
43
|
require 'ulid'
|
44
44
|
|
45
|
-
ULID.generate
|
45
|
+
ULID.generate # 01ARZ3NDEKTSV4RRFFQ69G5FAV
|
46
46
|
```
|
47
47
|
|
48
48
|
## Specification
|
@@ -105,23 +105,10 @@ r is Randomness
|
|
105
105
|
## Test Suite
|
106
106
|
|
107
107
|
```
|
108
|
-
|
108
|
+
bundle exec rake test
|
109
109
|
```
|
110
110
|
|
111
|
-
|
111
|
+
### Credits and references:
|
112
112
|
|
113
|
-
|
114
|
-
|
115
|
-
```
|
116
|
-
|
117
|
-
```
|
118
|
-
ulid
|
119
|
-
336,331,131 op/s » encodeTime
|
120
|
-
102,041,736 op/s » encodeRandom
|
121
|
-
17,408 op/s » generate
|
122
|
-
|
123
|
-
|
124
|
-
Suites: 1
|
125
|
-
Benches: 3
|
126
|
-
Elapsed: 7,285.75 ms
|
127
|
-
```
|
113
|
+
* https://github.com/alizain/ulid
|
114
|
+
* https://github.com/ulid-org/spec
|
data/lib/ulid/generator.rb
CHANGED
@@ -2,21 +2,23 @@ require 'sysrandom'
|
|
2
2
|
|
3
3
|
module ULID
|
4
4
|
class Generator
|
5
|
-
ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
|
5
|
+
ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ' # Crockford's Base32
|
6
|
+
TIME_LENGTH = 10
|
7
|
+
RANDOM_LENGTH = 16
|
6
8
|
|
7
9
|
def generate
|
8
|
-
encode_time(milliseconds_since_epoch,
|
10
|
+
encode_time(milliseconds_since_epoch, TIME_LENGTH) + encode_random(RANDOM_LENGTH)
|
9
11
|
end
|
10
12
|
|
11
13
|
private
|
12
14
|
|
13
15
|
def encode_time(now, length)
|
14
|
-
length.times.reduce(
|
16
|
+
length.times.reduce('') do |output|
|
15
17
|
mod = now % ENCODING.length
|
16
18
|
now = (now - mod) / ENCODING.length
|
17
19
|
|
18
20
|
output << ENCODING[mod]
|
19
|
-
end
|
21
|
+
end.reverse
|
20
22
|
end
|
21
23
|
|
22
24
|
def encode_random(length)
|
@@ -24,7 +26,7 @@ module ULID
|
|
24
26
|
random = Sysrandom.random_number(ENCODING.length)
|
25
27
|
|
26
28
|
output << ENCODING[random]
|
27
|
-
end
|
29
|
+
end.reverse
|
28
30
|
end
|
29
31
|
|
30
32
|
def milliseconds_since_epoch
|
data/lib/ulid/version.rb
CHANGED
data/spec/lib/ulid_spec.rb
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ULID do
|
4
|
-
it "
|
4
|
+
it "ensures it has 26 chars" do
|
5
5
|
ulid = ULID.generate
|
6
|
+
|
6
7
|
ulid.length.must_equal 26
|
7
8
|
end
|
9
|
+
|
10
|
+
it "is sortable" do
|
11
|
+
ulid_1 = ULID.generate
|
12
|
+
ulid_2 = ULID.generate
|
13
|
+
|
14
|
+
assert ulid_2 > ulid_1
|
15
|
+
end
|
8
16
|
end
|
9
17
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ulid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Sales
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sysrandom
|