Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 9, 2021
1 parent f0e8509 commit 5312b08
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 58 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
63 changes: 28 additions & 35 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
declare const stop: unique symbol;

declare namespace pEachSeries {
type StopSymbol = typeof stop;
}
export type StopSymbol = typeof stop;

declare const pEachSeries: {
/**
Stop iterating through items by returning `pEachSeries.stop` from the iterator function.
@example
```
import pEachSeries from 'p-each-series';
// Logs `a` and `b`.
const result = await pEachSeries(['a', 'b', 'c'], value => {
console.log(value);
if (value === 'b') {
return pEachSeries.stop;
}
});
console.log(result);
//=> ['a', 'b', 'c']
```
*/
readonly stop: StopSymbol;

/**
Iterate over promises serially.
Expand All @@ -14,7 +34,7 @@ declare const pEachSeries: {
@example
```
import pEachSeries = require('p-each-series');
import pEachSeries from 'p-each-series';
const keywords = [
getTopKeyword(), //=> Promise
Expand All @@ -24,41 +44,14 @@ declare const pEachSeries: {
const iterator = async element => saveToDiskPromise(element);
(async () => {
console.log(await pEachSeries(keywords, iterator));
//=> ['unicorn', 'rainbow', 'pony']
})();
console.log(await pEachSeries(keywords, iterator));
//=> ['unicorn', 'rainbow', 'pony']
```
*/
<ValueType>(
input: Iterable<PromiseLike<ValueType> | ValueType>,
iterator: (element: ValueType, index: number) => pEachSeries.StopSymbol | unknown
iterator: (element: ValueType, index: number) => StopSymbol | unknown
): Promise<ValueType[]>;

/**
Stop iterating through items by returning `pEachSeries.stop` from the iterator function.
@example
```
const pEachSeries = require('p-each-series');
// Logs `a` and `b`.
const result = await pEachSeries(['a', 'b', 'c'], value => {
console.log(value);
if (value === 'b') {
return pEachSeries.stop;
}
});
console.log(result);
//=> ['a', 'b', 'c']
```
*/
readonly stop: pEachSeries.StopSymbol;

// TODO: Remove this for the next major release
default: typeof pEachSeries;
};

export = pEachSeries;
export default pEachSeries;
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const pEachSeries = async (iterable, iterator) => {
let index = 0;

Expand All @@ -17,6 +15,4 @@ const pEachSeries = async (iterable, iterator) => {

pEachSeries.stop = Symbol('pEachSeries.stop');

module.exports = pEachSeries;
// TODO: Remove this for the next major release
module.exports.default = pEachSeries;
export default pEachSeries;
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import pEachSeries = require('.');
import pEachSeries from './index.js';

const keywords = [Promise.resolve('foo'), 'rainbow', 'pony'];

Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -39,10 +41,10 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"time-span": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"time-span": "^4.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ npm install p-each-series
## Usage

```js
const pEachSeries = require('p-each-series');
import pEachSeries from 'p-each-series';

const keywords = [
getTopKeyword(), //=> Promise
Expand All @@ -23,10 +23,8 @@ const keywords = [

const iterator = async element => saveToDiskPromise(element);

(async () => {
console.log(await pEachSeries(keywords, iterator));
//=> ['unicorn', 'rainbow', 'pony']
})();
console.log(await pEachSeries(keywords, iterator));
//=> ['unicorn', 'rainbow', 'pony']
```

## API
Expand All @@ -52,7 +50,7 @@ Return value is ignored unless it's `Promise`, then it's awaited before continui
Stop iterating through items by returning `pEachSeries.stop` from the iterator function.

```js
const pEachSeries = require('p-each-series');
import pEachSeries from 'p-each-series';

// Logs `a` and `b`.
const result = await pEachSeries(['a', 'b', 'c'], value => {
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import delay from 'delay';
import timeSpan from 'time-span';
import pEachSeries from '.';
import pEachSeries from './index.js';

const fixtureError = new Error('fixture');

Expand Down Expand Up @@ -31,7 +31,7 @@ test('main', async t => {
});

test('rejection input rejects the promise', async t => {
await t.throwsAsync(pEachSeries([1, Promise.reject(fixtureError)], () => {}), fixtureError.message);
await t.throwsAsync(pEachSeries([1, Promise.reject(fixtureError)], () => {}), {message: fixtureError.message});
});

test('handles empty iterable', async t => {
Expand Down

0 comments on commit 5312b08

Please sign in to comment.