Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/p-filter
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.1.0
Choose a base ref
...
head repository: sindresorhus/p-filter
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.0.0
Choose a head ref
  • 4 commits
  • 9 files changed
  • 2 contributors

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    eb7d98f View commit details

Commits on Dec 31, 2020

  1. Move to GitHub Actions (#5)

    Richienb authored Dec 31, 2020
    Copy the full SHA
    a23c8a6 View commit details

Commits on Aug 12, 2021

  1. Copy the full SHA
    63f8c5a View commit details
  2. 3.0.0

    sindresorhus committed Aug 12, 2021
    Copy the full SHA
    befd29e View commit details
Showing with 93 additions and 109 deletions.
  1. +20 −0 .github/workflows/main.yml
  2. +0 −4 .travis.yml
  3. +39 −59 index.d.ts
  4. +5 −9 index.js
  5. +5 −5 index.test-d.ts
  6. +1 −1 license
  7. +10 −7 package.json
  8. +12 −23 readme.md
  9. +1 −1 test.js
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

98 changes: 39 additions & 59 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,41 @@
import {Options as PMapOptions} from 'p-map';

declare namespace pFilter {
type Options = PMapOptions;
}

declare const pFilter: {
/**
Filter promises concurrently.
@param input - Iterated over concurrently in the `filterer` function.
@param filterer - The filterer function that decides whether an element should be included into result.
@example
```
import pFilter = require('p-filter');
import getWeather from 'get-weather'; // not a real module
const places = [
getCapital('Norway').then(info => info.name),
'Bangkok, Thailand',
'Berlin, Germany',
'Tokyo, Japan'
];
const filterer = async place => {
const weather = await getWeather(place);
return weather.temperature > 30;
};
(async () => {
const result = await pFilter(places, filterer);
console.log(result);
//=> ['Bangkok, Thailand']
})();
```
*/
<ValueType>(
input: Iterable<ValueType | PromiseLike<ValueType>>,
filterer: (
element: ValueType,
index: number
) => boolean | PromiseLike<boolean>,
options?: pFilter.Options
): Promise<ValueType[]>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pFilter<ValueType>(
// input: Iterable<ValueType | PromiseLike<ValueType>>,
// filterer: (
// element: ValueType,
// index: number
// ) => boolean | PromiseLike<boolean>,
// options?: pFilter.Options
// ): Promise<ValueType[]>;
// export = pFilter;
default: typeof pFilter;
import {Options} from 'p-map';

/**
Filter promises concurrently.
@param input - Iterated over concurrently in the `filterer` function.
@param filterer - The filterer function that decides whether an element should be included into result.
@example
```
import pFilter from 'p-filter';
import getWeather from 'get-weather'; // Not a real module
const places = [
getCapital('Norway').then(info => info.name),
'Bangkok, Thailand',
'Berlin, Germany',
'Tokyo, Japan',
];
const filterer = async place => {
const weather = await getWeather(place);
return weather.temperature > 30;
};
export = pFilter;
const result = await pFilter(places, filterer);
console.log(result);
//=> ['Bangkok, Thailand']
```
*/
export default function pFilter<ValueType>(
input: Iterable<ValueType | PromiseLike<ValueType>>,
filterer: (
element: ValueType,
index: number
) => boolean | PromiseLike<boolean>,
options?: Options
): Promise<ValueType[]>;

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

const pFilter = async (iterable, filterer, options) => {
export default async function pFilter(iterable, filterer, options) {
const values = await pMap(
iterable,
(element, index) => Promise.all([filterer(element, index), element]),
options
options,
);
return values.filter(value => Boolean(value[0])).map(value => value[1]);
};

module.exports = pFilter;
// TODO: Remove this for the next major release
module.exports.default = pFilter;
return values.filter(value => Boolean(value[0])).map(value => value[1]);
}
10 changes: 5 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {expectType} from 'tsd';
import pFilter = require('.');
import pFilter from './index.js';

const places = [
'Bangkok, Thailand',
'Berlin, Germany',
Promise.resolve('Tokyo, Japan')
Promise.resolve('Tokyo, Japan'),
];

expectType<Promise<string[]>>(
pFilter(places, async place =>
place === 'Bangkok, Thailand' ? true : Promise.resolve(false)
)
place === 'Bangkok, Thailand' ? true : Promise.resolve(false),
),
);
expectType<Promise<number[]>>(
pFilter(new Set([1, 2]), number => number > 1, {concurrency: 1})
pFilter(new Set([1, 2]), number => number > 1, {concurrency: 1}),
);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "p-filter",
"version": "2.1.0",
"version": "3.0.0",
"description": "Filter promises concurrently",
"license": "MIT",
"repository": "sindresorhus/p-filter",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -35,11 +38,11 @@
"bluebird"
],
"dependencies": {
"p-map": "^2.0.0"
"p-map": "^5.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
35 changes: 12 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
# p-filter [![Build Status](https://travis-ci.org/sindresorhus/p-filter.svg?branch=master)](https://travis-ci.org/sindresorhus/p-filter)
# p-filter

> Filter promises concurrently
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently and get a filtered down result.


## Install

```
$ npm install p-filter
```


## Usage

```js
const pFilter = require('p-filter');
const getWeather = require('get-weather'); // not a real module
import pFilter from 'p-filter';
import getWeather from 'get-weather'; // Not a real module

const places = [
getCapital('Norway').then(info => info.name),
'Bangkok, Thailand',
'Berlin, Germany',
'Tokyo, Japan'
'Tokyo, Japan',
];

const filterer = async place => {
const weather = await getWeather(place);
return weather.temperature > 30;
};

(async () => {
const result = await pFilter(places, filterer);
const result = await pFilter(places, filterer);

console.log(result);
//=> ['Bangkok, Thailand']
})();
console.log(result);
//=> ['Bangkok, Thailand']
```


## API

### pFilter(input, filterer, [options])
### pFilter(input, filterer, options?)

Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `filterer` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `filterer` in `input` order.

@@ -59,25 +54,19 @@ The filterer function that decides whether an element should be included into re

#### options

Type: `Object`
Type: `object`

##### concurrency

Type: `number`<br>
Default: `Infinity`<br>
Type: `number`\
Default: `Infinity`\
Minimum: `1`

Number of concurrently pending promises returned by `filterer`.

The number of concurrently pending promises returned by `filterer`.

## Related

- [p-locate](https://github.com/sindresorhus/p-locate) - Get the first fulfilled promise that satisfies the provided testing function
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
- [More…](https://github.com/sindresorhus/promise-fun)


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import pFilter from '.';
import pFilter from './index.js';

// See `p-map` for more comprehensive tests
test('main', async t => {