Skip to content

Instantly share code, notes, and snippets.

@NV
Created August 24, 2016 23:42
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 NV/7bde31e6abd1cbc7cf424437399c5285 to your computer and use it in GitHub Desktop.
Save NV/7bde31e6abd1cbc7cf424437399c5285 to your computer and use it in GitHub Desktop.
isLikelyMinified.js
function isLikelyMinified(content) {
let whiteSpaceCount = 0;
let ratio = 0;
let i = 0;
let size = Math.min(5000, content.length);
for (; i < size; i++) {
let char = content[i];
if (char === " ")
whiteSpaceCount++;
else if (char === "\t")
whiteSpaceCount += 4;
else if (char === "\n")
whiteSpaceCount += 8;
if (i >= 500) {
ratio = whiteSpaceCount / i;
if (ratio < 0.05)
return true;
}
}
ratio = whiteSpaceCount / i;
return ratio < 0.1;
}
isLikelyMinified("rastarstrastarstrast");
isLikelyMinified("a \na \nc");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment