[Pkg-javascript-commits] [node-detect-indent] 01/03: Import Upstream version 5.0.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Fri Dec 2 14:53:21 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-detect-indent.

commit f91a7ae6cd3144fe236405e38960f372b66aeb22
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Fri Dec 2 14:41:10 2016 +0000

    Import Upstream version 5.0.0
---
 .editorconfig                   |  12 ++++
 .gitattributes                  |   2 +
 .gitignore                      |   1 +
 .travis.yml                     |   5 ++
 fixture/mixed-space.js          |  31 ++++++++++
 fixture/mixed-tab.js            |  31 ++++++++++
 fixture/space.js                |  31 ++++++++++
 fixture/tab-four.js             |   2 +
 fixture/tab.js                  |  31 ++++++++++
 fixture/vendor-prefixed-css.css |  10 ++++
 index.js                        | 122 ++++++++++++++++++++++++++++++++++++++++
 license                         |  21 +++++++
 package.json                    |  41 ++++++++++++++
 readme.md                       | 111 ++++++++++++++++++++++++++++++++++++
 test.js                         |  97 ++++++++++++++++++++++++++++++++
 15 files changed, 548 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..98a761d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..391f0a4
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+* text=auto
+*.js text eol=lf
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b18bae5
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+sudo: false
+language: node_js
+node_js:
+  - '6'
+  - '4'
diff --git a/fixture/mixed-space.js b/fixture/mixed-space.js
new file mode 100644
index 0000000..e1d82d8
--- /dev/null
+++ b/fixture/mixed-space.js
@@ -0,0 +1,31 @@
+// devtools-detect
+// by Sindre Sorhus
+// MIT License
+(function (window) {
+    /*global CustomEvent */
+    'use strict';
+    var devtools = window.devtools = { open: false };
+    var threshold = 150;
+    var emitEvent = function (state) {
+        window.dispatchEvent(new CustomEvent('devtoolschange', {
+            detail: {
+                open: state
+            }
+        }));
+    };
+
+    setInterval(function () {
+        if (window.outerWidth - window.innerWidth > threshold ||
+            window.outerHeight - window.innerHeight > threshold) {
+			if (!devtools.open) {
+                emitEvent(true);
+			}
+			devtools.open = true;
+        } else {
+			if (devtools.open) {
+				emitEvent(false);
+			}
+			devtools.open = false;
+        }
+    }, 500);
+})(window);
diff --git a/fixture/mixed-tab.js b/fixture/mixed-tab.js
new file mode 100644
index 0000000..28816a6
--- /dev/null
+++ b/fixture/mixed-tab.js
@@ -0,0 +1,31 @@
+// devtools-detect
+// by Sindre Sorhus
+// MIT License
+(function (window) {
+    /*global CustomEvent */
+    'use strict';
+  var devtools = window.devtools = { open: false };
+  var threshold = 150;
+  var emitEvent = function (state) {
+		window.dispatchEvent(new CustomEvent('devtoolschange', {
+			detail: {
+				open: state
+			}
+		}));
+    };
+
+    setInterval(function () {
+		if (window.outerWidth - window.innerWidth > threshold ||
+			window.outerHeight - window.innerHeight > threshold) {
+			if (!devtools.open) {
+                emitEvent(true);
+			}
+			devtools.open = true;
+        } else {
+			if (devtools.open) {
+				emitEvent(false);
+			}
+			devtools.open = false;
+		}
+	}, 500);
+})(window);
diff --git a/fixture/space.js b/fixture/space.js
new file mode 100644
index 0000000..5394d6e
--- /dev/null
+++ b/fixture/space.js
@@ -0,0 +1,31 @@
+// devtools-detect
+// by Sindre Sorhus
+// MIT License
+(function (window) {
+    /*global CustomEvent */
+    'use strict';
+    var devtools = window.devtools = { open: false };
+    var threshold = 150;
+    var emitEvent = function (state) {
+        window.dispatchEvent(new CustomEvent('devtoolschange', {
+            detail: {
+                open: state
+            }
+        }));
+    };
+
+    setInterval(function () {
+        if (window.outerWidth - window.innerWidth > threshold ||
+            window.outerHeight - window.innerHeight > threshold) {
+            if (!devtools.open) {
+                emitEvent(true);
+            }
+            devtools.open = true;
+        } else {
+            if (devtools.open) {
+                emitEvent(false);
+            }
+            devtools.open = false;
+        }
+    }, 500);
+})(window);
diff --git a/fixture/tab-four.js b/fixture/tab-four.js
new file mode 100644
index 0000000..4e47b37
--- /dev/null
+++ b/fixture/tab-four.js
@@ -0,0 +1,2 @@
+				* four
+				* tabs
diff --git a/fixture/tab.js b/fixture/tab.js
new file mode 100644
index 0000000..fff08ac
--- /dev/null
+++ b/fixture/tab.js
@@ -0,0 +1,31 @@
+// devtools-detect
+// by Sindre Sorhus
+// MIT License
+(function (window) {
+	/*global CustomEvent */
+	'use strict';
+	var devtools = window.devtools = { open: false };
+	var threshold = 150;
+	var emitEvent = function (state) {
+		window.dispatchEvent(new CustomEvent('devtoolschange', {
+			detail: {
+				open: state
+			}
+		}));
+	};
+
+	setInterval(function () {
+		if (window.outerWidth - window.innerWidth > threshold ||
+			window.outerHeight - window.innerHeight > threshold) {
+			if (!devtools.open) {
+				emitEvent(true);
+			}
+			devtools.open = true;
+		} else {
+			if (devtools.open) {
+				emitEvent(false);
+			}
+			devtools.open = false;
+		}
+	}, 500);
+})(window);
diff --git a/fixture/vendor-prefixed-css.css b/fixture/vendor-prefixed-css.css
new file mode 100644
index 0000000..44f35fb
--- /dev/null
+++ b/fixture/vendor-prefixed-css.css
@@ -0,0 +1,10 @@
+#leaflet {
+    position: absolute;
+    top: 0;
+    right: 0;
+    bottom: 0;
+    left: 0;
+    -webkit-box-shadow: inset -5px -5px 5px rgba(0, 0, 0, 0.6);
+       -moz-box-shadow: inset -5px -5px 5px rgba(0, 0, 0, 0.6);
+            box-shadow: inset -5px -5px 5px rgba(0, 0, 0, 0.6);
+}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..eda0b1b
--- /dev/null
+++ b/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/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 at 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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..81c7edf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,41 @@
+{
+  "name": "detect-indent",
+  "version": "5.0.0",
+  "description": "Detect the indentation of code",
+  "license": "MIT",
+  "repository": "sindresorhus/detect-indent",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=4"
+  },
+  "scripts": {
+    "test": "xo && ava"
+  },
+  "files": [
+    "index.js"
+  ],
+  "keywords": [
+    "indent",
+    "indentation",
+    "detect",
+    "infer",
+    "identify",
+    "code",
+    "string",
+    "text",
+    "source",
+    "space",
+    "tab"
+  ],
+  "devDependencies": {
+    "ava": "*",
+    "xo": "*"
+  },
+  "xo": {
+    "esnext": true
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..3aeb1ba
--- /dev/null
+++ b/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)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..371fb22
--- /dev/null
+++ b/test.js
@@ -0,0 +1,97 @@
+import fs from 'fs';
+import path from 'path';
+import test from 'ava';
+import m from './';
+
+function getFile(file) {
+	return fs.readFileSync(path.join(__dirname, file), 'utf8');
+}
+
+test('detect the indent of a file with space indent', t => {
+	t.is(m(getFile('fixture/space.js')).indent, '    ');
+});
+
+test('return indentation stats for spaces', t => {
+	const stats = m(getFile('fixture/space.js'));
+	t.deepEqual(stats, {
+		amount: 4,
+		indent: '    ',
+		type: 'space'
+	});
+});
+
+test('return indentation stats for multiple tabs', t => {
+	const stats = m(getFile('fixture/tab-four.js'));
+	t.deepEqual(stats, {
+		amount: 4,
+		indent: '\t\t\t\t',
+		type: 'tab'
+	});
+});
+
+test('detect the indent of a file with tab indent', t => {
+	t.is(m(getFile('fixture/tab.js')).indent, '\t');
+});
+
+test('return indentation stats for tabs', t => {
+	const stats = m(getFile('fixture/tab.js'));
+	t.deepEqual(stats, {
+		amount: 1,
+		indent: '\t',
+		type: 'tab'
+	});
+});
+
+test('detect the indent of a file with equal tabs and spaces', t => {
+	t.is(m(getFile('fixture/mixed-tab.js')).indent, '\t');
+});
+
+test('return indentation stats for equal tabs and spaces', t => {
+	const indent = m(getFile('fixture/mixed-tab.js'));
+	t.deepEqual(indent, {
+		amount: 1,
+		indent: '\t',
+		type: 'tab'
+	});
+});
+
+test('detect the indent of a file with mostly spaces', t => {
+	const stats = m(getFile('fixture/mixed-space.js'));
+	t.is(stats.indent, '    ');
+});
+
+test('return indentation stats for mostly spaces', t => {
+	const stats = m(getFile('fixture/mixed-space.js'));
+	t.deepEqual(stats, {
+		amount: 4,
+		indent: '    ',
+		type: 'space'
+	});
+});
+
+test('detect the indent of a weirdly indented vendor prefixed CSS', t => {
+	const stats = m(getFile('fixture/vendor-prefixed-css.css'));
+	t.is(stats.indent, '    ');
+});
+
+test('return indentation stats for various spaces', t => {
+	const stats = m(getFile('fixture/vendor-prefixed-css.css'));
+	t.deepEqual(stats, {
+		amount: 4,
+		indent: '    ',
+		type: 'space'
+	});
+});
+
+test('return `0` when there is no indentation', t => {
+	t.is(m('<ul></ul>').amount, 0);
+});
+
+test('return indentation stats for no indentation', t => {
+	const stats = m('<ul></ul>');
+	t.deepEqual(stats, {
+		amount: 0,
+		indent: '',
+		type: null
+	});
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-detect-indent.git



More information about the Pkg-javascript-commits mailing list