Tag Archives: String

Escape & Unescape – Deprecated?

Two of the most widely used JavaScript functions that are now deprecated are the escape() and unescape() functions. This means that in order to ensure that your code will continue to work in all future browsers, you should use one of the alternative functions as was indicated on MDN:

  • encodeURI()
  • decodeURI()
  • encodeURIComponent()
  • decodeURIComponent()

What To Do When They Are No More

Since these functions may not always exist, I decided to write the necessary JavaScript code to make sure they can continue to be used:

(function() {
var objGlobal = this;
if(!(objGlobal.escape && objGlobal.unescape)) {
  var escapeHash = {
    _ : function(input) {
      var ret = escapeHash[input];
      if(!ret) {
        if(input.length - 1) {
          ret = String.fromCharCode(input.substring(input.length - 3 ? 2 : 1));
        }
        else {
          var code = input.charCodeAt(0);
          ret = code < 256
            ? "%" + (0 + code.toString(16)).slice(-2).toUpperCase()
            : "%u" + ("000" + code.toString(16)).slice(-4).toUpperCase();
        }
        escapeHash[ret] = input;
        escapeHash[input] = ret;
      }
      return ret;
    }
  };
  objGlobal.escape = objGlobal.escape || function(str) {
    return str.replace(/[^\w @\*\-\+\.\/]/g, function(aChar) {
      return escapeHash._(aChar);
    });
  };
  objGlobal.unescape = objGlobal.unescape || function(str) {
    return str.replace(/%(u[\da-f]{4}|[\da-f]{2})/gi, function(seq) {
      return escapeHash._(seq);
    });
  };
}
})();

Reference

I created these functions based on information found at DevGuru.com.

String.prototype.expandTabs()

I personally love both JavaScript and Python. For this reason, from time to time I like to port functions that are available in Python, over to JavaScript. The following is a port of the expandTabs function.

String.prototype.expandTabs = function(tabSize) {
  tabSize = Math.min(Math.max(parseInt(+tabSize || 8), 1), 32);
  return this.replace(/^.*\t.*$/gm, function(line) {
    var allBefore = "";
    return line.replace(/(.*?)(\t+)/g, function(match, before, tabs) {
      allBefore += before;
      tabs = new Array(1 + tabSize * tabs.length - allBefore.length % tabSize).join(" ");
      allBefore += tabs;
      return before + tabs;
    });
  });
};

The biggest difference between this JavaScript version and the Python version is the limitation of a 32 character tab size. If you want to increase the maximum tab size, simply change 32 to something else.

String.protototype.toCharCodes()

One of the functions that didn’t make the cut for jPaq is String.prototype.toCharCodes().  The reason is I don’t feel that it is a function that many people are looking for.  Still, if you are looking for a good implementation for it, you can use this:

String.prototype.toCharCodes = function() {
  for(var arr = this.split(""), i = this.length - 1; i >= 0; i--)
    arr[i] = arr[i].charCodeAt(0);
  return arr;
};