humanized_id 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -1
- data/CHANGELOG.md +14 -0
- data/Gemfile +1 -0
- data/Rakefile +1 -0
- data/Readme.md +13 -16
- data/bin/console +1 -0
- data/humanized_id.gemspec +2 -2
- data/lib/humanized_id.rb +3 -4
- data/lib/humanized_id/humanizer.rb +15 -21
- data/lib/humanized_id/rand_generator.rb +9 -8
- data/lib/humanized_id/version.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae0f1548e28b86a0eb8ef77c60e2f443ee921d71
|
4
|
+
data.tar.gz: ed2e71e69d3894663724123b8e6af446da5f1e4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f88c10da3c918d4801648690f132842919523684331668898d6d886fdcaf716f8d64993f707e7c85c357158bd00b396abd759a9c31b6442df90d53c55ac312ed
|
7
|
+
data.tar.gz: 451492aef369a46fd67cd2b661246e4bfe543ca11fda7cff490b61ab757e6300dfddf92ccf9fd3c69da02c876e2bde1f95f740a29eeea05abac8c3f2c185cbf6
|
data/.rubocop.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.1
|
3
3
|
|
4
4
|
Metrics/LineLength:
|
5
5
|
Max: 100
|
@@ -12,3 +12,9 @@ Style/BlockDelimiters:
|
|
12
12
|
|
13
13
|
Style/TrivialAccessors:
|
14
14
|
AllowPredicates: true
|
15
|
+
|
16
|
+
Style/SignalException:
|
17
|
+
EnforcedStyle: semantic
|
18
|
+
|
19
|
+
Style/SingleLineBlockParams:
|
20
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/Readme.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# HumanizedId
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/humanized_id.svg)](https://badge.fury.io/rb/humanized_id)
|
4
|
+
[![Build Status](https://travis-ci.org/sealink/humanized_id.svg?branch=master)](https://travis-ci.org/sealink/humanized_id)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/sealink/humanized_id/badge.svg?branch=master)](https://coveralls.io/github/sealink/humanized_id?branch=master)
|
6
|
+
[![Dependency Status](https://gemnasium.com/sealink/humanized_id.svg)](https://gemnasium.com/sealink/humanized_id)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/sealink/humanized_id/badges/gpa.svg)](https://codeclimate.com/github/sealink/humanized_id)
|
8
|
+
|
3
9
|
HumanizedId is a gem designed to help you either:
|
4
10
|
- Convert an existing numerical id into a 'human friendly' alphanumerical id
|
5
11
|
- Generate a random 'human friendly' id that is of a specified or default length
|
@@ -27,10 +33,7 @@ Or install it yourself as:
|
|
27
33
|
|
28
34
|
### Humanize an existing id
|
29
35
|
|
30
|
-
The simplest way to call humanize is to pass in an existing numerical id,
|
31
|
-
and let HumanizedId return a human friendly version of that id. This id will be shorter
|
32
|
-
in length (due to base conversion) and will be an alphanumerical string based on
|
33
|
-
a 'human safe' character-set
|
36
|
+
The simplest way to call humanize is to pass in an existing numerical id, and let HumanizedId return a human friendly version of that id. This id will be shorter in length (due to base conversion) and will be an alphanumerical string based on a 'human safe' character-set.
|
34
37
|
|
35
38
|
```ruby
|
36
39
|
humanized_id = HumanizedId.humanize id: 1234567
|
@@ -39,20 +42,16 @@ humanized_id = HumanizedId.humanize id: 1234567
|
|
39
42
|
|
40
43
|
#### Ensuring minimum length of output id
|
41
44
|
|
42
|
-
An optional min_length flag can be passed in order to guarantee the minimum length
|
43
|
-
of the returned value. This will be done by 'padding' the return id with the
|
44
|
-
safe-charset default value '2'
|
45
|
+
An optional min_length flag can be passed in order to guarantee the minimum length of the returned value. This will be done by 'padding' the return id with the safe-charset default value '2'.
|
45
46
|
|
46
47
|
```ruby
|
47
48
|
humanized_id = HumanizedId.humanize id: 1234567, min_length: 20
|
48
49
|
# Returns '222222222222226RDFD'
|
49
50
|
```
|
50
51
|
|
51
|
-
Note that the original length is not preserved during the base conversion, so you
|
52
|
-
will need to explicitely pass in a min_length if you'd like a return id of the same length
|
52
|
+
Note that the original length is not preserved during the base conversion, so you will need to explicitly pass in a min_length if you'd like a output id of the same length.
|
53
53
|
|
54
|
-
Also note that if you specify a min_length shorter than the actual output id length,
|
55
|
-
the output id will not be modified (as expected)
|
54
|
+
Also note that if you specify a min_length shorter than the actual output id length, the output id will not be modified (as expected).
|
56
55
|
|
57
56
|
#### Adding a prefix
|
58
57
|
|
@@ -63,12 +62,11 @@ humanized_id = HumanizedId.humanize id: 1234567, min_length: 20, prefix: 'TEST'
|
|
63
62
|
# Will return 'TEST222222222222226RDFD'
|
64
63
|
```
|
65
64
|
|
66
|
-
The prefix is added to the humanized id after all other processing (including min_length padding).
|
67
|
-
Therefore the total length of the above example wil be 20 + 'TEST'.length = 24
|
65
|
+
The prefix is added to the humanized id after all other processing (including min_length padding). Therefore the total length of the above example wil be 20 + 4 (length of 'TEST').
|
68
66
|
|
69
67
|
### Generating a random humanized id
|
70
68
|
|
71
|
-
Call 'generate_random' with optional length and prefix
|
69
|
+
Call 'generate_random' with optional length and prefix.
|
72
70
|
|
73
71
|
```ruby
|
74
72
|
random_humanized_id = HumanizedId.generate_random
|
@@ -80,8 +78,7 @@ Will generate a random human friendly id of default length (see HumanizedId::DEF
|
|
80
78
|
random_humanized_id = HumanizedId.generate_random length: 20, prefix: 'TEST'
|
81
79
|
```
|
82
80
|
|
83
|
-
This will generate a random humanized id of length 20 and then add the prefix to the id
|
84
|
-
thus resulting in a total length of 24
|
81
|
+
This will generate a random humanized id of length 20 and then add the prefix to the id thus resulting in a total length of 24.
|
85
82
|
|
86
83
|
## Development
|
87
84
|
|
data/bin/console
CHANGED
data/humanized_id.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
lib = File.expand_path('../lib', __FILE__)
|
4
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
@@ -24,11 +25,10 @@ Gem::Specification.new do |spec|
|
|
24
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
26
|
spec.require_paths = ['lib']
|
26
27
|
|
27
|
-
spec.required_ruby_version = '>= 2.
|
28
|
+
spec.required_ruby_version = '>= 2.1'
|
28
29
|
|
29
30
|
spec.add_development_dependency 'bundler'
|
30
31
|
spec.add_development_dependency 'rake'
|
31
|
-
spec.add_development_dependency 'dotenv'
|
32
32
|
spec.add_development_dependency 'rspec'
|
33
33
|
spec.add_development_dependency 'simplecov'
|
34
34
|
spec.add_development_dependency 'simplecov-rcov'
|
data/lib/humanized_id.rb
CHANGED
@@ -3,16 +3,15 @@ module HumanizedId
|
|
3
3
|
DEFAULT_GENERATION_LENGTH = 24
|
4
4
|
SIMILAR_NUMBERS_LETTERS = %w(0 O 1 I L 5 S 8 B).freeze
|
5
5
|
VOWELS = %w(A E I O U).freeze
|
6
|
-
|
7
|
-
(('0'..'9').to_a + ('A'..'Z').to_a - SIMILAR_NUMBERS_LETTERS - VOWELS).freeze
|
6
|
+
CHARSET = (('0'..'9').to_a + ('A'..'Z').to_a - SIMILAR_NUMBERS_LETTERS - VOWELS).freeze
|
8
7
|
|
9
8
|
class << self
|
10
9
|
def humanize(id:, min_length: nil, prefix: '')
|
11
|
-
|
10
|
+
Humanizer.new(id: id, min_length: min_length, prefix: prefix).generate_humanized_id
|
12
11
|
end
|
13
12
|
|
14
13
|
def generate_random(prefix: '', length: DEFAULT_GENERATION_LENGTH)
|
15
|
-
|
14
|
+
RandGenerator.new(prefix: prefix, length: length).generate_random_humanized_id
|
16
15
|
end
|
17
16
|
end
|
18
17
|
Error = Class.new(StandardError)
|
@@ -1,43 +1,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module HumanizedId
|
3
3
|
class Humanizer
|
4
|
-
SOURCE_CHARSET =
|
4
|
+
SOURCE_CHARSET = (('0'..'9').to_a + ('a'..'z').to_a).freeze
|
5
5
|
|
6
6
|
def initialize(id:, min_length: nil, prefix: '')
|
7
7
|
@id = id
|
8
8
|
@min_length = min_length
|
9
|
-
@prefix = prefix.
|
10
|
-
@target_charset =
|
11
|
-
@source_charset = SOURCE_CHARSET.join
|
9
|
+
@prefix = prefix.to_s
|
10
|
+
@target_charset = CHARSET.join
|
11
|
+
@source_charset = SOURCE_CHARSET.join
|
12
12
|
end
|
13
13
|
|
14
14
|
def generate_humanized_id
|
15
|
-
new_id =
|
16
|
-
new_id = convert_to_target_charset id: new_id
|
17
|
-
new_id = resize id: new_id if @min_length
|
15
|
+
new_id = resize convert_to_target_charset convert_to_target_base(@id)
|
18
16
|
"#{@prefix}#{new_id}"
|
19
17
|
end
|
20
18
|
|
21
19
|
private
|
22
20
|
|
23
|
-
def convert_to_target_base(id
|
24
|
-
id.
|
25
|
-
|
26
|
-
raise Error, 'id is not an integer'
|
21
|
+
def convert_to_target_base(id)
|
22
|
+
fail Error, 'id is not an integer' unless id.is_a? Integer
|
23
|
+
id.to_s(@target_charset.length)
|
27
24
|
end
|
28
25
|
|
29
|
-
def convert_to_target_charset(
|
30
|
-
|
31
|
-
|
32
|
-
id.tr(source_charset_subset, target_charset)
|
26
|
+
def convert_to_target_charset(id)
|
27
|
+
source_charset_subset = @source_charset.slice(0, @target_charset.length)
|
28
|
+
id.tr(source_charset_subset, @target_charset)
|
33
29
|
end
|
34
30
|
|
35
|
-
def resize(id
|
36
|
-
if min_length
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
id
|
31
|
+
def resize(id)
|
32
|
+
return id if @min_length.nil? || @min_length <= id.length
|
33
|
+
padding = @target_charset[0] * (@min_length - id.length)
|
34
|
+
"#{padding}#{id}"
|
41
35
|
end
|
42
36
|
end
|
43
37
|
end
|
@@ -1,9 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module HumanizedId
|
2
3
|
class RandGenerator
|
3
4
|
def initialize(prefix: '', length:)
|
4
5
|
@prefix = prefix.nil? ? '' : prefix
|
5
6
|
@length = length
|
6
|
-
@target_charset =
|
7
|
+
@target_charset = CHARSET
|
7
8
|
end
|
8
9
|
|
9
10
|
def generate_random_humanized_id
|
@@ -12,13 +13,13 @@ module HumanizedId
|
|
12
13
|
|
13
14
|
private
|
14
15
|
|
15
|
-
def generate_random
|
16
|
-
SecureRandom.random_bytes(length).unpack('C*').map{ |byte|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
def generate_random
|
17
|
+
SecureRandom.random_bytes(@length).unpack('C*').map { |byte| map_to_char(byte) }.join
|
18
|
+
end
|
19
|
+
|
20
|
+
def map_to_char(byte)
|
21
|
+
index = byte % @target_charset.size
|
22
|
+
@target_charset[index]
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
data/lib/humanized_id/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanized_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akil Madan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: dotenv
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rspec
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,6 +135,7 @@ files:
|
|
149
135
|
- ".rubocop.yml"
|
150
136
|
- ".ruby-version"
|
151
137
|
- ".travis.yml"
|
138
|
+
- CHANGELOG.md
|
152
139
|
- CODE_OF_CONDUCT.md
|
153
140
|
- Gemfile
|
154
141
|
- LICENSE.txt
|
@@ -173,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
173
160
|
requirements:
|
174
161
|
- - ">="
|
175
162
|
- !ruby/object:Gem::Version
|
176
|
-
version: '2.
|
163
|
+
version: '2.1'
|
177
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
165
|
requirements:
|
179
166
|
- - ">="
|