diff --git a/.editorconfig b/.editorconfig index b0d7fd9..8311fe1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,5 +8,9 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +[package.json] +indent_style = space +indent_size = 2 + [*.md] trim_trailing_whitespace = false diff --git a/.jshintrc b/.jshintrc index ac35c13..804f8af 100644 --- a/.jshintrc +++ b/.jshintrc @@ -4,17 +4,10 @@ "bitwise": true, "camelcase": true, "curly": true, - "eqeqeq": true, "immed": true, - "indent": 4, - "latedef": true, "newcap": true, "noarg": true, - "quotmark": "single", - "regexp": true, "undef": true, - "unused": true, - "strict": true, - "trailing": true, - "smarttabs": true + "unused": "vars", + "strict": true } diff --git a/.travis.yml b/.travis.yml index 2cdacaf..244b7e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ language: node_js node_js: - '0.10' - - '0.8' diff --git a/bower.json b/bower.json deleted file mode 100644 index 9ba090f..0000000 --- a/bower.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "detect-indent", - "version": "0.1.3", - "description": "Detect the indentation of code", - "license": "MIT", - "main": "detect-indent.js", - "authors": [{ - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "http://sindresorhus.com" - }], - "keywords": [ - "indent", - "indentation", - "detect", - "infer", - "identify", - "code", - "string", - "text", - "source", - "space", - "tab" - ], - "ignore": [ - "test", - ".*", - "component.json", - "package.json" - ] -} diff --git a/component.json b/component.json deleted file mode 100644 index 2194b1d..0000000 --- a/component.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "detect-indent", - "version": "0.1.4", - "description": "Detect the indentation of code", - "repository": "sindresorhus/detect-indent", - "keywords": [ - "indent", - "indentation", - "detect", - "infer", - "identify", - "code", - "string", - "text", - "source", - "space", - "tab" - ], - "main": "detect-indent.js", - "scripts": ["detect-indent.js"], - "license": "MIT" -} diff --git a/detect-indent.js b/detect-indent.js deleted file mode 100644 index e929bbc..0000000 --- a/detect-indent.js +++ /dev/null @@ -1,77 +0,0 @@ -/*! - detect-indent - by Sindre Sorhus - https://github.com/sindresorhus/detect-indent - MIT License -*/ -(function () { - '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; - } - - function detectIndent(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; - } - - if (typeof module !== 'undefined' && module.exports) { - module.exports = detectIndent; - } else { - window.detectIndent = detectIndent; - } -})(); diff --git a/test/fixture/mixed-space.js b/fixture/mixed-space.js similarity index 100% rename from test/fixture/mixed-space.js rename to fixture/mixed-space.js diff --git a/test/fixture/mixed-tab.js b/fixture/mixed-tab.js similarity index 100% rename from test/fixture/mixed-tab.js rename to fixture/mixed-tab.js diff --git a/test/fixture/space.js b/fixture/space.js similarity index 100% rename from test/fixture/space.js rename to fixture/space.js diff --git a/test/fixture/tab.js b/fixture/tab.js similarity index 100% rename from test/fixture/tab.js rename to fixture/tab.js diff --git a/test/fixture/vendor-prefixed-css.css b/fixture/vendor-prefixed-css.css similarity index 100% rename from test/fixture/vendor-prefixed-css.css rename to fixture/vendor-prefixed-css.css diff --git a/index.js b/index.js new file mode 100644 index 0000000..480a91f --- /dev/null +++ b/index.js @@ -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; +} diff --git a/license b/license new file mode 100644 index 0000000..654d0bf --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus (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. diff --git a/package.json b/package.json index 84b3b19..67451f1 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,22 @@ "name": "detect-indent", "version": "0.1.4", "description": "Detect the indentation of code", + "license": "MIT", + "repository": "sindresorhus/detect-indent", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "http://sindresorhus.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "mocha" + }, + "files": [ + "index.js" + ], "keywords": [ "indent", "indentation", @@ -15,30 +31,7 @@ "space", "tab" ], - "license": "MIT", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "http://sindresorhus.com" - }, - "files": [ - "detect-indent.js" - ], - "main": "detect-indent", - "repository": { - "type": "git", - "url": "git://github.com/sindresorhus/detect-indent.git" - }, - "scripts": { - "test": "mocha && phantomjs node_modules/mocha-phantomjs/lib/mocha-phantomjs.coffee test/test.html" - }, "devDependencies": { - "mocha": "~1.12.0", - "chai": "~1.7.2", - "phantomjs": "~1.9.1", - "mocha-phantomjs": "~3.1.0" - }, - "engines": { - "node": ">=0.8.0" + "mocha": "*" } } diff --git a/readme.md b/readme.md index 608b60e..debcc8f 100644 --- a/readme.md +++ b/readme.md @@ -14,25 +14,10 @@ Pass in a string of any kind of text and get the indentation. ## Install -Download [manually](https://github.com/sindresorhus/detect-indent/releases) or with a package-manager. - -```bash +```sh $ npm install --save detect-indent ``` -```bash -$ bower install --save detect-indent -``` - -```bash -$ component install sindresorhus/detect-indent -``` - - -## API - -Accepts a string and returns the indentation or `null` if it can't be detected. - ## Usage @@ -62,6 +47,11 @@ fs.writeFileSync('foo.json', JSON.stringify(json, null, indent)); ``` +## API + +Accepts a string and returns the indentation or `null` if it can't be detected. + + ## License -[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) +MIT © [Sindre Sorhus](http://sindresorhus.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..24e0fdf --- /dev/null +++ b/test.js @@ -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(''), null); +}); diff --git a/test/test.html b/test/test.html deleted file mode 100644 index 12a9c6f..0000000 --- a/test/test.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - Tests - - -
- - - - - - - - diff --git a/test/test.js b/test/test.js deleted file mode 100644 index e74c770..0000000 --- a/test/test.js +++ /dev/null @@ -1,63 +0,0 @@ -/*global describe, it */ -'use strict'; -var isBrowser = typeof window !== 'undefined'; -var readFileSync; - -if (isBrowser) { - var xhrSync = (function () { - /*global XMLHttpRequest */ - var xhr = new XMLHttpRequest(); - return function(method, url, callback) { - xhr.onreadystatechange = function () { - if (xhr.readyState === 4) { - callback(xhr.responseText); - } - }; - xhr.open(method, url, false); - xhr.send(); - }; - })(); - - readFileSync = function (file) { - var ret; - xhrSync('get', file, function (data) { - ret = data; - }); - return ret; - } -} else { - var fs = require('fs'); - var path = require('path'); - var assert = require('chai').assert; - var detectIndent = require('../detect-indent'); - - readFileSync = function (file) { - return fs.readFileSync(path.join(__dirname, file), 'utf8'); - }; -} - -describe('detectIndent()', function () { - it('should detect the indent of a file with space indent', function () { - assert.equal(detectIndent(readFileSync('fixture/space.js')), ' '); - }); - - it('should detect the indent of a file with tab indent', function () { - assert.equal(detectIndent(readFileSync('fixture/tab.js')), '\t'); - }); - - it('should detect the indent of a file with mostly tabs', function () { - assert.equal(detectIndent(readFileSync('fixture/mixed-tab.js')), '\t'); - }); - - it('should detect the indent of a file with mostly spaces', function () { - assert.equal(detectIndent(readFileSync('fixture/mixed-space.js')), ' '); - }); - - it('should detect the indent of a weirdly indented vendor prefixed CSS', function () { - assert.equal(detectIndent(readFileSync('fixture/vendor-prefixed-css.css')), ' '); - }); - - it('should return `null` when there are no indentation', function () { - assert.equal(detectIndent(''), null); - }); -});