Skip to content

Commit

Permalink
Tweaks (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Sep 29, 2017
1 parent 6c7485c commit c182bbd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sudo: false
language: node_js
node_js:
- '8'
- '6'
- '4'
20 changes: 9 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

// detect either spaces or tabs but not both to properly handle tabs
// for indentation and spaces for alignment
// Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment
const INDENT_RE = /^(?:( )+|\t+)/;

function getMostUsed(indents) {
Expand Down Expand Up @@ -32,15 +31,14 @@ module.exports = str => {
throw new TypeError('Expected a string');
}

// used to see if tabs or spaces are the most used
// Used to see if tabs or spaces are the most used
let tabs = 0;
let spaces = 0;

// remember the size of previous line's indentation
// Remember the size of previous line's indentation
let prev = 0;

// remember how many indents/unindents as occurred for a given size
// and how much lines follow a given indentation
// Remember how many indents/unindents as occurred for a given size and how much lines follow a given indentation
//
// indents = {
// 3: [1, 0],
Expand All @@ -50,15 +48,15 @@ module.exports = str => {
// }
const indents = new Map();

// pointer to the array of last used indent
// Pointer to the array of last used indent
let current;

// whether the last action was an indent (opposed to an unindent)
// Whether the last action was an indent (opposed to an unindent)
let isIndent;

for (const line of str.split(/\n/g)) {
if (!line) {
// ignore empty lines
// Ignore empty lines
continue;
}

Expand All @@ -81,7 +79,7 @@ module.exports = str => {
prev = indent;

if (diff) {
// an indent or unindent has been detected
// An indent or unindent has been detected

isIndent = diff > 0;

Expand All @@ -94,7 +92,7 @@ module.exports = str => {
indents.set(diff, current);
}
} else if (current) {
// if the last action was an indent, increment the weight
// If the last action was an indent, increment the weight
current[1] += Number(isIndent);
}
}
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
}

0 comments on commit c182bbd

Please sign in to comment.