filter.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _filter;
  6. var _arrayMap = require('lodash/_arrayMap');
  7. var _arrayMap2 = _interopRequireDefault(_arrayMap);
  8. var _isArrayLike = require('lodash/isArrayLike');
  9. var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
  10. var _baseProperty = require('lodash/_baseProperty');
  11. var _baseProperty2 = _interopRequireDefault(_baseProperty);
  12. var _noop = require('lodash/noop');
  13. var _noop2 = _interopRequireDefault(_noop);
  14. var _wrapAsync = require('./wrapAsync');
  15. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. function filterArray(eachfn, arr, iteratee, callback) {
  18. var truthValues = new Array(arr.length);
  19. eachfn(arr, function (x, index, callback) {
  20. iteratee(x, function (err, v) {
  21. truthValues[index] = !!v;
  22. callback(err);
  23. });
  24. }, function (err) {
  25. if (err) return callback(err);
  26. var results = [];
  27. for (var i = 0; i < arr.length; i++) {
  28. if (truthValues[i]) results.push(arr[i]);
  29. }
  30. callback(null, results);
  31. });
  32. }
  33. function filterGeneric(eachfn, coll, iteratee, callback) {
  34. var results = [];
  35. eachfn(coll, function (x, index, callback) {
  36. iteratee(x, function (err, v) {
  37. if (err) {
  38. callback(err);
  39. } else {
  40. if (v) {
  41. results.push({ index: index, value: x });
  42. }
  43. callback();
  44. }
  45. });
  46. }, function (err) {
  47. if (err) {
  48. callback(err);
  49. } else {
  50. callback(null, (0, _arrayMap2.default)(results.sort(function (a, b) {
  51. return a.index - b.index;
  52. }), (0, _baseProperty2.default)('value')));
  53. }
  54. });
  55. }
  56. function _filter(eachfn, coll, iteratee, callback) {
  57. var filter = (0, _isArrayLike2.default)(coll) ? filterArray : filterGeneric;
  58. filter(eachfn, coll, (0, _wrapAsync2.default)(iteratee), callback || _noop2.default);
  59. }
  60. module.exports = exports['default'];