@common.js/p-locate 6.0.0 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.d.ts +33 -3
- package/index.js +154 -10
- package/package.json +24 -10
- package/readme.md +0 -91
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [p-locate](https://www.npmjs.com/package/p-locate) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [p-locate@
|
|
5
|
+
Exported from [p-locate@7.0.0](https://www.npmjs.com/package/p-locate/v/7.0.0) using https://github.com/etienne-martin/common.js.
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Options = {
|
|
2
2
|
/**
|
|
3
3
|
The number of concurrently pending promises returned by `tester`.
|
|
4
4
|
|
|
@@ -16,7 +16,7 @@ export interface Options {
|
|
|
16
16
|
@default true
|
|
17
17
|
*/
|
|
18
18
|
readonly preserveOrder?: boolean;
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
Get the first fulfilled promise that satisfies the provided testing function.
|
|
@@ -39,7 +39,7 @@ const files = [
|
|
|
39
39
|
const foundPath = await pLocate(files, file => pathExists(file));
|
|
40
40
|
|
|
41
41
|
console.log(foundPath);
|
|
42
|
-
//=> 'rainbow'
|
|
42
|
+
//=> 'rainbow.png'
|
|
43
43
|
```
|
|
44
44
|
*/
|
|
45
45
|
export default function pLocate<ValueType>(
|
|
@@ -47,3 +47,33 @@ export default function pLocate<ValueType>(
|
|
|
47
47
|
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
|
|
48
48
|
options?: Options
|
|
49
49
|
): Promise<ValueType | undefined>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
Get the first fulfilled promise that satisfies the provided testing function.
|
|
53
|
+
|
|
54
|
+
This overload accepts an `AsyncIterable` as input. Since async iterables are iterated serially, the `concurrency` and `preserveOrder` options are not applicable.
|
|
55
|
+
|
|
56
|
+
@param input - An async iterable of promises/values to test.
|
|
57
|
+
@param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
|
|
58
|
+
@returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
|
|
59
|
+
|
|
60
|
+
@example
|
|
61
|
+
```
|
|
62
|
+
import pLocate from 'p-locate';
|
|
63
|
+
|
|
64
|
+
async function * getFiles() {
|
|
65
|
+
yield 'unicorn.png';
|
|
66
|
+
yield 'rainbow.png';
|
|
67
|
+
yield 'pony.png';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const foundPath = await pLocate(getFiles(), file => file === 'rainbow.png');
|
|
71
|
+
|
|
72
|
+
console.log(foundPath);
|
|
73
|
+
//=> 'rainbow.png'
|
|
74
|
+
```
|
|
75
|
+
*/
|
|
76
|
+
export default function pLocate<ValueType>(
|
|
77
|
+
input: AsyncIterable<PromiseLike<ValueType> | ValueType>,
|
|
78
|
+
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
|
|
79
|
+
): Promise<ValueType | undefined>;
|
package/index.js
CHANGED
|
@@ -23,6 +23,47 @@ function _assertThisInitialized(self) {
|
|
|
23
23
|
}
|
|
24
24
|
return self;
|
|
25
25
|
}
|
|
26
|
+
function _asyncIterator(iterable) {
|
|
27
|
+
var method, async, sync, retry = 2;
|
|
28
|
+
for("undefined" != typeof Symbol && (async = Symbol.asyncIterator, sync = Symbol.iterator); retry--;){
|
|
29
|
+
if (async && null != (method = iterable[async])) return method.call(iterable);
|
|
30
|
+
if (sync && null != (method = iterable[sync])) return new AsyncFromSyncIterator(method.call(iterable));
|
|
31
|
+
async = "@@asyncIterator", sync = "@@iterator";
|
|
32
|
+
}
|
|
33
|
+
throw new TypeError("Object is not async iterable");
|
|
34
|
+
}
|
|
35
|
+
function AsyncFromSyncIterator(s) {
|
|
36
|
+
function AsyncFromSyncIteratorContinuation(r) {
|
|
37
|
+
if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object."));
|
|
38
|
+
var done = r.done;
|
|
39
|
+
return Promise.resolve(r.value).then(function(value) {
|
|
40
|
+
return {
|
|
41
|
+
value: value,
|
|
42
|
+
done: done
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return AsyncFromSyncIterator = function(s) {
|
|
47
|
+
this.s = s, this.n = s.next;
|
|
48
|
+
}, AsyncFromSyncIterator.prototype = {
|
|
49
|
+
s: null,
|
|
50
|
+
n: null,
|
|
51
|
+
next: function() {
|
|
52
|
+
return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));
|
|
53
|
+
},
|
|
54
|
+
return: function(value) {
|
|
55
|
+
var ret = this.s.return;
|
|
56
|
+
return void 0 === ret ? Promise.resolve({
|
|
57
|
+
value: value,
|
|
58
|
+
done: !0
|
|
59
|
+
}) : AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));
|
|
60
|
+
},
|
|
61
|
+
throw: function(value) {
|
|
62
|
+
var thr = this.s.return;
|
|
63
|
+
return void 0 === thr ? Promise.reject(value) : AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));
|
|
64
|
+
}
|
|
65
|
+
}, new AsyncFromSyncIterator(s);
|
|
66
|
+
}
|
|
26
67
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
27
68
|
try {
|
|
28
69
|
var info = gen[key](arg);
|
|
@@ -367,12 +408,115 @@ function pLocate(iterable, tester) {
|
|
|
367
408
|
}
|
|
368
409
|
function _pLocate() {
|
|
369
410
|
_pLocate = _asyncToGenerator(function(iterable, tester) {
|
|
370
|
-
var ref, _concurrency, concurrency, _preserveOrder, preserveOrder, limit, items, checkLimit, error;
|
|
411
|
+
var ref, _concurrency, concurrency, _preserveOrder, preserveOrder, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, element, resolvedElement, err, limit, items, checkLimit, error;
|
|
371
412
|
var _arguments = arguments;
|
|
372
413
|
return __generator(this, function(_state) {
|
|
373
414
|
switch(_state.label){
|
|
374
415
|
case 0:
|
|
375
416
|
ref = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {}, _concurrency = ref.concurrency, concurrency = _concurrency === void 0 ? Number.POSITIVE_INFINITY : _concurrency, _preserveOrder = ref.preserveOrder, preserveOrder = _preserveOrder === void 0 ? true : _preserveOrder;
|
|
417
|
+
if (!iterable[Symbol.asyncIterator]) return [
|
|
418
|
+
3,
|
|
419
|
+
15
|
|
420
|
+
];
|
|
421
|
+
_iteratorAbruptCompletion = false, _didIteratorError = false;
|
|
422
|
+
_state.label = 1;
|
|
423
|
+
case 1:
|
|
424
|
+
_state.trys.push([
|
|
425
|
+
1,
|
|
426
|
+
8,
|
|
427
|
+
9,
|
|
428
|
+
14
|
|
429
|
+
]);
|
|
430
|
+
_iterator = _asyncIterator(iterable);
|
|
431
|
+
_state.label = 2;
|
|
432
|
+
case 2:
|
|
433
|
+
return [
|
|
434
|
+
4,
|
|
435
|
+
_iterator.next()
|
|
436
|
+
];
|
|
437
|
+
case 3:
|
|
438
|
+
if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
|
|
439
|
+
3,
|
|
440
|
+
7
|
|
441
|
+
];
|
|
442
|
+
_value = _step.value;
|
|
443
|
+
element = _value;
|
|
444
|
+
return [
|
|
445
|
+
4,
|
|
446
|
+
element
|
|
447
|
+
];
|
|
448
|
+
case 4:
|
|
449
|
+
resolvedElement = _state.sent();
|
|
450
|
+
return [
|
|
451
|
+
4,
|
|
452
|
+
tester(resolvedElement)
|
|
453
|
+
];
|
|
454
|
+
case 5:
|
|
455
|
+
if (_state.sent() === true) {
|
|
456
|
+
return [
|
|
457
|
+
2,
|
|
458
|
+
resolvedElement
|
|
459
|
+
];
|
|
460
|
+
}
|
|
461
|
+
_state.label = 6;
|
|
462
|
+
case 6:
|
|
463
|
+
_iteratorAbruptCompletion = false;
|
|
464
|
+
return [
|
|
465
|
+
3,
|
|
466
|
+
2
|
|
467
|
+
];
|
|
468
|
+
case 7:
|
|
469
|
+
return [
|
|
470
|
+
3,
|
|
471
|
+
14
|
|
472
|
+
];
|
|
473
|
+
case 8:
|
|
474
|
+
err = _state.sent();
|
|
475
|
+
_didIteratorError = true;
|
|
476
|
+
_iteratorError = err;
|
|
477
|
+
return [
|
|
478
|
+
3,
|
|
479
|
+
14
|
|
480
|
+
];
|
|
481
|
+
case 9:
|
|
482
|
+
_state.trys.push([
|
|
483
|
+
9,
|
|
484
|
+
,
|
|
485
|
+
12,
|
|
486
|
+
13
|
|
487
|
+
]);
|
|
488
|
+
if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
|
|
489
|
+
3,
|
|
490
|
+
11
|
|
491
|
+
];
|
|
492
|
+
return [
|
|
493
|
+
4,
|
|
494
|
+
_iterator.return()
|
|
495
|
+
];
|
|
496
|
+
case 10:
|
|
497
|
+
_state.sent();
|
|
498
|
+
_state.label = 11;
|
|
499
|
+
case 11:
|
|
500
|
+
return [
|
|
501
|
+
3,
|
|
502
|
+
13
|
|
503
|
+
];
|
|
504
|
+
case 12:
|
|
505
|
+
if (_didIteratorError) {
|
|
506
|
+
throw _iteratorError;
|
|
507
|
+
}
|
|
508
|
+
return [
|
|
509
|
+
7
|
|
510
|
+
];
|
|
511
|
+
case 13:
|
|
512
|
+
return [
|
|
513
|
+
7
|
|
514
|
+
];
|
|
515
|
+
case 14:
|
|
516
|
+
return [
|
|
517
|
+
2
|
|
518
|
+
];
|
|
519
|
+
case 15:
|
|
376
520
|
limit = (0, _pLimit.default)(concurrency);
|
|
377
521
|
items = _toConsumableArray(iterable).map(function(element) {
|
|
378
522
|
return [
|
|
@@ -381,13 +525,13 @@ function _pLocate() {
|
|
|
381
525
|
];
|
|
382
526
|
});
|
|
383
527
|
checkLimit = (0, _pLimit.default)(preserveOrder ? 1 : Number.POSITIVE_INFINITY);
|
|
384
|
-
_state.label =
|
|
385
|
-
case
|
|
528
|
+
_state.label = 16;
|
|
529
|
+
case 16:
|
|
386
530
|
_state.trys.push([
|
|
387
|
-
|
|
388
|
-
|
|
531
|
+
16,
|
|
532
|
+
18,
|
|
389
533
|
,
|
|
390
|
-
|
|
534
|
+
19
|
|
391
535
|
]);
|
|
392
536
|
return [
|
|
393
537
|
4,
|
|
@@ -395,13 +539,13 @@ function _pLocate() {
|
|
|
395
539
|
return checkLimit(finder, element);
|
|
396
540
|
}))
|
|
397
541
|
];
|
|
398
|
-
case
|
|
542
|
+
case 17:
|
|
399
543
|
_state.sent();
|
|
400
544
|
return [
|
|
401
545
|
3,
|
|
402
|
-
|
|
546
|
+
19
|
|
403
547
|
];
|
|
404
|
-
case
|
|
548
|
+
case 18:
|
|
405
549
|
error = _state.sent();
|
|
406
550
|
if (_instanceof(error, EndError)) {
|
|
407
551
|
return [
|
|
@@ -410,7 +554,7 @@ function _pLocate() {
|
|
|
410
554
|
];
|
|
411
555
|
}
|
|
412
556
|
throw error;
|
|
413
|
-
case
|
|
557
|
+
case 19:
|
|
414
558
|
return [
|
|
415
559
|
2
|
|
416
560
|
];
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-locate",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.0.1",
|
|
4
|
+
"description": "p-locate package exported as CommonJS modules",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "etienne-martin/common.js",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
8
|
"type": "commonjs",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
9
14
|
"engines": {
|
|
10
|
-
"node": "
|
|
15
|
+
"node": ">=20"
|
|
11
16
|
},
|
|
12
17
|
"scripts": {
|
|
13
18
|
"test": "xo && ava && tsd"
|
|
@@ -17,16 +22,25 @@
|
|
|
17
22
|
"index.d.ts"
|
|
18
23
|
],
|
|
19
24
|
"dependencies": {
|
|
20
|
-
"
|
|
25
|
+
"p-limit": "npm:@common.js/p-limit@7.3.1"
|
|
21
26
|
},
|
|
22
27
|
"devDependencies": {
|
|
23
|
-
"ava": "^
|
|
24
|
-
"delay": "^
|
|
28
|
+
"ava": "^6.4.1",
|
|
29
|
+
"delay": "^7.0.0",
|
|
25
30
|
"in-range": "^3.0.0",
|
|
26
|
-
"time-span": "^5.
|
|
27
|
-
"tsd": "^0.
|
|
28
|
-
"xo": "^
|
|
31
|
+
"time-span": "^5.1.0",
|
|
32
|
+
"tsd": "^0.33.0",
|
|
33
|
+
"xo": "^1.2.3"
|
|
29
34
|
},
|
|
30
35
|
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
31
|
-
"
|
|
36
|
+
"commonjs": {
|
|
37
|
+
"source": {
|
|
38
|
+
"name": "p-locate",
|
|
39
|
+
"version": "7.0.0"
|
|
40
|
+
},
|
|
41
|
+
"transformRevision": 1,
|
|
42
|
+
"buildKey": "f2580d993728d7e4cd1a0a8503e9aadd66b2a32ff156a322e0b6579f9d0f7c1f"
|
|
43
|
+
},
|
|
44
|
+
"main": "./index.js",
|
|
45
|
+
"types": "./index.d.ts"
|
|
32
46
|
}
|
package/readme.md
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# p-locate
|
|
2
|
-
|
|
3
|
-
> Get the first fulfilled promise that satisfies the provided testing function
|
|
4
|
-
|
|
5
|
-
Think of it like an async version of [`Array#find`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
$ npm install p-locate
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
Here we find the first file that exists on disk, in array order.
|
|
16
|
-
|
|
17
|
-
```js
|
|
18
|
-
import {pathExists} from 'path-exists';
|
|
19
|
-
import pLocate from 'p-locate';
|
|
20
|
-
|
|
21
|
-
const files = [
|
|
22
|
-
'unicorn.png',
|
|
23
|
-
'rainbow.png', // Only this one actually exists on disk
|
|
24
|
-
'pony.png'
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
const foundPath = await pLocate(files, file => pathExists(file));
|
|
28
|
-
|
|
29
|
-
console.log(foundPath);
|
|
30
|
-
//=> 'rainbow'
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
*The above is just an example. Use [`locate-path`](https://github.com/sindresorhus/locate-path) if you need this.*
|
|
34
|
-
|
|
35
|
-
## API
|
|
36
|
-
|
|
37
|
-
### pLocate(input, tester, options?)
|
|
38
|
-
|
|
39
|
-
Returns a `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
|
|
40
|
-
|
|
41
|
-
#### input
|
|
42
|
-
|
|
43
|
-
Type: `Iterable<Promise | unknown>`
|
|
44
|
-
|
|
45
|
-
An iterable of promises/values to test.
|
|
46
|
-
|
|
47
|
-
#### tester(element)
|
|
48
|
-
|
|
49
|
-
Type: `Function`
|
|
50
|
-
|
|
51
|
-
This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
|
|
52
|
-
|
|
53
|
-
#### options
|
|
54
|
-
|
|
55
|
-
Type: `object`
|
|
56
|
-
|
|
57
|
-
##### concurrency
|
|
58
|
-
|
|
59
|
-
Type: `number`\
|
|
60
|
-
Default: `Infinity`\
|
|
61
|
-
Minimum: `1`
|
|
62
|
-
|
|
63
|
-
The number of concurrently pending promises returned by `tester`.
|
|
64
|
-
|
|
65
|
-
##### preserveOrder
|
|
66
|
-
|
|
67
|
-
Type: `boolean`\
|
|
68
|
-
Default: `true`
|
|
69
|
-
|
|
70
|
-
Preserve `input` order when searching.
|
|
71
|
-
|
|
72
|
-
Disable this to improve performance if you don't care about the order.
|
|
73
|
-
|
|
74
|
-
## Related
|
|
75
|
-
|
|
76
|
-
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
|
|
77
|
-
- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
|
|
78
|
-
- [p-any](https://github.com/sindresorhus/p-any) - Wait for any promise to be fulfilled
|
|
79
|
-
- [More…](https://github.com/sindresorhus/promise-fun)
|
|
80
|
-
|
|
81
|
-
---
|
|
82
|
-
|
|
83
|
-
<div align="center">
|
|
84
|
-
<b>
|
|
85
|
-
<a href="https://tidelift.com/subscription/pkg/npm-p-locate?utm_source=npm-p-locate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
86
|
-
</b>
|
|
87
|
-
<br>
|
|
88
|
-
<sub>
|
|
89
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
90
|
-
</sub>
|
|
91
|
-
</div>
|