Skip to content
/ base62.cr Public

Base62 encoder/decoder for Crystal

License

Notifications You must be signed in to change notification settings

Sija/base62.cr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Base62.cr CI Releases License

Crystal shard for Base62 encoding/decoding. It's especially useful for converting data into shortened strings suitable for URL shortening and/or obfuscating auto-incrementing resource ids from being exposed through RESTful APIs.

What is Base62 encoding?

Base62 encoding converts numbers to ASCII strings (0-9, a-z, and A-Z) and vice versa, which typically results in comparatively short strings. Such identifiers also tend to be more readily identifiable by humans.

Installation

Add this to your application's shard.yml:

dependencies:
  base62:
    github: Sija/base62.cr

Usage

require "base62"

Base62.encode(999)  # => "G7"
Base62.decode("G7") # => 999

This uses the default ASCII character set for encoding/decoding.

It's also possible to define a custom character set instead:

charset = "~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-="

Base62.encode(999, charset)  # => "F3"
Base62.decode("F3", charset) # => 999

Note that charset typically expects the respective string to contain exactly 62 unique characters. It's also possible to use character sets with more than 62 characters to achieve shorter identifiers for large numbers.

Contributing

  1. Fork it (https://github.com/Sija/base62.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • @Sija Sijawusz Pur Rahnama - creator, maintainer