Skip to content

Instantly share code, notes, and snippets.

@harthur harthur/gcd.js Secret
Last active Mar 4, 2016

Embed
What would you like to do?
GCD indentation detection
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
You can’t perform that action at this time.