nanoid 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nanoid.rb +12 -9
  3. data/lib/nanoid/version.rb +3 -1
  4. metadata +13 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b4bc9b383a529f747be9817cd0c3c6991b4f23a
4
- data.tar.gz: c78ba7699645c46b0e54ffde520946b42a9ae241
3
+ metadata.gz: 73d89528124fe3ce6e356621bdd47dafb3c0d63e
4
+ data.tar.gz: 92595e297bdad2d9608b03c67434e75848ac2ee1
5
5
  SHA512:
6
- metadata.gz: 88ebff8d8b2785e0e1102b25d173f20bc57605124bb3a6227ac202ed84f3ebb17360d6ab0648ad638326fd58bd8c44637eb87acd9aa0e702a37d7d3ac1a5219e
7
- data.tar.gz: c5524dcda652475be03f045c4c0f41ffbac0ed7335fa1bafe8e376a09e996e24547fbe6d67339213aacccdc8bff8f0b8ae022dfa36901ef551437587b5eeea6d
6
+ metadata.gz: 28c149c686bea5f5c7ec5a9c59d819f014c2ac17b9a7be70200eb21d8cb1e203395dab02de49b67dc956ffd51cb57bee23f6ec86a52bdeb7d18c252a51ffcec0
7
+ data.tar.gz: cda8cfada149a6065ae8cd9f10a67858cc22af1e80e14d445b1701c34e643bd7de7373cdc3048568d534e8b80d6c26f1c5e4bcdd7f262ae6a4d8f0a0b2eaf8d1
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'securerandom'
2
4
 
3
5
  module Nanoid
4
- MASKS = [15, 31, 63, 127, 255].freeze
5
- SAFE_ALPHABET = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
6
+ SAFE_ALPHABET = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
6
7
 
7
- def self.generate(size: 22, alphabet: SAFE_ALPHABET)
8
+ def self.generate(size: 21, alphabet: SAFE_ALPHABET)
8
9
  return simple_generate(size: size) if alphabet == SAFE_ALPHABET
9
10
 
10
11
  complex_generate(size: size, alphabet: alphabet)
@@ -13,23 +14,25 @@ module Nanoid
13
14
  def self.simple_generate(size:)
14
15
  bytes = random_bytes(size)
15
16
 
16
- (0...size).inject('') do |acc, i|
17
+ (0...size).reduce('') do |acc, i|
17
18
  acc + SAFE_ALPHABET[bytes[i] & 63]
18
19
  end
19
20
  end
20
21
 
21
22
  def self.complex_generate(size:, alphabet:)
22
- mask = MASKS.find { |i| i >= alphabet.size - 1 }
23
- step = (1.6 * mask * size / alphabet.size).ceil
23
+ alphabet_size = alphabet.size
24
+ mask = (2 << Math.log(alphabet_size - 1) / Math.log(2)) - 1
25
+ step = (1.6 * mask * size / alphabet_size).ceil
24
26
 
25
27
  id = ''
26
- catch (:computed) do
28
+ catch :computed do
27
29
  loop do
28
30
  bytes = random_bytes(size)
29
31
  (0...step).each do |i|
30
32
  byte = bytes[i] & mask
31
- if byte && alphabet[byte]
32
- id += alphabet[byte]
33
+ character = byte && alphabet[byte]
34
+ if character
35
+ id = "#{id}#{character}"
33
36
  throw :computed if id.size == size
34
37
  end
35
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NanoId
2
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radovan Smitala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-16 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '12.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '12.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '5.10'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '5.10'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: reek
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '4.5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '4.5'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '0.5'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '0.5'
83
83
  description:
84
84
  email:
85
85
  executables: []
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.6.11
111
+ rubygems_version: 2.6.13
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Ruby implementation of Nanoid, secure URL-friendly unique ID generator