detect-indent
Advanced tools
+54
| #!/usr/bin/env node | ||
| 'use strict'; | ||
| var fs = require('fs'); | ||
| var stdin = require('get-stdin'); | ||
| var argv = require('minimist')(process.argv.slice(2)); | ||
| var pkg = require('./package.json'); | ||
| var detectIndent = require('./'); | ||
| var input = argv._[0]; | ||
| function help() { | ||
| console.log([ | ||
| pkg.description, | ||
| '', | ||
| 'Usage', | ||
| ' $ detect-indent <file>', | ||
| ' $ echo <string> | detect-indent', | ||
| '', | ||
| 'Example', | ||
| ' $ echo \' foo\\n bar\' | detect-indent | wc --chars', | ||
| ' 2' | ||
| ].join('\n')); | ||
| } | ||
| function init(data) { | ||
| var indent = detectIndent(data); | ||
| if (indent !== null) { | ||
| process.stdout.write(indent); | ||
| } else { | ||
| console.error('Indentation could not be detected'); | ||
| process.exit(2); | ||
| } | ||
| } | ||
| if (argv.help) { | ||
| help(); | ||
| return; | ||
| } | ||
| if (argv.version) { | ||
| console.log(pkg.version); | ||
| return; | ||
| } | ||
| if (process.stdin.isTTY) { | ||
| if (!input) { | ||
| help(); | ||
| return; | ||
| } | ||
| init(fs.readFileSync(input, 'utf8')); | ||
| } else { | ||
| stdin(init); | ||
| } |
+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; | ||
| } |
+27
-24
| { | ||
| "name": "detect-indent", | ||
| "version": "0.1.4", | ||
| "version": "0.2.0", | ||
| "description": "Detect the indentation of code", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/detect-indent", | ||
| "bin": { | ||
| "detect-indent": "cli.js" | ||
| }, | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "http://sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "cli.js" | ||
| ], | ||
| "keywords": [ | ||
| "cli", | ||
| "bin", | ||
| "indent", | ||
@@ -18,28 +40,9 @@ "indentation", | ||
| ], | ||
| "license": "MIT", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "http://sindresorhus.com" | ||
| "dependencies": { | ||
| "get-stdin": "^0.1.0", | ||
| "minimist": "^0.1.0" | ||
| }, | ||
| "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": "*" | ||
| } | ||
| } |
+23
-16
@@ -17,27 +17,15 @@ # detect-indent [](https://travis-ci.org/sindresorhus/detect-indent) | ||
| 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 | ||
| ``` | ||
| ## Usage | ||
| ## API | ||
| Accepts a string and returns the indentation or `null` if it can't be detected. | ||
| ## Usage | ||
| Modify a JSON file while persisting the indentation in Node.js. | ||
| ```js | ||
| // modify a JSON file while persisting the indentation in Node | ||
| var fs = require('fs'); | ||
@@ -66,4 +54,23 @@ var detectIndent = require('detect-indent'); | ||
| ## CLI | ||
| ```sh | ||
| $ npm install --global detect-indent | ||
| ``` | ||
| ```sh | ||
| $ detect-indent --help | ||
| Usage | ||
| $ detect-indent <file> | ||
| $ echo <string> | detect-indent | ||
| Example | ||
| $ echo ' foo\n bar' | detect-indent | wc --chars | ||
| 2 | ||
| ``` | ||
| ## License | ||
| [MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) | ||
| MIT © [Sindre Sorhus](http://sindresorhus.com) |
| /*! | ||
| detect-indent | ||
| by Sindre Sorhus | ||
| https://github.com/sindresorhus/detect-indent | ||
| MIT License | ||
| */ | ||
| (function () { | ||
| 'use strict'; | ||
| var RE_MULTILINE_COMMENTS = /\*(.|[\r\n])*?\*/; | ||
| 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 Error('Argument must be a string.'); | ||
| } | ||
| var lines = str.replace(RE_MULTILINE_COMMENTS, '').split(/\n|\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; | ||
| } | ||
| })(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4209
12.21%1
-75%4
33.33%95
55.74%75
10.29%2
Infinity%2
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added