Skip to content

Commit

Permalink
Move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 18, 2021
1 parent b8ae27b commit 41c2559
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- 12
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
83 changes: 38 additions & 45 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {Options as PMapOptions} from 'p-map';
import {Options} from 'p-map';

declare namespace pProps {
type Options = PMapOptions;
export type PromiseResult<Value> = Value extends PromiseLike<infer Result> ? Result : Value;

type PromiseResult<Value> = Value extends PromiseLike<infer Result> ? Result : Value;

type Mapper<ValueType, KeyType, MappedValueType> = (
value: ValueType,
key: KeyType
) => MappedValueType | PromiseLike<MappedValueType>;
}
export type Mapper<ValueType, KeyType, MappedValueType> = (
value: ValueType,
key: KeyType
) => MappedValueType | PromiseLike<MappedValueType>;

/**
Like [`Promise.all()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) but for `Map` and `Object`.
Expand All @@ -21,50 +17,47 @@ Like [`Promise.all()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Refe
@example
```
import pProps = require('p-props');
import got = require('got');
(async () => {
const fetch = async url => {
const {body} = await got(url);
return body;
};
const sites = {
ava: fetch('https://avajs.dev'),
todomvc: fetch('https://todomvc.com'),
github: fetch('https://github.com'),
foo: 'bar'
};
console.log(await pProps(sites));
// {
// ava: '<!doctype …',
// todomvc: '<!doctype …',
// github: '<!doctype …',
// foo: 'bar'
// }
})();
import pProps from 'p-props';
import got from 'got';
const fetch = async url => {
const {body} = await got(url);
return body;
};
const sites = {
ava: fetch('https://avajs.dev'),
todomvc: fetch('https://todomvc.com'),
github: fetch('https://github.com'),
foo: 'bar'
};
console.log(await pProps(sites));
// {
// ava: '<!doctype …',
// todomvc: '<!doctype …',
// github: '<!doctype …',
// foo: 'bar'
// }
```
*/
declare function pProps<
export default function pProps<
KeyType,
ValueType,
MappedValueType = pProps.PromiseResult<ValueType>
MappedValueType = PromiseResult<ValueType>
>(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
map: ReadonlyMap<KeyType, ValueType>,
mapper?: pProps.Mapper<pProps.PromiseResult<ValueType>, KeyType, MappedValueType>,
options?: pProps.Options
mapper?: Mapper<PromiseResult<ValueType>, KeyType, MappedValueType>,
options?: Options
): Promise<Map<KeyType, MappedValueType>>;
declare function pProps<
InputType extends {[key: string]: any},
export default function pProps<
InputType extends Record<string, any>,
ValueType extends InputType[keyof InputType],
MappedValueType = pProps.PromiseResult<ValueType>
MappedValueType = PromiseResult<ValueType>
>(
map: InputType,
mapper?: pProps.Mapper<pProps.PromiseResult<ValueType>, keyof InputType, MappedValueType>,
options?: pProps.Options
mapper?: Mapper<PromiseResult<ValueType>, keyof InputType, MappedValueType>,
options?: Options
): Promise<{[key in keyof InputType]: MappedValueType}>;

export = pProps;
export {Options} from 'p-map';
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict';
const pMap = require('p-map');
import pMap from 'p-map';

const pProps = async (input, mapper = (value => value), options = undefined) => {
export default async function pProps(input, mapper = (value => value), options = undefined) {
const isMap = input instanceof Map;
const entries = isMap ? [...input.entries()] : Object.entries(input);
const values = await pMap(entries, async ([key, value]) => mapper(await value, key), options);
const mappedEntries = entries.map(([key], index) => [key, values[index]]);
return isMap ? new Map(mappedEntries) : Object.fromEntries(mappedEntries);
};

module.exports = pProps;
}
11 changes: 5 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expectType, expectAssignable} from 'tsd';
import pProps = require('.');
import pProps, {Options} from './index.js';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const options: pProps.Options = {};
const options: Options = {};

expectType<Promise<{[key in 'foo']: string}>>(pProps({foo: 'bar'}));
expectType<Promise<{[key in 'foo']: boolean}>>(
Expand Down Expand Up @@ -63,10 +63,9 @@ const map = new Map<number, string | Promise<string>>([
[2, '2']
]);

pProps(map).then(result => {
expectType<Map<number, string>>(result);
expectType<string | undefined>(result.get(1));
});
const result = await pProps(map);
expectType<Map<number, string>>(result);
expectType<string | undefined>(result.get(1));

expectType<Promise<Map<number, string>>>(pProps(map));
expectType<Promise<Map<number, number>>>(
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": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,12 +39,12 @@
"bluebird"
],
"dependencies": {
"p-map": "^4.0.0"
"p-map": "^5.0.0"
},
"devDependencies": {
"ava": "^2.0.0",
"delay": "^4.2.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"tsd": "^0.17.0",
"xo": "^0.40.2"
}
}
50 changes: 24 additions & 26 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,30 @@ $ npm install p-props
## Usage

```js
const pProps = require('p-props');
const got = require('got');

(async () => {
const fetch = async url => {
const {body} = await got(url);
return body;
};

const sites = {
ava: fetch('https://avajs.dev'),
todomvc: fetch('https://todomvc.com'),
github: fetch('https://github.com'),
foo: 'bar'
};

console.log(await pProps(sites));
/*
{
ava: '<!doctype …',
todomvc: '<!doctype …',
github: '<!doctype …',
foo: 'bar'
}
*/
})();
import pProps from 'p-props';
import got from 'got';

const fetch = async url => {
const {body} = await got(url);
return body;
};

const sites = {
ava: fetch('https://avajs.dev'),
todomvc: fetch('https://todomvc.com'),
github: fetch('https://github.com'),
foo: 'bar'
};

console.log(await pProps(sites));
/*
{
ava: '<!doctype …',
todomvc: '<!doctype …',
github: '<!doctype …',
foo: 'bar'
}
*/
```

## API
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import delay from 'delay';
import pProps from '.';
import pProps from './index.js';

test('main', async t => {
t.deepEqual(
Expand Down Expand Up @@ -36,7 +36,7 @@ test('rejects if any of the input promises reject', async t => {
foo: Promise.resolve(1),
bar: Promise.reject(new Error('bar'))
}),
'bar'
{message: 'bar'}
);
});

Expand Down

0 comments on commit 41c2559

Please sign in to comment.