first commit

This commit is contained in:
armansansd
2023-07-12 16:31:19 +01:00
commit 3424b1927b
23306 changed files with 2217776 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
#!/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).indent;
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);
}
+119
View File
@@ -0,0 +1,119 @@
'use strict';
var repeating = require('repeating');
// detect either spaces or tabs but not both to properly handle tabs
// for indentation and spaces for alignment
var INDENT_RE = /^(?:( )+|\t+)/;
function getMostUsed(indents) {
var result = 0;
var maxUsed = 0;
var maxWeight = 0;
for (var n in indents) {
var indent = indents[n];
var u = indent[0];
var w = indent[1];
if (u > maxUsed || u === maxUsed && w > maxWeight) {
maxUsed = u;
maxWeight = w;
result = +n;
}
}
return result;
}
module.exports = function (str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string');
}
// used to see if tabs or spaces are the most used
var tabs = 0;
var spaces = 0;
// remember the size of previous line's indentation
var 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],
// }
var indents = {};
// pointer to the array of last used indent
var current;
// whether the last action was an indent (opposed to an unindent)
var isIndent;
str.split(/\n/g).forEach(function (line) {
if (!line) {
// ignore empty lines
return;
}
var indent;
var matches = line.match(INDENT_RE);
if (!matches) {
indent = 0;
} else {
indent = matches[0].length;
if (matches[1]) {
spaces++;
} else {
tabs++;
}
}
var diff = indent - prev;
prev = indent;
if (diff) {
// an indent or unindent has been detected
isIndent = diff > 0;
current = indents[isIndent ? diff : -diff];
if (current) {
current[0]++;
} else {
current = indents[diff] = [1, 0];
}
} else if (current) {
// if the last action was an indent, increment the weight
current[1] += +isIndent;
}
});
var amount = getMostUsed(indents);
var type;
var actual;
if (!amount) {
type = null;
actual = '';
} else if (spaces >= tabs) {
type = 'space';
actual = repeating(' ', amount);
} else {
type = 'tab';
actual = repeating('\t', amount);
}
return {
amount: amount,
type: type,
indent: actual
};
};
+1
View File
@@ -0,0 +1 @@
../repeating/cli.js
+39
View File
@@ -0,0 +1,39 @@
'use strict';
module.exports = function (cb) {
var ret = '';
if (process.stdin.isTTY) {
cb('');
return;
}
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
ret += chunk;
});
process.stdin.on('end', function () {
cb(ret);
});
};
module.exports.buffer = function (cb) {
var ret = [];
var len = 0;
if (process.stdin.isTTY) {
cb(new Buffer(''));
return;
}
process.stdin.on('data', function (chunk) {
ret.push(chunk);
len += chunk.length;
});
process.stdin.on('end', function () {
cb(Buffer.concat(ret, len));
});
};
+67
View File
@@ -0,0 +1,67 @@
{
"_from": "get-stdin@^3.0.0",
"_id": "get-stdin@3.0.2",
"_inBundle": false,
"_integrity": "sha1-wc7SS5A5s43thb3xYeV3E7bdSr4=",
"_location": "/detect-indent/get-stdin",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "get-stdin@^3.0.0",
"name": "get-stdin",
"escapedName": "get-stdin",
"rawSpec": "^3.0.0",
"saveSpec": null,
"fetchSpec": "^3.0.0"
},
"_requiredBy": [
"/detect-indent"
],
"_resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-3.0.2.tgz",
"_shasum": "c1ced24b9039b38ded85bdf161e57713b6dd4abe",
"_spec": "get-stdin@^3.0.0",
"_where": "/srv/http/mmap_sarrebourg/user/themes/basic/node_modules/detect-indent",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/get-stdin/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Easier stdin",
"devDependencies": {
"ava": "0.0.4",
"buffer-equal": "0.0.1"
},
"engines": {
"node": ">=0.10.0"
},
"files": [
"index.js"
],
"homepage": "https://github.com/sindresorhus/get-stdin#readme",
"keywords": [
"std",
"stdin",
"stdio",
"concat",
"buffer",
"stream",
"process",
"stream"
],
"license": "MIT",
"name": "get-stdin",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/get-stdin.git"
},
"scripts": {
"test": "node test.js && echo unicorns | node test2.js"
},
"version": "3.0.2"
}
+44
View File
@@ -0,0 +1,44 @@
# get-stdin [![Build Status](https://travis-ci.org/sindresorhus/get-stdin.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stdin)
> Easier stdin
## Install
```sh
$ npm install --save get-stdin
```
## Usage
```js
// example.js
var stdin = require('get-stdin');
stdin(function (data) {
console.log(data);
//=> unicorns
});
```
```sh
$ echo unicorns | node example.js
unicorns
```
## API
### stdin(callback)
Get `stdin` as a string.
### stdin.buffer(callback)
Get `stdin` as a buffer.
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env node
'use strict';
var pkg = require('./package.json');
var repeating = require('./');
var argv = process.argv.slice(2);
function help() {
console.log([
'',
' ' + pkg.description,
'',
' Usage',
' $ repeating <string> <count>',
'',
' Example',
' $ repeating \'unicorn \' 2',
' unicorn unicorn'
].join('\n'));
}
if (process.argv.indexOf('--help') !== -1) {
help();
return;
}
if (process.argv.indexOf('--version') !== -1) {
console.log(pkg.version);
return;
}
if (!argv[1]) {
console.error('You have to define how many times to repeat the string.');
process.exit(1);
}
console.log(repeating(argv[0], Number(argv[1])));
+24
View File
@@ -0,0 +1,24 @@
'use strict';
var isFinite = require('is-finite');
module.exports = function (str, n) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string as the first argument');
}
if (n < 0 || !isFinite(n)) {
throw new TypeError('Expected a finite positive number');
}
var ret = '';
do {
if (n & 1) {
ret += str;
}
str += str;
} while (n = n >> 1);
return ret;
};
+21
View File
@@ -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.
+74
View File
@@ -0,0 +1,74 @@
{
"_from": "repeating@^1.1.0",
"_id": "repeating@1.1.3",
"_inBundle": false,
"_integrity": "sha1-PUEUIYh3U3SU+X93+Xhfq4EPpKw=",
"_location": "/detect-indent/repeating",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "repeating@^1.1.0",
"name": "repeating",
"escapedName": "repeating",
"rawSpec": "^1.1.0",
"saveSpec": null,
"fetchSpec": "^1.1.0"
},
"_requiredBy": [
"/detect-indent"
],
"_resolved": "https://registry.npmjs.org/repeating/-/repeating-1.1.3.tgz",
"_shasum": "3d4114218877537494f97f77f9785fab810fa4ac",
"_spec": "repeating@^1.1.0",
"_where": "/srv/http/mmap_sarrebourg/user/themes/basic/node_modules/detect-indent",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"bin": {
"repeating": "cli.js"
},
"bugs": {
"url": "https://github.com/sindresorhus/repeating/issues"
},
"bundleDependencies": false,
"dependencies": {
"is-finite": "^1.0.0"
},
"deprecated": false,
"description": "Repeat a string - fast",
"devDependencies": {
"ava": "0.0.4"
},
"engines": {
"node": ">=0.10.0"
},
"files": [
"index.js",
"cli.js"
],
"homepage": "https://github.com/sindresorhus/repeating#readme",
"keywords": [
"cli-app",
"cli",
"bin",
"repeat",
"repeating",
"string",
"str",
"text",
"fill"
],
"license": "MIT",
"name": "repeating",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/repeating.git"
},
"scripts": {
"test": "node test.js"
},
"version": "1.1.3"
}
+40
View File
@@ -0,0 +1,40 @@
# repeating [![Build Status](https://travis-ci.org/sindresorhus/repeating.svg?branch=master)](https://travis-ci.org/sindresorhus/repeating)
> Repeat a string - fast
## Usage
```sh
$ npm install --save repeating
```
```js
var repeating = require('repeating');
repeating('unicorn ', 100);
//=> unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn unicorn
```
## CLI
```sh
$ npm install --global repeating
```
```
$ repeating --help
Usage
repeating <string> <count>
Example
repeating 'unicorn ' 2
unicorn unicorn
```
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
+82
View File
@@ -0,0 +1,82 @@
{
"_from": "detect-indent@3.0.0",
"_id": "detect-indent@3.0.0",
"_inBundle": false,
"_integrity": "sha1-ehLEthvk68FwzW1BbOQ3VBJGN9g=",
"_location": "/detect-indent",
"_phantomChildren": {
"is-finite": "1.0.2"
},
"_requested": {
"type": "version",
"registry": true,
"raw": "detect-indent@3.0.0",
"name": "detect-indent",
"escapedName": "detect-indent",
"rawSpec": "3.0.0",
"saveSpec": null,
"fetchSpec": "3.0.0"
},
"_requiredBy": [
"/6to5-core"
],
"_resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-3.0.0.tgz",
"_shasum": "7a12c4b61be4ebc170cd6d416ce43754124637d8",
"_spec": "detect-indent@3.0.0",
"_where": "/srv/http/mmap_sarrebourg/user/themes/basic/node_modules/6to5-core",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"bin": {
"detect-indent": "cli.js"
},
"bugs": {
"url": "https://github.com/sindresorhus/detect-indent/issues"
},
"bundleDependencies": false,
"dependencies": {
"get-stdin": "^3.0.0",
"minimist": "^1.1.0",
"repeating": "^1.1.0"
},
"deprecated": false,
"description": "Detect the indentation of code",
"devDependencies": {
"mocha": "*"
},
"engines": {
"node": ">=0.10.0"
},
"files": [
"index.js",
"cli.js"
],
"homepage": "https://github.com/sindresorhus/detect-indent#readme",
"keywords": [
"cli",
"bin",
"indent",
"indentation",
"detect",
"infer",
"identify",
"code",
"string",
"text",
"source",
"space",
"tab"
],
"license": "MIT",
"name": "detect-indent",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/detect-indent.git"
},
"scripts": {
"test": "mocha"
},
"version": "3.0.0"
}
+87
View File
@@ -0,0 +1,87 @@
# 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
```sh
$ npm install --save detect-indent
```
## Usage
```js
// modify a JSON file while persisting the indentation in Node
var fs = require('fs');
var detectIndent = require('detect-indent');
/*
{
"ilove": "pizza"
}
*/
var file = fs.readFileSync('foo.json', 'utf8');
// tries to detect the indentation and falls back to a default if it can't
var indent = detectIndent(file).indent || ' ';
var 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} the amount of indentation, e.g. `2`
* `type`: {String|Null} the type of indentation. Possible values are `tab`, `space` or `null` if no indentation is detected
* `indent`: {String} the actual indentation
## CLI
```sh
$ npm install --global detect-indent
```
```
$ detect-indent --help
Usage
detect-indent <file>
echo <string> | detect-indent
Example
echo ' foo\n bar' | detect-indent | wc --chars
2
```
## Algorithm
Look for the most common difference between two consecutive non-empty
lines.
[Source](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918).
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)