Clean up the oopsie. Rebuild the blank exercises.

This commit is contained in:
Michael Frank
2021-05-09 00:39:21 +12:00
parent b984cb4c6f
commit d28d80c46f
1565 changed files with 27186 additions and 199003 deletions
+39 -18
View File
@@ -8,24 +8,45 @@ class EndError extends Error {
}
}
// the input can also be a promise, so we `Promise.all()` them both
const finder = el => Promise.all(el).then(val => val[1] === true && Promise.reject(new EndError(val[0])));
// The input can also be a promise, so we await it
const testElement = async (element, tester) => tester(await element);
module.exports = (iterable, tester, opts) => {
opts = Object.assign({
concurrency: Infinity,
preserveOrder: true
}, opts);
// The input can also be a promise, so we `Promise.all()` them both
const finder = async element => {
const values = await Promise.all(element);
if (values[1] === true) {
throw new EndError(values[0]);
}
const limit = pLimit(opts.concurrency);
// start all the promises concurrently with optional limit
const items = Array.from(iterable).map(el => [el, limit(() => Promise.resolve(el).then(tester))]);
// check the promises either serially or concurrently
const checkLimit = pLimit(opts.preserveOrder ? 1 : Infinity);
return Promise.all(items.map(el => checkLimit(() => finder(el))))
.then(() => {})
.catch(err => err instanceof EndError ? err.value : Promise.reject(err));
return false;
};
const pLocate = async (iterable, tester, options) => {
options = {
concurrency: Infinity,
preserveOrder: true,
...options
};
const limit = pLimit(options.concurrency);
// Start all the promises concurrently with optional limit
const items = [...iterable].map(element => [element, limit(testElement, element, tester)]);
// Check the promises either serially or concurrently
const checkLimit = pLimit(options.preserveOrder ? 1 : Infinity);
try {
await Promise.all(items.map(element => checkLimit(finder, element)));
} catch (error) {
if (error instanceof EndError) {
return error.value;
}
throw error;
}
};
module.exports = pLocate;
// TODO: Remove this for the next major release
module.exports.default = pLocate;
+4 -16
View File
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+21 -22
View File
@@ -1,27 +1,27 @@
{
"_from": "p-locate@^2.0.0",
"_id": "p-locate@2.0.0",
"_from": "p-locate@^4.1.0",
"_id": "p-locate@4.1.0",
"_inBundle": false,
"_integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=",
"_integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
"_location": "/p-locate",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "p-locate@^2.0.0",
"raw": "p-locate@^4.1.0",
"name": "p-locate",
"escapedName": "p-locate",
"rawSpec": "^2.0.0",
"rawSpec": "^4.1.0",
"saveSpec": null,
"fetchSpec": "^2.0.0"
"fetchSpec": "^4.1.0"
},
"_requiredBy": [
"/locate-path"
],
"_resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz",
"_shasum": "20a0103b222a70c8fd39cc2e580680f3dde5ec43",
"_spec": "p-locate@^2.0.0",
"_where": "/Users/cloyd/coderrr/odin/javascript-exercises/node_modules/locate-path",
"_resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
"_shasum": "a3428bb7088b3a60292f66919278b7c297ad4f07",
"_spec": "p-locate@^4.1.0",
"_where": "/home/michael/projects/javascript-exercises/node_modules/locate-path",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
@@ -32,22 +32,24 @@
},
"bundleDependencies": false,
"dependencies": {
"p-limit": "^1.1.0"
"p-limit": "^2.2.0"
},
"deprecated": false,
"description": "Get the first fulfilled promise that satisfies the provided testing function",
"devDependencies": {
"ava": "*",
"delay": "^1.3.1",
"ava": "^1.4.1",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"time-span": "^1.0.0",
"xo": "*"
"time-span": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"homepage": "https://github.com/sindresorhus/p-locate#readme",
"keywords": [
@@ -77,10 +79,7 @@
"url": "git+https://github.com/sindresorhus/p-locate.git"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"version": "2.0.0",
"xo": {
"esnext": true
}
"version": "4.1.0"
}
+10 -6
View File
@@ -8,7 +8,7 @@ Think of it like an async version of [`Array#find`](https://developer.mozilla.or
## Install
```
$ npm install --save p-locate
$ npm install p-locate
```
@@ -22,14 +22,16 @@ const pLocate = require('p-locate');
const files = [
'unicorn.png',
'rainbow.png', // only this one actually exists on disk
'rainbow.png', // Only this one actually exists on disk
'pony.png'
];
pLocate(files, file => pathExists(file)).then(foundPath => {
(async () => {
const foundPath = await pLocate(files, file => pathExists(file));
console.log(foundPath);
//=> 'rainbow'
});
})();
```
*The above is just an example. Use [`locate-path`](https://github.com/sindresorhus/locate-path) if you need this.*
@@ -43,13 +45,15 @@ Returns a `Promise` that is fulfilled when `tester` resolves to `true` or the it
#### input
Type: `Iterable<Promise|any>`
Type: `Iterable<Promise | unknown>`
An iterable of promises/values to test.
#### tester(element)
Type: `Function`
Expected to return a `Promise<boolean>` or boolean.
This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
#### options