Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6800e4f
commit cb6202f
Showing
18 changed files
with
147 additions
and
271 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- '0.10' | ||
- '0.8' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
var RE_MULTILINE_COMMENTS = /\/\*[\S\s]*?\*\//; | ||
var RE_EMPTY_LINE = /^\s*$/; | ||
var RE_LEADING_WHITESPACE = /^[ \t]+/; | ||
|
||
function gcd(a, b) { | ||
return b ? gcd(b, a % b) : a; | ||
} | ||
|
||
module.exports = function (str) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
|
||
var lines = str.replace(RE_MULTILINE_COMMENTS, '').split(/\r?\n/); | ||
var tabs = 0; | ||
var spaces = []; | ||
|
||
for (var i = 0; i < lines.length; i++) { | ||
var line = lines[i]; | ||
|
||
if (RE_EMPTY_LINE.test(line)) { | ||
continue; | ||
} | ||
|
||
var matches = line.match(RE_LEADING_WHITESPACE); | ||
|
||
if (matches) { | ||
var whitespace = matches[0]; | ||
var len = whitespace.length; | ||
|
||
if (whitespace.indexOf('\t') !== -1) { | ||
tabs++; | ||
} | ||
|
||
// convert odd numbers to even numbers | ||
if (len % 2 === 1) { | ||
len += 1; | ||
} | ||
|
||
if (whitespace.indexOf(' ') !== -1) { | ||
spaces.push(len); | ||
} | ||
} | ||
} | ||
|
||
if (tabs > spaces.length) { | ||
return '\t'; | ||
} | ||
|
||
if (spaces.length === 0) { | ||
return null; | ||
} | ||
|
||
// greatest common divisor is most likely the indent size | ||
var indentSize = spaces.reduce(gcd); | ||
|
||
if (indentSize > 0) { | ||
return new Array(indentSize + 1).join(' '); | ||
} | ||
|
||
return null; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var detectIndent = require('./'); | ||
|
||
function getFile(file) { | ||
return fs.readFileSync(path.join(__dirname, file), 'utf8'); | ||
} | ||
|
||
it('should detect the indent of a file with space indent', function () { | ||
assert.equal(detectIndent(getFile('fixture/space.js')), ' '); | ||
}); | ||
|
||
it('should detect the indent of a file with tab indent', function () { | ||
assert.equal(detectIndent(getFile('fixture/tab.js')), '\t'); | ||
}); | ||
|
||
it('should detect the indent of a file with mostly tabs', function () { | ||
assert.equal(detectIndent(getFile('fixture/mixed-tab.js')), '\t'); | ||
}); | ||
|
||
it('should detect the indent of a file with mostly spaces', function () { | ||
assert.equal(detectIndent(getFile('fixture/mixed-space.js')), ' '); | ||
}); | ||
|
||
it('should detect the indent of a weirdly indented vendor prefixed CSS', function () { | ||
assert.equal(detectIndent(getFile('fixture/vendor-prefixed-css.css')), ' '); | ||
}); | ||
|
||
it('should return `null` when there are no indentation', function () { | ||
assert.equal(detectIndent('<ul></ul>'), null); | ||
}); |
Oops, something went wrong.