This Gist was automatically created by Carbide, a free online programming environment.
Created
August 24, 2016 23:42
-
-
Save NV/7bde31e6abd1cbc7cf424437399c5285 to your computer and use it in GitHub Desktop.
isLikelyMinified.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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