detect.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createTester = require('./internal/createTester.js');
  6. var _createTester2 = _interopRequireDefault(_createTester);
  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 the first value in `coll` that passes an async truth test. The
  14. * `iteratee` is applied in parallel, meaning the first iteratee to return
  15. * `true` will fire the detect `callback` with that result. That means the
  16. * result might not be the first item in the original `coll` (in terms of order)
  17. * that passes the test.
  18. * If order within the original `coll` is important, then look at
  19. * [`detectSeries`]{@link module:Collections.detectSeries}.
  20. *
  21. * @name detect
  22. * @static
  23. * @memberOf module:Collections
  24. * @method
  25. * @alias find
  26. * @category Collections
  27. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  28. * @param {AsyncFunction} iteratee - A truth test to apply to each item in `coll`.
  29. * The iteratee must complete with a boolean value as its result.
  30. * Invoked with (item, callback).
  31. * @param {Function} [callback] - A callback which is called as soon as any
  32. * iteratee returns `true`, or after all the `iteratee` functions have finished.
  33. * Result will be the first item in the array that passes the truth test
  34. * (iteratee) or the value `undefined` if none passed. Invoked with
  35. * (err, result).
  36. * @returns {Promise} a promise, if a callback is omitted
  37. * @example
  38. *
  39. * // dir1 is a directory that contains file1.txt, file2.txt
  40. * // dir2 is a directory that contains file3.txt, file4.txt
  41. * // dir3 is a directory that contains file5.txt
  42. *
  43. * // asynchronous function that checks if a file exists
  44. * function fileExists(file, callback) {
  45. * fs.access(file, fs.constants.F_OK, (err) => {
  46. * callback(null, !err);
  47. * });
  48. * }
  49. *
  50. * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists,
  51. * function(err, result) {
  52. * console.log(result);
  53. * // dir1/file1.txt
  54. * // result now equals the first file in the list that exists
  55. * }
  56. *);
  57. *
  58. * // Using Promises
  59. * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists)
  60. * .then(result => {
  61. * console.log(result);
  62. * // dir1/file1.txt
  63. * // result now equals the first file in the list that exists
  64. * }).catch(err => {
  65. * console.log(err);
  66. * });
  67. *
  68. * // Using async/await
  69. * async () => {
  70. * try {
  71. * let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists);
  72. * console.log(result);
  73. * // dir1/file1.txt
  74. * // result now equals the file in the list that exists
  75. * }
  76. * catch (err) {
  77. * console.log(err);
  78. * }
  79. * }
  80. *
  81. */
  82. function detect(coll, iteratee, callback) {
  83. return (0, _createTester2.default)(bool => bool, (res, item) => item)(_eachOf2.default, coll, iteratee, callback);
  84. }
  85. exports.default = (0, _awaitify2.default)(detect, 3);
  86. module.exports = exports['default'];