Created
October 17, 2011 20:11
-
-
Save NV/1293653 to your computer and use it in GitHub Desktop.
Guess indent style used in JS file (i.e. tabs, 2 spaces, 4 spaces)
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
var sample = "'use strict';\n\ | |
\n\ | |
/**\n\ | |
* @param {string} selector\n\ | |
* @nosideeffects\n\ | |
* @return {Element|null}\n\ | |
*/\n\ | |
HTMLElement.prototype.up = function(selector) {\n\ | |
var element = this;\n\ | |
while (element = element.parentElement) {\n\ | |
if (element.webkitMatchesSelector(selector)) {\n\ | |
return element;\n\ | |
}\n\ | |
}\n\ | |
return null;\n\ | |
};"; | |
function detectIndentStyle(input) { | |
var r = /^[ |\t]+/gm; | |
var indents = []; | |
var match; | |
var text; | |
while (match = r.exec(input)) { | |
text = match[0]; | |
if (text === '\t') | |
return '\t'; | |
if (text === ' ') | |
// Who the hell uses 1 space indent? | |
continue; | |
return text; | |
} | |
return ''; | |
} | |
console.log(detectIndentStyle(sample) === ' '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment