make_id 0.1.2 → 0.1.3

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/make_id/version.rb +1 -1
  3. data/lib/make_id.rb +52 -0
  4. metadata +17 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2448e655a9a006abb23272b2b54c6b4fc537f702407c931afcf106b82c468b04
4
- data.tar.gz: 9852758f34e12c2be8aa90149cdded09e011f2ab879320089fc1bfb3f577d849
3
+ metadata.gz: 7f93b4ee4fa18932ab4bc7769214bee5a4297555d776ab4b38a31a56e3c98fe6
4
+ data.tar.gz: ea62bc4fa6bab34e649d9c3983146b55c81dbe469d16142c9847c00c8c291f49
5
5
  SHA512:
6
- metadata.gz: 108e40b61bfdcc00ac5e550b4afb14fb8f7298df617d10b7179975b2be8f6f04c6b4693fc6909de26f42e6a9977333d12f7ecc5c5846b1317b3ab728b6e6d04d
7
- data.tar.gz: 25c7229af03bc353707c2ee832f00c2ad63243318156ffa51bcd197f328a2cae865eaf37fa0949d7c8d98bcf9e583b4ec8f2fb2563317d7584b906b9f3c8cf08
6
+ metadata.gz: 183ea9a2f7e14c6a70d5f0addec62718ce467face83be99128163df22eae71cb5a6cdb813fe413cac2bb0daa80e1bda17267400995455fa11b916ba37bb20662
7
+ data.tar.gz: c2001d8acbc1427e88007146a05a32bbf47e725cce418ac42fd6242d94a23757819afefbaca268558943881012ba3ddcf5b133bc580b2fea44b6f4af092041bd
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MakeId
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/make_id.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "make_id/version"
4
4
  require "securerandom"
5
5
  require "zlib"
6
+ require "digest"
6
7
 
7
8
  # MakeID generates record Identifiers other than sequential integers.
8
9
  # MakeId - From the "make_id" gem found at https://github.com/afair/make_id
@@ -100,6 +101,21 @@ module MakeId
100
101
  check_digit ? append_check_digit(id, base) : id
101
102
  end
102
103
 
104
+ # Takes a string and returns an 8-byte integer for the string.
105
+ # This implementation uses the SHA256 hash of the string to generate the ID.
106
+ # You can also have random bits added to the ID to make it unique.
107
+ # This is not a unique ID, but a repeatable ID for the same string
108
+ # (unless random_bits is used), and can be used like a hashing value.
109
+ def self.string_id(string, random_bits: 0, base: 10, bytes: 8)
110
+ id = Digest::SHA256.hexdigest(string)[0, bytes * 2].to_i(16)
111
+ if random_bits > 0
112
+ id >> random_bits
113
+ id << random_bits
114
+ id |= SecureRandom.random_number(2**random_bits - 1)
115
+ end
116
+ (base == 10) ? id : int_to_base(id, base)
117
+ end
118
+
103
119
  ##############################################################################
104
120
  # UUID - Universally Unique Identifier
105
121
  ##############################################################################
@@ -197,6 +213,36 @@ module MakeId
197
213
  ].join
198
214
  end
199
215
 
216
+ # Returns an id using the current epoch time with floating precision and random bits.
217
+ # Be default, this returns an 8-byte integer in ascending order within precision.
218
+ # Format is approximately "SSSSSSSSSMMMRRR" (seconds, milliseconds, random).
219
+ # * precision (1..5) The number of milliseconds decimeal places to use. Default is 4.
220
+ # * random_bits (1..) The number of bits to use for the random number. Default is 18.
221
+ # * time - Time object to use for the token. Default is Time.now
222
+ # * base - The base to use for the id. Default is 10
223
+ # * chars - The character set to use for the base conversion. Default is BASE62
224
+ def self.time_id(base: 10, precision: 4, random_bits: 18, time: nil, chars: nil)
225
+ seconds = ((time || Time.now).to_f * (10**precision)).to_i
226
+ id = (seconds * (2**random_bits)) | SecureRandom.random_number(2**random_bits - 1)
227
+ (base == 10) ? id : int_to_base(id, base, chars: chars)
228
+ end
229
+
230
+ # Returns a string from the time with floating precision, and a random number appended.
231
+ # This calls time_id() and returns a token of the given size.
232
+ # If size is zero, the full token is returned. If size is greater than the token,
233
+ # the token is right-justified with zeros. If the token is larger than size, the
234
+ # right-most characters are returned.
235
+ def self.time_token(size: 0, base: 62, chars: nil, time: nil, precision: 3, random_bits: 8)
236
+ token = time_id(base: base, precision: precision, random_bits: random_bits, time: time, chars: chars)
237
+ if size == 0
238
+ token
239
+ elsif token.size < size
240
+ token.rjust(size, "0")
241
+ else
242
+ token[-size, size]
243
+ end
244
+ end
245
+
200
246
  ##############################################################################
201
247
  # Snowflake Id - Epoch + millisecond + worker_id id + sequence number
202
248
  # Snowflakes are a form of unique identifier used in distributed computing.
@@ -370,6 +416,12 @@ module MakeId
370
416
  id.to_s + compute_check_digit(id, base)
371
417
  end
372
418
 
419
+ # Removes and validates the check digit. Retuns nil if the check digit is invalid.
420
+ def self.remove_check_digit(id, base = 10)
421
+ id, cd = id.to_s[0..-2], id.to_s[-1]
422
+ valid_check_digit?(id + cd, base) ? id : nil
423
+ end
424
+
373
425
  # Returns a character computed using the CRC32 algorithm
374
426
  # Uses a pre-defined check_proc if configured. See check_proc=().
375
427
  def self.compute_check_digit(id, base = 10)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen Fair
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-11-18 00:00:00.000000000 Z
10
+ date: 2025-03-18 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: base64
@@ -24,6 +23,20 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: irb
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
27
40
  description: MakeId is a collection of record Identifier generators
28
41
  email:
29
42
  - allen.fair@gmail.com
@@ -49,7 +62,6 @@ licenses:
49
62
  metadata:
50
63
  homepage_uri: https://github.com/afair/make_id
51
64
  source_code_uri: https://github.com/afair/make_id
52
- post_install_message:
53
65
  rdoc_options: []
54
66
  require_paths:
55
67
  - lib
@@ -64,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
76
  - !ruby/object:Gem::Version
65
77
  version: '0'
66
78
  requirements: []
67
- rubygems_version: 3.5.20
68
- signing_key:
79
+ rubygems_version: 3.6.2
69
80
  specification_version: 4
70
81
  summary: MakeId provides a collection of record Identifier generators
71
82
  test_files: []