Class: Lite::Uxid::Irreversible::Nanoid

Inherits:
Base
  • Object
show all
Defined in:
lib/lite/uxid/irreversible/nanoid.rb

Instance Attribute Summary

Attributes inherited from Base

#opts

Instance Method Summary collapse

Methods inherited from Base

decode, #decode, encode, #initialize

Methods included from Base

#decode

Constructor Details

This class inherits a constructor from Lite::Uxid::Irreversible::Base

Instance Method Details

#encodeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lite/uxid/irreversible/nanoid.rb', line 8

def encode
  encoded_id = +""

  loop do
    cached_bytes = bytes

    (0...step).each do |idx|
      byte = cached_bytes[idx] & mask
      char = byte && coder_charset[byte]
      next unless char

      encoded_id << char
      return "#{coder_prefix}#{encoded_id}" if encoded_id.size == coder_size
    end
  end

  "#{coder_prefix}#{encoded_id}"
end