From 5312b0880f7c730e6f1c4d7035493e6acd1433ff Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 9 Apr 2021 12:35:06 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM --- .github/workflows/main.yml | 4 +-- index.d.ts | 63 +++++++++++++++++--------------------- index.js | 6 +--- index.test-d.ts | 2 +- package.json | 14 +++++---- readme.md | 10 +++--- test.js | 4 +-- 7 files changed, 45 insertions(+), 58 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..d36e1a8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index 2425bd0..8bd7b63 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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. @@ -14,7 +34,7 @@ declare const pEachSeries: { @example ``` - import pEachSeries = require('p-each-series'); + import pEachSeries from 'p-each-series'; const keywords = [ getTopKeyword(), //=> Promise @@ -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'] ``` */ ( input: Iterable | ValueType>, - iterator: (element: ValueType, index: number) => pEachSeries.StopSymbol | unknown + iterator: (element: ValueType, index: number) => StopSymbol | unknown ): Promise; - - /** - 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; diff --git a/index.js b/index.js index d1a5c8b..35fe504 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -'use strict'; - const pEachSeries = async (iterable, iterator) => { let index = 0; @@ -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; diff --git a/index.test-d.ts b/index.test-d.ts index 92d070d..0b12e33 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import pEachSeries = require('.'); +import pEachSeries from './index.js'; const keywords = [Promise.resolve('foo'), 'rainbow', 'pony']; diff --git a/package.json b/package.json index 4816ad3..bcc3a2b 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" @@ -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" } } diff --git a/readme.md b/readme.md index c5e95f0..32a330d 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 @@ -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 => { diff --git a/test.js b/test.js index c4d81a5..090775f 100644 --- a/test.js +++ b/test.js @@ -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'); @@ -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 => {