Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 17, 2019
1 parent 767c465 commit 578f3a7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 22 deletions.
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
// cake
stripIndent(string);
//unicorn
// cake
```
*/
declare function stripIndent(string: string): string;

export = stripIndent;
10 changes: 5 additions & 5 deletions index.js
@@ -1,14 +1,14 @@
'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');
});

0 comments on commit 578f3a7

Please sign in to comment.