Skip to content

Instantly share code, notes, and snippets.

@NV
Created October 17, 2011 20:11
Show Gist options
  • Save NV/1293653 to your computer and use it in GitHub Desktop.
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)
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