Introducing Sqids Crystal
In an effort to blog about more of my random projects, today I’m announcing my port of Sqids to the Crystal programming language.
Sqids is a simple encoding for a list of numbers. For example,
[123, 456] gets encoded as
B9fj0X. I didn’t create this project, but I thought it was neat.
Sqids Crystal, as the name suggests, is a Crystal library for encoding and decoding Sqids. This is the thing I made!
Because Crystal is so similar to Ruby, it was a small effort to port Sqids Ruby to Crystal. For the most part, I was able to copy-paste the code and make some small changes:
- Add types. This was the most significant part, as it allowed me to remove a bunch of validation code.
- Tweak the tests to use Crystal’s
Specmodule instead of Ruby’s RSpec. This was easy but tedious.
- Change various method calls.
#size. Most of these changes were mechanical; a few required a little bit more thought but not much.
- Switch to double quotes for string literals, because Crystal doesn’t support single quoted strings.
Porting this code from Ruby to Crystal was as easy as 1, 2, 3…or should I say
It was a fun little project!