Skip to content

Instantly share code, notes, and snippets.

@sunnycmf
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnycmf/9246814 to your computer and use it in GitHub Desktop.
Save sunnycmf/9246814 to your computer and use it in GitHub Desktop.
trying to escape all chars from an input file
var fs = require('fs');
fs.readFile('testing.txt', {encoding:'utf8'}, function (err, data) {
if (err) throw err;
console.log('orig:\n' + data);
console.log('\n\n\nafter JSON.stringify:\n' + JSON.stringify(data));
console.log('\n\n\nafter JSON.stringify escapeSpecialChars() :\n' + JSON.stringify(data).escapeSpecialChars());
});
String.prototype.escapeSpecialChars = function() {
return this.replace(/\\n/g, "\\n").replace(/\\'/g, "\\'").replace(/\\"/g, '\\"').replace(/\\&/g, "\\&").replace(/\\r/g, "\\r").replace(/\\t/g, "\\t").replace(/\\b/g, "\\b").replace(/\\f/g, "\\f");
};
var fs = require('fs');
sunny
newline
another new line
testing tab more tab and more
i'm trying to add\n\n invalid\tchars. \n\t
@sunnycmf
Copy link
Author

result is so sad:

orig:
sunny
newline
another new line
testing tab more    tab and more





after:
sunny
newline
another new line
testing tab more    tab and more

@hkbenchan
Copy link

var fs = require('fs');
fs.readFile('testing.txt', {encoding:'utf8'}, function (err, data) {
  if (err) throw err;
  console.log('orig:\n' + data + '\n\n\n');
  console.log('after:\n' + data.escapeSpecialChars());
});

String.prototype.escapeSpecialChars = function() {
    return JSON.stringify(this).replace(/\\n/g, "\\n").replace(/\\'/g, "\\'").replace(/\\"/g, '\\"').replace(/\\&/g, "\\&").replace(/\\r/g, "\\r").replace(/\\t/g, "\\t").replace(/\\b/g, "\\b").replace(/\\f/g, "\\f");
};

@hkbenchan
Copy link

Result:
orig:
sunny
newline
another new line
testing tab more tab and more

after:
"sunny\nnewline\nanother new line\ntesting tab\tmore\ttab\tand\tmore"

@sunnycmf
Copy link
Author

new result:

orig:
sunny
newline
another new line
testing tab more    tab and more
i'm trying to add\n\n invalid\tchars. \n\t




after JSON.stringify:
"sunny\nnewline\nanother new line\ntesting\ttab\tmore\ttab\tand\tmore\ni'm trying to add\\n\\n invalid\\tchars. \\n\\t\n"



after JSON.stringify escapeSpecialChars() :
"sunny\nnewline\nanother new line\ntesting\ttab\tmore\ttab\tand\tmore\ni'm trying to add\\n\\n invalid\\tchars. \\n\\t\n"

@hkbenchan
Copy link

String.prototype.escapeSpecialChars = function() {
    return this.replace(/\n/g, "\\n")
               .replace(/\\'/g, "\\'")
               .replace(/\\"/g, '\\"')
               .replace(/\\&/g, "\\&")
               .replace(/\\r/g, "\\r")
               .replace(/\t/g, "\\t")
               .replace(/\\b/g, "\\b")
               .replace(/\\f/g, "\\f");
};

// Returns the pass.json object (not a string).
Passbook.prototype.getPassJson = function() {
    var passjson = this.template.content;
    passjson['serialNumber'] = this.serialNumber; // append the serial number
    passjson['authenticationToken'] = this.authenticationToken; // append AuthenticationToken
    passjson['webServiceURL'] = conf.webServiceURL; // append the url to the pass.

    var passjsonString = JSON.stringify(passjson, 'utf-8'); // FIXME: reduce file size by remove 2nd & 3rd params. here for beauty print only.

    for (var key in this.fields) {
        var searchKey = MERGE_TAGS_ESCAPE_CHARS + key + MERGE_TAGS_ESCAPE_CHARS;
        // passjsonString = replaceAll(searchKey, this.fields[key], passjsonString);
        passjsonString = passjsonString.split(searchKey).join(this.fields[key]);
    }
    console.log(passjsonString.escapeSpecialChars());
    // console.log(passjsonString.escapeSpecialChars());

    return passjsonString.escapeSpecialChars();
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment