Skip to content

Commit 860ac63

Browse files
committed
Require Node.js 12 and move to ESM
1 parent 704e889 commit 860ac63

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
16-
- 8
1715
steps:
1816
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
17+
- uses: actions/setup-node@v2
2018
with:
2119
node-version: ${{ matrix.node-version }}
2220
- run: npm install

index.d.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
declare namespace indentString {
2-
interface Options {
3-
/**
4-
The string to use for the indent.
5-
6-
@default ' '
7-
*/
8-
readonly indent?: string;
9-
10-
/**
11-
Also indent empty lines.
12-
13-
@default false
14-
*/
15-
readonly includeEmptyLines?: boolean;
16-
}
1+
export interface Options {
2+
/**
3+
The string to use for the indent.
4+
5+
@default ' '
6+
*/
7+
readonly indent?: string;
8+
9+
/**
10+
Also indent empty lines.
11+
12+
@default false
13+
*/
14+
readonly includeEmptyLines?: boolean;
1715
}
1816

1917
/**
@@ -24,7 +22,7 @@ Indent each line in a string.
2422
2523
@example
2624
```
27-
import indentString = require('indent-string');
25+
import indentString from 'indent-string';
2826
2927
indentString('Unicorns\nRainbows', 4);
3028
//=> ' Unicorns\n Rainbows'
@@ -33,10 +31,8 @@ indentString('Unicorns\nRainbows', 4, {indent: '♥'});
3331
//=> '♥♥♥♥Unicorns\n♥♥♥♥Rainbows'
3432
```
3533
*/
36-
declare function indentString(
34+
export default function indentString(
3735
string: string,
3836
count?: number,
39-
options?: indentString.Options
37+
options?: Options
4038
): string;
41-
42-
export = indentString;

index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
'use strict';
2-
3-
module.exports = (string, count = 1, options) => {
4-
options = {
5-
indent: ' ',
6-
includeEmptyLines: false,
7-
...options
8-
};
1+
export default function indentString(string, count = 1, options = {}) {
2+
const {
3+
indent = ' ',
4+
includeEmptyLines = false
5+
} = options;
96

107
if (typeof string !== 'string') {
118
throw new TypeError(
@@ -25,17 +22,17 @@ module.exports = (string, count = 1, options) => {
2522
);
2623
}
2724

28-
if (typeof options.indent !== 'string') {
25+
if (typeof indent !== 'string') {
2926
throw new TypeError(
30-
`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``
27+
`Expected \`options.indent\` to be a \`string\`, got \`${typeof indent}\``
3128
);
3229
}
3330

3431
if (count === 0) {
3532
return string;
3633
}
3734

38-
const regex = options.includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
35+
const regex = includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
3936

40-
return string.replace(regex, options.indent.repeat(count));
41-
};
37+
return string.replace(regex, indent.repeat(count));
38+
}

index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import indentString = require('.');
2+
import indentString from './index.js';
33

44
expectType<string>(indentString('Unicorns\nRainbows'));
55
expectType<string>(indentString('Unicorns\nRainbows', 4));

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
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:
66

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Indent each line in a string",
55
"license": "MIT",
66
"repository": "sindresorhus/indent-string",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=12"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -30,8 +33,8 @@
3033
"every"
3134
],
3235
"devDependencies": {
33-
"ava": "^1.4.1",
34-
"tsd": "^0.7.2",
35-
"xo": "^0.24.0"
36+
"ava": "^3.15.0",
37+
"tsd": "^0.14.0",
38+
"xo": "^0.38.2"
3639
}
3740
}

readme.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
> Indent each line in a string
44
5-
65
## Install
76

87
```
98
$ npm install indent-string
109
```
1110

12-
1311
## Usage
1412

1513
```js
16-
const indentString = require('indent-string');
14+
import indentString from 'indent-string';
1715

1816
indentString('Unicorns\nRainbows', 4);
1917
//=> ' Unicorns\n Rainbows'
@@ -22,7 +20,6 @@ indentString('Unicorns\nRainbows', 4, {indent: '♥'});
2220
//=> '♥♥♥♥Unicorns\n♥♥♥♥Rainbows'
2321
```
2422

25-
2623
## API
2724

2825
### indentString(string, count?, options?)
@@ -35,7 +32,7 @@ The string to indent.
3532

3633
#### count
3734

38-
Type: `number`<br>
35+
Type: `number`\
3936
Default: `1`
4037

4138
How many times you want `options.indent` repeated.
@@ -46,25 +43,23 @@ Type: `object`
4643

4744
##### indent
4845

49-
Type: `string`<br>
46+
Type: `string`\
5047
Default: `' '`
5148

5249
The string to use for the indent.
5350

5451
##### includeEmptyLines
5552

56-
Type: `boolean`<br>
53+
Type: `boolean`\
5754
Default: `false`
5855

5956
Also indent empty lines.
6057

61-
6258
## Related
6359

6460
- [indent-string-cli](https://github.com/sindresorhus/indent-string-cli) - CLI for this module
6561
- [strip-indent](https://github.com/sindresorhus/strip-indent) - Strip leading whitespace from every line in a string
6662

67-
6863
---
6964

7065
<div align="center">

test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
import test from 'ava';
2-
import indentString from '.';
2+
import indentString from './index.js';
33

44
test('throw if input is not a string', t => {
55
t.throws(() => {
66
indentString(5);
7-
}, 'Expected `input` to be a `string`, got `number`');
7+
}, {
8+
message: 'Expected `input` to be a `string`, got `number`'
9+
});
810

911
t.throws(() => {
1012
indentString(true);
11-
}, 'Expected `input` to be a `string`, got `boolean`');
13+
}, {
14+
message: 'Expected `input` to be a `string`, got `boolean`'
15+
});
1216
});
1317

1418
test('throw if count is not a number', t => {
1519
t.throws(() => {
1620
indentString('foo', 'bar');
17-
}, 'Expected `count` to be a `number`, got `string`');
21+
}, {
22+
message: 'Expected `count` to be a `number`, got `string`'
23+
});
1824
});
1925

2026
test('throw if count is a negative', t => {
2127
t.throws(() => {
2228
indentString('foo', -1);
23-
}, 'Expected `count` to be at least 0, got `-1`');
29+
}, {
30+
message: 'Expected `count` to be at least 0, got `-1`'
31+
});
2432
});
2533

2634
test('throw if indent is not a string', t => {
2735
t.throws(() => {
2836
indentString('foo', 1, {indent: 1});
29-
}, 'Expected `options.indent` to be a `string`, got `number`');
37+
}, {
38+
message: 'Expected `options.indent` to be a `string`, got `number`'
39+
});
3040
});
3141

3242
test('indent each line in a string', t => {

0 commit comments

Comments
 (0)