Skip to content

Commit

Permalink
feat: add a auto polyfill way & update README
Browse files Browse the repository at this point in the history
  • Loading branch information
calimanco committed Jan 7, 2021
1 parent 15ef598 commit 4305d49
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ npm install promise-polyfill-plus --save

```javascript
import PromisePlus from 'promise-polyfill-plus'
// or
const PromisePlus = require('promise-polyfill-plus')

new PromisePlus((resolve, reject) => {
if (true) {
Expand All @@ -43,20 +45,16 @@ new PromisePlus((resolve, reject) => {
})
```

可以替换掉原生的 Promise。
可以自动增强原生的 Promise。如果环境无原生支持则使用 polyfill 版本挂载到全局

```javascript
import PromisePlus from 'promise-polyfill-plus'
// browser
if (window && typeof window.Promise !== 'function') {
window.Promise = PromisePlus
}
// nodejs
if (global && typeof global.Promise !== 'function') {
global.Promise = PromisePlus
}
import 'promise-polyfill-plus/auto'
// or
require('promise-polyfill-plus/auto')
```

如果你想

## 特性

- 实例部分遵循 Promise A+ 规范,也实现了常用但非规范的 catch 和 finally 方法。
Expand Down
16 changes: 6 additions & 10 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import to the code directly.

```javascript
import PromisePlus from 'promise-polyfill-plus'
// or
const PromisePlus = require('promise-polyfill-plus')

new PromisePlus((resolve, reject) => {
if (true) {
Expand All @@ -43,18 +45,12 @@ new PromisePlus((resolve, reject) => {
})
```

You can replace the original Promise.
The native Promise can be automatically enhanced. If the environment does not have native support, mount the polyfill version globally.

```javascript
import PromisePlus from 'promise-polyfill-plus'
// browser
if (window && typeof window.Promise !== 'function') {
window.Promise = PromisePlus
}
// nodejs
if (global && typeof global.Promise !== 'function') {
global.Promise = PromisePlus
}
import 'promise-polyfill-plus/auto'
// or
require('promise-polyfill-plus/auto')
```

## Features
Expand Down
3 changes: 3 additions & 0 deletions auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'
// eslint-disable-next-line @typescript-eslint/no-var-requires
module.exports = require('./dist/promise-polyfill-plus.umd').autoPolyfill()
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"module": "dist/promise-polyfill-plus.es5.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist"
"dist",
"auto.js"
],
"author": "calimanco <calimanco@qq.com>",
"repository": {
Expand All @@ -24,7 +25,7 @@
"node": ">=6.0.0"
},
"scripts": {
"lint": "eslint -c .eslintrc --ignore-pattern .eslintignore src/**/**/*.ts test/**/**/*.ts",
"lint": "eslint -c .eslintrc --ignore-pattern .eslintignore {src,test}/**/**/*.ts",
"prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.js && typedoc --out docs --target es6 --theme minimal --mode file src",
"start": "rollup -c rollup.config.js -w",
Expand All @@ -39,7 +40,7 @@
"precommit": "lint-staged"
},
"lint-staged": {
"{src,test}/**/*.ts": [
"{src,test}/**/**/*.ts": [
"eslint --fix",
"git add"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// no-promise-environment
// eslint-disable-next-line @typescript-eslint/no-var-requires
const NodeEnvironment = require('jest-environment-node')

class NoPromiseEnvironment extends NodeEnvironment {
Expand All @@ -10,7 +11,7 @@ class NoPromiseEnvironment extends NodeEnvironment {
async setup() {
await super.setup()

setTimeout(()=>{
setTimeout(() => {
this.global.Promise = undefined
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/no_promise_env.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @jest-environment ./test/env/no-promise-environment
* @jest-environment ./test/environment/no-promise-environment
*/

import { autoPolyfill } from '../src'
Expand Down

0 comments on commit 4305d49

Please sign in to comment.