-
-
Save harthur/ffd3477b141086667c40 to your computer and use it in GitHub Desktop.
GCD indentation detection
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 detectIndent(lines) { | |
var widths = []; | |
lines.forEach(function (text) { | |
var width = leadingSpaces(text); | |
// throw out odd numbers so comments don't throw us | |
if (width % 2 == 1) { | |
return; | |
} | |
widths.push(width); | |
}); | |
// now get the largest width that divides all of them | |
var indent = widths.reduce(gcd); | |
return indent; | |
} | |
/* Greatest common divisor of two numbers */ | |
function gcd(n, m) { | |
return m > 0 ? gcd(m, n % m) : n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment