Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #6

Merged
merged 2 commits into from Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
21 changes: 21 additions & 0 deletions index.d.ts
@@ -0,0 +1,21 @@
/**
Strip leading whitespace from each line in a string.

The line with the least number of leading whitespace, ignoring empty lines, determines the number to remove.

@example
```
import stripIndent = require('strip-indent');

const string = '\tunicorn\n\t\tcake';
// unicorn
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issue on VS Code about supporting either /* */ comments unescaped inside codeblocks in doc comments (preferred) or alternatively/additionally support escaped \/* */ comments inside doc comments (meaning it's not displayed as escaped). It's silly that we have to use line comments like this. It makes it harder to read the example output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened an issue.

// cake

stripIndent(string);
//unicorn
// cake
```
*/
declare function stripIndent(string: string): string;

export = stripIndent;
11 changes: 6 additions & 5 deletions index.js
@@ -1,14 +1,15 @@
'use strict';

const minIndent = require('min-indent');

module.exports = str => {
const indent = minIndent(str);
module.exports = string => {
const indent = minIndent(string);

if (indent === 0) {
return str;
return string;
}

const re = new RegExp(`^[ \\t]{${indent}}`, 'gm');
const regExp = new RegExp(`^[ \\t]{${indent}}`, 'gm');

return str.replace(re, '');
return string.replace(regExp, '');
};
4 changes: 4 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,4 @@
import {expectType} from 'tsd';
import stripIndent = require('.');

expectType<string>(stripIndent('\tunicorn\n\t\tcake'));
12 changes: 7 additions & 5 deletions package.json
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"strip",
Expand All @@ -35,7 +36,8 @@
"min-indent": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -19,13 +19,13 @@ $ npm install strip-indent
```js
const stripIndent = require('strip-indent');

const str = '\tunicorn\n\t\tcake';
const string = '\tunicorn\n\t\tcake';
/*
unicorn
cake
*/

stripIndent(str);
stripIndent(string);
/*
unicorn
cake
Expand Down
12 changes: 6 additions & 6 deletions test.js
@@ -1,9 +1,9 @@
import test from 'ava';
import m from '.';
import stripIndent from '.';

test(t => {
t.is(m('\nunicorn\n'), '\nunicorn\n');
t.is(m('\n unicorn\n'), '\nunicorn\n');
t.is(m('\t\t<!doctype html>\n\t\t<html>\n\t\t\t<body>\n\n\n\n\t\t\t\t<h1>Hello world!</h1>\n\t\t\t</body>\n\t\t</html>'), '<!doctype html>\n<html>\n\t<body>\n\n\n\n\t\t<h1>Hello world!</h1>\n\t</body>\n</html>');
t.is(m('\n\t\n\t\tunicorn\n\n\n\n\t\t\tunicorn'), '\n\t\nunicorn\n\n\n\n\tunicorn', 'ignore whitespace only lines');
test('main', t => {
t.is(stripIndent('\nunicorn\n'), '\nunicorn\n');
t.is(stripIndent('\n unicorn\n'), '\nunicorn\n');
t.is(stripIndent('\t\t<!doctype html>\n\t\t<html>\n\t\t\t<body>\n\n\n\n\t\t\t\t<h1>Hello world!</h1>\n\t\t\t</body>\n\t\t</html>'), '<!doctype html>\n<html>\n\t<body>\n\n\n\n\t\t<h1>Hello world!</h1>\n\t</body>\n</html>');
t.is(stripIndent('\n\t\n\t\tunicorn\n\n\n\n\t\t\tunicorn'), '\n\t\nunicorn\n\n\n\n\tunicorn', 'ignore whitespace only lines');
});