Skip to content

Instantly share code, notes, and snippets.

@peterflynn
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save peterflynn/f0640225b5315b0f82c5 to your computer and use it in GitHub Desktop.

Select an option

Save peterflynn/f0640225b5315b0f82c5 to your computer and use it in GitHub Desktop.
Brackets scriptlet to anonymize all the strings in a JS file, without changing its length or formatting
function makeFiller(len) {
var str = "";
var i;
for (i = 0; i < len; i++) {
var mod = i % 10;
if (mod) {
str += mod;
} else {
mod = (i / 10) % 10;
str += mod;
}
}
return str;
}
var currentEditor = EditorManager.getActiveEditor();
var doc = currentEditor.document;
doc.batchOperation(function () {
var i;
var stringDelim;
function processCode() {
for (; i < line.length; i++) {
var ch = line[i];
if (ch === '"' || ch === "'") {
i++; // eat the opening quote
return ch;
}
}
}
function processString(delim) {
var stringStart = i;
for (; i < line.length; i++) {
var ch = line[i];
if (ch === "\\") {
i++; // skip whatever's next
} else if (ch === delim) {
// reached end of string literal: replace it with filler
var subst = makeFiller(i - stringStart);
doc.replaceRange(subst, { line: lineI, ch: stringStart }, { line: lineI, ch: i });
i++; // eat the closing quote
return;
}
}
}
var lineI;
for (lineI = 0; lineI < currentEditor.lineCount(); lineI++) {
var line = doc.getLine(lineI);
i = 0;
stringDelim = null;
while (i < line.length) {
if (stringDelim) {
processString(stringDelim);
stringDelim = null;
} else {
stringDelim = processCode();
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment