Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sija/base62.cr
Base62 encoder/decoder for Crystal
https://github.com/sija/base62.cr
base62 conversion crystal encoding url-shortener
Last synced: 29 days ago
JSON representation
Base62 encoder/decoder for Crystal
- Host: GitHub
- URL: https://github.com/sija/base62.cr
- Owner: Sija
- License: mit
- Created: 2018-07-27T14:00:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-12T15:47:39.000Z (about 1 year ago)
- Last Synced: 2024-08-01T17:32:59.760Z (3 months ago)
- Topics: base62, conversion, crystal, encoding, url-shortener
- Language: Crystal
- Homepage:
- Size: 15.6 KB
- Stars: 10
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Base62.cr [![CI](https://github.com/Sija/base62.cr/actions/workflows/ci.yml/badge.svg)](https://github.com/Sija/base62.cr/actions/workflows/ci.yml) [![Releases](https://img.shields.io/github/release/Sija/base62.cr.svg)](https://github.com/Sija/base62.cr/releases) [![License](https://img.shields.io/github/license/Sija/base62.cr.svg)](https://github.com/Sija/base62.cr/blob/master/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`:
```yaml
dependencies:
base62:
github: Sija/base62.cr
```## Usage
```crystal
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:
```crystal
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 ()
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](https://github.com/Sija) Sijawusz Pur Rahnama - creator, maintainer