diff --git a/README.md b/README.md index adfb32e..8f0406e 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,13 @@ The functions are standalone and depends on no particular Promise implementation and therefore works well for JavaScript's built-in Promise. -The library is written in TypeScript, so typings are provided. Apart from being exported as JavaScript (ES2019), it's also exported as an *ES module*, if imported in platforms (and bundlers) supporting this. +The library is written in TypeScript, so typings are provided. It is exported only as an [ESM package](esm-package)! # Versions * Since version 2, `Finally` and `Try` are removed. They should be replaced with `Promise.prototype.finally` and async functions. + * Since version 3, it's only exported as an [ESM package](esm-package). # Types @@ -170,13 +171,11 @@ somePromise .then( delayChain( 100 ) ) ``` -To always delay a chain, regardless of whether it was resolved or rejected, use `finallyDelay`. **Note the triple dots**, and read more about `Finally` below... +To always delay a chain, regardless of whether it was resolved or rejected, use: ```ts -import { finallyDelay } from 'already' - somePromise -.then( ...finallyDelay( 100 ) ) +.finally( delayChain( 100 ) ) ``` @@ -888,3 +887,4 @@ async function getConnection( ) [greenkeeper-url]: https://greenkeeper.io/ [lgtm-image]: https://img.shields.io/lgtm/grade/javascript/g/grantila/already.svg?logo=lgtm&logoWidth=18 [lgtm-url]: https://lgtm.com/projects/g/grantila/already/context:javascript +[esm-package]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c diff --git a/jest.config.common.js b/jest.config.common.js deleted file mode 100644 index 152147c..0000000 --- a/jest.config.common.js +++ /dev/null @@ -1,22 +0,0 @@ -module.exports = { - // preset: "ts-jest", - "transform": { - "^.+\\.m?jsx?$": "babel-jest", - "^.+\\.tsx?$": "ts-jest" - }, - testEnvironment: "node", - testMatch: [ - "/test-out-es5/**/*.js", - "/test-out/**/*.js", - "/test/**/*.ts", - ], - modulePathIgnorePatterns: [ - ".*\.d\.ts" - ], - collectCoverageFrom: ["/dist/**/*.js"], - coverageReporters: ["lcov", "text", "html"], - setupFiles: [ - "trace-unhandled/register", - ], - maxConcurrency: Infinity, -}; diff --git a/jest.config.js b/jest.config.js index a94fc4c..ba3e230 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,7 +1,24 @@ -const hasModules = - parseInt( process.versions.node.split( "." )[ 0 ], 10 ) > 12; - -module.exports = - hasModules - ? require( './jest.config.modules' ) - : require( './jest.config.common' ); +export default { + // preset: "ts-jest", + "transform": { + "^.+\\.jsx?$": "babel-jest", + "^.+\\.tsx?$": "ts-jest" + }, + testEnvironment: "node", + testMatch: [ + "/test-out/**/*.js", + "/test/**/*.ts", + ], + moduleFileExtensions: ["js", "ts", "mjs"], + modulePathIgnorePatterns: [ + ".*\.d\.ts" + ], + collectCoverageFrom: [ + "/dist/**/*.js", + ], + coverageReporters: ["lcov", "text", "html"], + setupFiles: [ + "trace-unhandled/register", + ], + maxConcurrency: Infinity, +} diff --git a/jest.config.modules.js b/jest.config.modules.js deleted file mode 100644 index 63c8c3e..0000000 --- a/jest.config.modules.js +++ /dev/null @@ -1,14 +0,0 @@ -const cjsConfig = require( './jest.config.common' ); - -module.exports = Object.assign( { }, cjsConfig, { - moduleFileExtensions: ["js", "ts", "mjs"], - testMatch: [ - ...cjsConfig.testMatch, - "/test-out-mjs/**/*.js", - "/test-out-mjs/**/*.mjs" - ], - collectCoverageFrom: [ - ...cjsConfig.collectCoverageFrom, - "/dist-mjs/**/*.js" - ], -} ); diff --git a/lib/index.ts b/lib/index.ts index 2922d08..86d70a9 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -631,7 +631,7 @@ function onceDynamic( ): OnceRunner export function retry< R >( times: number, fn: ( ) => R, - retryable: ( err: Error ) => boolean = ( ) => true + retryable: ( err: unknown ) => boolean = ( ) => true ) : R { @@ -647,7 +647,7 @@ export function retry< R >( return retryAsync( < any >fn( ) ); } ); - const retrySync = ( _err: Error ): R => + const retrySync = ( _err: unknown ): R => { while ( --times >= 0 ) { diff --git a/package.json b/package.json index 1e79998..4e06054 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "already", - "description": "Promise extensions; finally, map, filter, etc", + "description": "Promise extensions; map, filter, etc", "license": "MIT", "version": "0.0.0-development", "author": "Gustaf Räntilä ", @@ -8,23 +8,19 @@ "type": "git", "url": "https://github.com/grantila/already" }, + "engines": { + "node": ">=12.20" + }, + "type": "module", "files": [ - "dist", - "dist-mjs" + "dist" ], "main": "./dist/index.js", + "exports": "./dist/index.js", "types": "./dist/index.d.ts", - "exports": { - "import": "./dist-mjs/index.js", - "require": "./dist/index.js" - }, "scripts": { - "build:dist": "scripts/build.sh dist ./tsconfig.prod.json --module CommonJS -t ES2019", - "build:dist-mjs": "scripts/build.sh dist-mjs ./tsconfig.prod.json --module ESNext -t ES2019", - "build:test-out": "scripts/build.sh test-out ./tsconfig.test.json", - "build:test-out-mjs": "scripts/build.sh test-out-mjs ./tsconfig.test.json --module CommonJS -t esnext && scripts/rewrite-mjs-test.js", - "build:lib": "concurrently 'yarn build:dist' 'yarn build:dist-mjs'", - "build:test": "concurrently 'yarn build:test-out' 'yarn build:test-out-mjs'", + "build:lib": "scripts/build.sh dist ./tsconfig.prod.json", + "build:test": "scripts/build.sh test-out ./tsconfig.test.json", "build": "yarn build:lib && yarn build:test", "lint": "true", "jest": "node_modules/.bin/jest --coverage", @@ -52,11 +48,11 @@ ], "devDependencies": { "@babel/plugin-syntax-optional-chaining": "^7.8.3", - "@babel/plugin-transform-modules-commonjs": "^7.12.1", - "@babel/preset-typescript": "^7.12.7", + "@babel/plugin-transform-modules-commonjs": "^7.15.4", + "@babel/preset-typescript": "^7.15.0", "@types/jest": "^26.0.15", "commitizen": "^4.2.2", - "concurrently": "^6.2.0", + "concurrently": "^6.2.1", "cz-conventional-changelog": "^3.3.0", "jest": "^26.6.3", "pre-commit": "1.x", @@ -64,7 +60,7 @@ "trace-unhandled": "^2.0.1", "ts-jest": "^26.4.4", "tslint": "6.1.3", - "typescript": "4.3.4" + "typescript": "4.4.3" }, "dependencies": {}, "config": { diff --git a/test/index.ts b/test/index.ts index 4565c0e..94d12f9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2323,9 +2323,9 @@ describe( "wrapFunction", ( ) => ( ( ) => Promise.resolve( 42 ) ); expect( false ).toBe( true ); } - catch ( err ) + catch ( err: any ) { - expect( err.message ).toBe( + expect( err?.message ).toBe( "Invalid invocation, function requires 2 arguments" ); } @@ -2358,9 +2358,9 @@ describe( "wrapFunction", ( ) => ( "foo", ( ) => Promise.resolve( 42 ) ); expect( false ).toBe( true ); } - catch ( err ) + catch ( err: any ) { - expect( err.message ).toBe( + expect( err?.message ).toBe( "Invalid return value in 'before' handler" ); } @@ -2383,9 +2383,9 @@ describe( "wrapFunction", ( ) => ( "foo", ( ) => Promise.resolve( 42 ) ); expect( false ).toBe( true ); } - catch ( err ) + catch ( err: any ) { - expect( err.message ).toBe( "foobar" ); + expect( err?.message ).toBe( "foobar" ); } } )( ); diff --git a/tsconfig.json b/tsconfig.json index 9996790..a56d9e8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "node" ], "target": "ES2019", - "module": "CommonJS", + "module": "ES2020", "moduleResolution": "node", "noImplicitAny": true, "noUnusedLocals": true,