Skip to content

Commit

Permalink
obliterate junk
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 16, 2014
1 parent 6800e4f commit cb6202f
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 271 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Expand Up @@ -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
11 changes: 2 additions & 9 deletions .jshintrc
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,4 +1,3 @@
language: node_js
node_js:
- '0.10'
- '0.8'
31 changes: 0 additions & 31 deletions bower.json

This file was deleted.

22 changes: 0 additions & 22 deletions component.json

This file was deleted.

77 changes: 0 additions & 77 deletions detect-indent.js

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions 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;
}
21 changes: 21 additions & 0 deletions license
@@ -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.
41 changes: 17 additions & 24 deletions package.json
Expand Up @@ -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",
Expand All @@ -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": "*"
}
}
24 changes: 7 additions & 17 deletions readme.md
Expand Up @@ -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

Expand Down Expand Up @@ -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)
33 changes: 33 additions & 0 deletions 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('<ul></ul>'), null);
});

0 comments on commit cb6202f

Please sign in to comment.