class Sqids

Overview

A Sqids encoder and decoder.

Example:

sqids = Sqids.new

id = sqids.encode([1, 2, 3])
# => "86Rf07"

numbers = sqids.decode(id)
# => [1, 2, 3]

Defined in:

sqids.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(alphabet : String = DEFAULT_ALPHABET, min_length : UInt8 = DEFAULT_MIN_LENGTH, blocklist : Set(String) = DEFAULT_BLOCKLIST) #

Creates a Sqids encoder and decoder.

Can take a custom alphabet. By default, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" is used.

sqids = Sqids.new(alphabet: "abc")

id = sqids.encode([1, 2, 3])
# => "aacacbaa"

numbers = sqids.decode(id)
# => [1, 2, 3]

Can take a minimum length, which defaults to 0 (no minimum length).

sqids = Sqids.new(min_length: 10_u8)

sqids.encode([1, 2, 3])
# => "86Rf07xd4z"

Can take a custom blocklist of words to avoid. Defaults to a long list of words.

sqids = Sqids.new(blocklist: Set.new(%w[86Rf07]))

id = sqids.encode([1, 2, 3])
# => "se8ojk"

numbers = sqids.decode(id)
# => [1, 2, 3]

[View source]

Instance Method Detail

def decode(id : String) : Array(UInt64) #

Decode a Sqid string into an array of numbers.

If the argument is an empty string, an empty array is returned.

sqids = Sqids.new

sqids.decode("86Rf07")
# => [1, 2, 3]

[View source]
def encode(numbers : Array(UInt8 | UInt16 | UInt32 | UInt64)) : String #

Encode an array of numbers into a Sqid string.

If the array is empty, an empty string is returned.

sqids = Sqids.new

sqids.encode([1, 2, 3] of UInt64)
# => "86Rf07"

[View source]
def encode(numbers : Array(Int8 | Int16 | Int32 | Int64)) : String #

Like #encode, but takes signed integers.

If any of the numbers are negative, an OverflowError is raised.

sqids = Sqids.new

sqids.encode([1, 2, 3])
# => "86Rf07"

[View source]