encoded_id 1.0.0.rc6 → 1.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -4
- data/README.md +44 -33
- data/context/encoded_id.md +229 -75
- data/lib/encoded_id/alphabet.rb +2 -0
- data/lib/encoded_id/blocklist.rb +17 -7
- data/lib/encoded_id/encoders/base_configuration.rb +145 -0
- data/lib/encoded_id/encoders/{hash_id.rb → hashid.rb} +53 -57
- data/lib/encoded_id/encoders/hashid_configuration.rb +33 -0
- data/lib/encoded_id/encoders/{hash_id_consistent_shuffle.rb → hashid_consistent_shuffle.rb} +4 -4
- data/lib/encoded_id/encoders/{hash_id_ordinal_alphabet_separator_guards.rb → hashid_ordinal_alphabet_separator_guards.rb} +28 -54
- data/lib/encoded_id/encoders/{hash_id_salt.rb → hashid_salt.rb} +3 -3
- data/lib/encoded_id/encoders/my_sqids.rb +5 -16
- data/lib/encoded_id/encoders/sqids.rb +25 -11
- data/lib/encoded_id/encoders/sqids_configuration.rb +17 -0
- data/lib/encoded_id/encoders/sqids_with_blocklist_mode.rb +52 -0
- data/lib/encoded_id/hex_representation.rb +6 -9
- data/lib/encoded_id/reversible_id.rb +56 -138
- data/lib/encoded_id/version.rb +1 -2
- data/lib/encoded_id.rb +16 -19
- metadata +27 -10
- data/lib/encoded_id/encoders/base.rb +0 -71
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# rbs_inline: enabled
|
|
4
|
-
|
|
5
|
-
module EncodedId
|
|
6
|
-
module Encoders
|
|
7
|
-
class Base
|
|
8
|
-
# @rbs @min_hash_length: Integer
|
|
9
|
-
# @rbs @alphabet: Alphabet
|
|
10
|
-
# @rbs @salt: String
|
|
11
|
-
# @rbs @blocklist: Blocklist
|
|
12
|
-
|
|
13
|
-
# @rbs (String salt, ?Integer min_hash_length, ?Alphabet alphabet, ?Blocklist blocklist) -> void
|
|
14
|
-
def initialize(salt, min_hash_length = 0, alphabet = Alphabet.alphanum, blocklist = Blocklist.empty)
|
|
15
|
-
@min_hash_length = min_hash_length
|
|
16
|
-
@alphabet = alphabet
|
|
17
|
-
@salt = salt
|
|
18
|
-
@blocklist = blocklist
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
attr_reader :min_hash_length #: Integer
|
|
22
|
-
attr_reader :alphabet #: Alphabet
|
|
23
|
-
attr_reader :salt #: String
|
|
24
|
-
attr_reader :blocklist #: Blocklist
|
|
25
|
-
|
|
26
|
-
# Encode array of numbers into a string
|
|
27
|
-
# @rbs (Array[Integer] numbers) -> String
|
|
28
|
-
def encode(numbers)
|
|
29
|
-
raise NotImplementedError, "#{self.class} must implement #encode"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# Encode hexadecimal string(s) into a string
|
|
33
|
-
# @rbs (String str) -> String
|
|
34
|
-
def encode_hex(str)
|
|
35
|
-
return "" unless hex_string?(str)
|
|
36
|
-
|
|
37
|
-
numbers = str.scan(/[\w\W]{1,12}/).map do |num|
|
|
38
|
-
"1#{num}".to_i(16)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
encode(numbers)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# Decode a string back into an array of numbers
|
|
45
|
-
# @rbs (String hash) -> Array[Integer]
|
|
46
|
-
def decode(hash)
|
|
47
|
-
raise NotImplementedError, "#{self.class} must implement #decode"
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# Decode a string back into an array of hexadecimal strings
|
|
51
|
-
# @rbs (String hash) -> String
|
|
52
|
-
def decode_hex(hash)
|
|
53
|
-
numbers = decode(hash)
|
|
54
|
-
return "" if numbers.empty?
|
|
55
|
-
|
|
56
|
-
ret = numbers.map do |n|
|
|
57
|
-
n.to_s(16)[1..]
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
ret.join.upcase
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
private
|
|
64
|
-
|
|
65
|
-
# @rbs (String string) -> MatchData?
|
|
66
|
-
def hex_string?(string)
|
|
67
|
-
string.to_s.match(/\A[0-9a-fA-F]+\Z/)
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|