Nodejs String Quote quote()

Here you can find the source of quote()

Method Source Code

// Taken from "Remedial Javascript" by Douglas Crockford:
// http://javascript.crockford.com/remedial.html

String.prototype.quote = function () {
    var c, i, l = this.length, o = '"';
    for (i = 0; i < l; i += 1) {
        c = this.charAt(i);/*from   w w  w.ja v a 2s . c  o  m*/
        if (c >= ' ') {
            if (c === '\\' || c === '"') {
                o += '\\';
            }
            o += c;
        } else {
            switch (c) {
            case '\b':
                o += '\\b';
                break;
            case '\f':
                o += '\\f';
                break;
            case '\n':
                o += '\\n';
                break;
            case '\r':
                o += '\\r';
                break;
            case '\t':
                o += '\\t';
                break;
            default:
                c = c.charCodeAt();
                o += '\\u00' + Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }
        }
    }
    return o + '"';
};

Related

  1. dequote()
    String.prototype.dequote = function() {
      return this.replace(/^"|"$/g, '');
    
  2. quote(()
    String.prototype.quote = (function(){
        return '"' + this + '"';
    })
    
  3. quote(sym)
    String.prototype.quote = function(sym) {
        if (!sym) {
            sym = '"';
        return sym + this + sym;
    };