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.
@@ -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