| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 |
|
|---|
| 7 | var _filter2 = require('./internal/filter.js');
|
|---|
| 8 |
|
|---|
| 9 | var _filter3 = _interopRequireDefault(_filter2);
|
|---|
| 10 |
|
|---|
| 11 | var _eachOf = require('./eachOf.js');
|
|---|
| 12 |
|
|---|
| 13 | var _eachOf2 = _interopRequireDefault(_eachOf);
|
|---|
| 14 |
|
|---|
| 15 | var _awaitify = require('./internal/awaitify.js');
|
|---|
| 16 |
|
|---|
| 17 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
|---|
| 18 |
|
|---|
| 19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Returns a new array of all the values in `coll` which pass an async truth
|
|---|
| 23 | * test. This operation is performed in parallel, but the results array will be
|
|---|
| 24 | * in the same order as the original.
|
|---|
| 25 | *
|
|---|
| 26 | * @name filter
|
|---|
| 27 | * @static
|
|---|
| 28 | * @memberOf module:Collections
|
|---|
| 29 | * @method
|
|---|
| 30 | * @alias select
|
|---|
| 31 | * @category Collection
|
|---|
| 32 | * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|---|
| 33 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
|---|
| 34 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
|---|
| 35 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
|---|
| 36 | * @param {Function} [callback] - A callback which is called after all the
|
|---|
| 37 | * `iteratee` functions have finished. Invoked with (err, results).
|
|---|
| 38 | * @returns {Promise} a promise, if no callback provided
|
|---|
| 39 | * @example
|
|---|
| 40 | *
|
|---|
| 41 | * // dir1 is a directory that contains file1.txt, file2.txt
|
|---|
| 42 | * // dir2 is a directory that contains file3.txt, file4.txt
|
|---|
| 43 | * // dir3 is a directory that contains file5.txt
|
|---|
| 44 | *
|
|---|
| 45 | * const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt'];
|
|---|
| 46 | *
|
|---|
| 47 | * // asynchronous function that checks if a file exists
|
|---|
| 48 | * function fileExists(file, callback) {
|
|---|
| 49 | * fs.access(file, fs.constants.F_OK, (err) => {
|
|---|
| 50 | * callback(null, !err);
|
|---|
| 51 | * });
|
|---|
| 52 | * }
|
|---|
| 53 | *
|
|---|
| 54 | * // Using callbacks
|
|---|
| 55 | * async.filter(files, fileExists, function(err, results) {
|
|---|
| 56 | * if(err) {
|
|---|
| 57 | * console.log(err);
|
|---|
| 58 | * } else {
|
|---|
| 59 | * console.log(results);
|
|---|
| 60 | * // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|---|
| 61 | * // results is now an array of the existing files
|
|---|
| 62 | * }
|
|---|
| 63 | * });
|
|---|
| 64 | *
|
|---|
| 65 | * // Using Promises
|
|---|
| 66 | * async.filter(files, fileExists)
|
|---|
| 67 | * .then(results => {
|
|---|
| 68 | * console.log(results);
|
|---|
| 69 | * // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|---|
| 70 | * // results is now an array of the existing files
|
|---|
| 71 | * }).catch(err => {
|
|---|
| 72 | * console.log(err);
|
|---|
| 73 | * });
|
|---|
| 74 | *
|
|---|
| 75 | * // Using async/await
|
|---|
| 76 | * async () => {
|
|---|
| 77 | * try {
|
|---|
| 78 | * let results = await async.filter(files, fileExists);
|
|---|
| 79 | * console.log(results);
|
|---|
| 80 | * // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|---|
| 81 | * // results is now an array of the existing files
|
|---|
| 82 | * }
|
|---|
| 83 | * catch (err) {
|
|---|
| 84 | * console.log(err);
|
|---|
| 85 | * }
|
|---|
| 86 | * }
|
|---|
| 87 | *
|
|---|
| 88 | */
|
|---|
| 89 | function filter(coll, iteratee, callback) {
|
|---|
| 90 | return (0, _filter3.default)(_eachOf2.default, coll, iteratee, callback);
|
|---|
| 91 | }
|
|---|
| 92 | exports.default = (0, _awaitify2.default)(filter, 3);
|
|---|
| 93 | module.exports = exports.default; |
|---|