From cdc2f60887f7054cd04029b54786395b636e540b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 5 May 2017 19:47:09 -0700 Subject: detect-indent@5.0.0 --- node_modules/detect-indent/index.js | 122 ++++++++++++++++++++++++++++++++ node_modules/detect-indent/license | 21 ++++++ node_modules/detect-indent/package.json | 78 ++++++++++++++++++++ node_modules/detect-indent/readme.md | 111 +++++++++++++++++++++++++++++ 4 files changed, 332 insertions(+) create mode 100644 node_modules/detect-indent/index.js create mode 100644 node_modules/detect-indent/license create mode 100644 node_modules/detect-indent/package.json create mode 100644 node_modules/detect-indent/readme.md (limited to 'node_modules') diff --git a/node_modules/detect-indent/index.js b/node_modules/detect-indent/index.js new file mode 100644 index 000000000..eda0b1b1a --- /dev/null +++ b/node_modules/detect-indent/index.js @@ -0,0 +1,122 @@ +'use strict'; + +// 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) { + let result = 0; + let maxUsed = 0; + let maxWeight = 0; + + for (const entry of indents) { + // TODO: use destructuring when targeting Node.js 6 + const key = entry[0]; + const val = entry[1]; + + const u = val[0]; + const w = val[1]; + + if (u > maxUsed || (u === maxUsed && w > maxWeight)) { + maxUsed = u; + maxWeight = w; + result = Number(key); + } + } + + return result; +} + +module.exports = str => { + if (typeof str !== 'string') { + throw new TypeError('Expected a string'); + } + + // 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 + let prev = 0; + + // remember how many indents/unindents as occurred for a given size + // and how much lines follow a given indentation + // + // indents = { + // 3: [1, 0], + // 4: [1, 5], + // 5: [1, 0], + // 12: [1, 0], + // } + const indents = new Map(); + + // pointer to the array of last used indent + let current; + + // 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 + continue; + } + + let indent; + const matches = line.match(INDENT_RE); + + if (matches) { + indent = matches[0].length; + + if (matches[1]) { + spaces++; + } else { + tabs++; + } + } else { + indent = 0; + } + + const diff = indent - prev; + prev = indent; + + if (diff) { + // an indent or unindent has been detected + + isIndent = diff > 0; + + current = indents.get(isIndent ? diff : -diff); + + if (current) { + current[0]++; + } else { + current = [1, 0]; + indents.set(diff, current); + } + } else if (current) { + // if the last action was an indent, increment the weight + current[1] += Number(isIndent); + } + } + + const amount = getMostUsed(indents); + + let type; + let indent; + if (!amount) { + type = null; + indent = ''; + } else if (spaces >= tabs) { + type = 'space'; + indent = ' '.repeat(amount); + } else { + type = 'tab'; + indent = '\t'.repeat(amount); + } + + return { + amount, + type, + indent + }; +}; diff --git a/node_modules/detect-indent/license b/node_modules/detect-indent/license new file mode 100644 index 000000000..654d0bfe9 --- /dev/null +++ b/node_modules/detect-indent/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/node_modules/detect-indent/package.json b/node_modules/detect-indent/package.json new file mode 100644 index 000000000..0477e31bc --- /dev/null +++ b/node_modules/detect-indent/package.json @@ -0,0 +1,78 @@ +{ + "_from": "detect-indent", + "_id": "detect-indent@5.0.0", + "_integrity": "sha1-OHHMCmoALow+Wzz38zYmRnXwa50=", + "_location": "/detect-indent", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "detect-indent", + "name": "detect-indent", + "escapedName": "detect-indent", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-5.0.0.tgz", + "_shasum": "3871cc0a6a002e8c3e5b3cf7f336264675f06b9d", + "_shrinkwrap": null, + "_spec": "detect-indent", + "_where": "/Users/zkat/Documents/code/npm", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "bin": null, + "bugs": { + "url": "https://github.com/sindresorhus/detect-indent/issues" + }, + "bundleDependencies": false, + "dependencies": {}, + "deprecated": false, + "description": "Detect the indentation of code", + "devDependencies": { + "ava": "*", + "xo": "*" + }, + "engines": { + "node": ">=4" + }, + "files": [ + "index.js" + ], + "homepage": "https://github.com/sindresorhus/detect-indent#readme", + "keywords": [ + "indent", + "indentation", + "detect", + "infer", + "identify", + "code", + "string", + "text", + "source", + "space", + "tab" + ], + "license": "MIT", + "name": "detect-indent", + "optionalDependencies": {}, + "peerDependencies": {}, + "repository": { + "type": "git", + "url": "git+https://github.com/sindresorhus/detect-indent.git" + }, + "scripts": { + "test": "xo && ava" + }, + "version": "5.0.0", + "xo": { + "esnext": true + } +} diff --git a/node_modules/detect-indent/readme.md b/node_modules/detect-indent/readme.md new file mode 100644 index 000000000..3aeb1badf --- /dev/null +++ b/node_modules/detect-indent/readme.md @@ -0,0 +1,111 @@ +# detect-indent [![Build Status](https://travis-ci.org/sindresorhus/detect-indent.svg?branch=master)](https://travis-ci.org/sindresorhus/detect-indent) + +> Detect the indentation of code + +Pass in a string of any kind of text and get the indentation. + + +## Use cases + +- Persisting the indentation when modifying a file. +- Have new content match the existing indentation. +- Setting the right indentation in your editor. + + +## Install + +``` +$ npm install --save detect-indent +``` + + +## Usage + +Here we modify a JSON file while persisting the indentation: + +```js +const fs = require('fs'); +const detectIndent = require('detect-indent'); + +/* +{ + "ilove": "pizza" +} +*/ +const file = fs.readFileSync('foo.json', 'utf8'); + +// tries to detect the indentation and falls back to a default if it can't +const indent = detectIndent(file).indent || ' '; + +const json = JSON.parse(file); + +json.ilove = 'unicorns'; + +fs.writeFileSync('foo.json', JSON.stringify(json, null, indent)); +/* +{ + "ilove": "unicorns" +} +*/ +``` + + +## API + +Accepts a string and returns an object with stats about the indentation: + +* `amount` {number} - Amount of indentation, for example `2` +* `type` {string|null} - Type of indentation. Possible values are `tab`, `space` or `null` if no indentation is detected +* `indent` {string} - Actual indentation + + +## Algorithm + +The current algorithm looks for the most common difference between two consecutive non-empty lines. + +In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation: + +```css +html { + box-sizing: border-box; +} + +body { + background: gray; +} + +p { + line-height: 1.3em; + margin-top: 1em; + text-indent: 2em; +} +``` + +[Source.](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918) + +Furthermore, if there are more than one most used difference, the indentation with the most lines is selected. + +In the following example, the indentation is detected as 4-spaces: + +```css +body { + background: gray; +} + +p { + line-height: 1.3em; + margin-top: 1em; + text-indent: 2em; +} +``` + + +## Related + +- [detect-indent-cli](https://github.com/sindresorhus/detect-indent-cli) - CLI for this module +- [detect-newline](https://github.com/sindresorhus/detect-newline) - Detect the dominant newline character of a string + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) -- cgit v1.2.3