shanekilkelly

shanekilkelly

Youtube-style IDs in Elixir

Just a bit of fun, today I found out that Youtube IDs are just url-safe base-64 truncated to 11 characters. And it turns out that Elixir ships with just the right in-built functions to make that work:

:crypto.strong_rand_bytes(8)
  |> Base.url_encode64()
  |> String.slice(0, 11)

# => "NxdgQMuCZ5Q"

EDIT:

Or, as @idi527 pointed out below, if you pass padding: false to the url_encode64() call, you don’t need the String.slice():

:crypto.strong_rand_bytes(8)
  |> Base.url_encode64(padding: false)

# => "NxdgQMuCZ5Q"

Most Liked

idi527

idi527

:+1:

You can use padding: false instead probably.

:crypto.strong_rand_bytes(8)
|> Base.url_encode64(padding: false)

Last Post!

shanekilkelly

shanekilkelly

Ah, good catch :slight_smile:

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20142 166
New
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics Top

New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New

We're in Beta

About us Mission Statement