Skip to content

Instantly share code, notes, and snippets.

@peterflynn
Created November 25, 2014 02:05
Show Gist options
  • Save peterflynn/f214f87bafec5d360ccf to your computer and use it in GitHub Desktop.
Save peterflynn/f214f87bafec5d360ccf to your computer and use it in GitHub Desktop.
Brackets scriptlet to anonymize all the strings in a very large 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;
var contents = doc.getText();
var i = 0;
function processCode() {
for (; i < contents.length; i++) {
var ch = contents[i];
if (ch === '"' || ch === "'") {
i++; // eat the opening quote
return ch;
}
}
}
function processString(delim) {
var stringStart = i;
for (; i < contents.length; i++) {
var ch = contents[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);
contents = contents.substr(0, stringStart) + subst + contents.substr(i);
i++; // eat the closing quote
return;
} else if (ch === "\n") {
throw new Error("Newline inside string literal!", i);
}
}
}
var stringDelim;
while (i < contents.length) {
if (stringDelim) {
processString(stringDelim);
stringDelim = null;
} else {
stringDelim = processCode();
}
}
doc.setText(contents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment