select.js 2.8 KB

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