/ Published in: JavaScript

base36 Encode/Decode Strings
====================
*Requires JavaScript 1.8 or higher*
Syntax:
`base36[.ascii].encode(str[, separator]):String`
`base36[.ascii].decode(str[, separator]):String`
Default character separator is ZERO WIDTH SPACE U+200B
Examples:
base36.encode("foobar", "-") == "2u-33-33-2q-2p-36"
base36.decode("2u-33-33-2q-2p-36", "-") == "foobar"
====================
*Requires JavaScript 1.8 or higher*
Syntax:
`base36[.ascii].encode(str[, separator]):String`
`base36[.ascii].decode(str[, separator]):String`
Default character separator is ZERO WIDTH SPACE U+200B
Examples:
base36.encode("foobar", "-") == "2u-33-33-2q-2p-36"
base36.decode("2u-33-33-2q-2p-36", "-") == "foobar"
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
if (delete base36) const base36 = { // U+200B is a zero-width space encode: function(str, separator) Array.map(""+(str||""), function(c) c.charCodeAt(0).toString(36)).join(separator||"\u200b"), decode: function(str, separator) (""+(str||"")).split(separator||"\u200b").map(function(s) String.fromCharCode(parseInt(s, 36))).join(""), ascii: { encode: function(str) Array.map(""+(str||""), function(c) { var b36 = base36.encode(c, ""); if (b36.length === 1) b36 = "0" + b36; return b36; }).join(""), decode: function(str) (""+(str||"")).replace(/[a-z0-9]{2}/gi, function(s) base36.decode(s)) } };
Comments
