make a string url-safe

When running devops scripts (among other things), we often need to get a url-safe representation of an arbitrary string. This is commonly known as “url slug” by the average netizens.

As my JavaScript is better than my bash, and npx is more universally available than my capability to master both Windows and Linux shell scripts, I decided to publish a tiny command-line interface that takes in an arbitrary string and outputs a url-safe representation of it.

I intend to mainly use this for my personal projects (in other words: I’m not offering “warranty” of any kind 😅), so I published the script behind my own namespace. Anyhow, it can be used like this (if you have Node.js installed):

npx khang util urlize <your URL-unsafe string here>

(don’t forget to escape your input string properly esp. if you’re using this straight from the command line 😉)

For example:

npx khang util urlize haH\#\!woa-\%20_351\$
npx: installed 1 in 2.936s
haH--woa--20_351-

As seen from the above example, the url-unsafe string haH#!woa-\%20_351\$ was turned to a less hostile representation: haH--woa--20_351-

I have lots of use for this util. I hope you’ll find a good use for it, too!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s