Return to Snippet

Revision: 12032
at May 3, 2009 14:53 by Sephr


Updated Code
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))
    }
  };

Revision: 12031
at May 3, 2009 14:48 by Sephr


Updated Code
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("")
  };

base36.encode.ascii = function(str)
  Array.map(""+str, function(c) {
    var b36 = base36.encode(c, "");
    if (b36.length === 1)
      b36 = "0" + b36;
    return b36;
  }).join("");

base36.decode.ascii = function(str)
  (""+str).replace(/[a-z0-9]{2}/gi, function(s) base36.decode(s));

Revision: 12030
at May 3, 2009 14:47 by Sephr


Updated Code
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("")
  };

base36.encode.ASCII = function(str)
  Array.map(""+str, function(c) {
    var b36 = base36.encode(c, "");
    if (b36.length === 1)
      b36 = "0" + b36;
    return b36;
  }).join("");

base36.decode.ASCII = function(str)
  (""+str).replace(/[a-z0-9]{2}/gi, function(s) base36.decode(s));

Revision: 12029
at May 3, 2009 14:43 by Sephr


Updated Code
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("")
  };

base36.encode.ASCII = function(str)
  Array.map(""+str, function(c) {
    var b36 = base36.encode(c, "");
    if (b36.length === 1)
      b36 = "0" + b36;
    return b36;
  }).join("");

base36.decode.ASCII = function(str)
  (""+str).replace(/[a-z0-9]{2}/gi, base36.decode);

Revision: 12028
at May 3, 2009 14:35 by Sephr


Updated Code
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(""),
    encodeASCII: function(str)
      base36.encode(str, ""),
    decodeASCII: function(str)
      (""+str).replace(/[a-z]{2}/gi, base36.decode)   
  };

Revision: 12027
at February 27, 2009 22:33 by Sephr


Initial Code
var base36 = {
  encode: function(str, separator) {
    var encoded = [];
    for (var i=0; i<str.length; i++)
      encoded.push(str.charCodeAt(i).toString(36))
    return encoded.join(separator||"\u200b")
  },
  decode: function(str, separator) {
    str = str.split(separator||"\u200b");
    for (var i=0; i<str.length; i++)
      str[i] = String.fromCharCode(parseInt(str[i], 36))
    return str.join("")
  }
};

Initial URL


Initial Description
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"

Initial Title
base36 Encode/Decode [JS 1.8]

Initial Tags


Initial Language
JavaScript