Skip to content

Instantly share code, notes, and snippets.

@harthur

harthur/gcd.js Secret

Last active March 4, 2016 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harthur/ffd3477b141086667c40 to your computer and use it in GitHub Desktop.
Save harthur/ffd3477b141086667c40 to your computer and use it in GitHub Desktop.
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