_xreduceBy.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import _clone from "./_clone.js";
  2. import _has from "./_has.js";
  3. import _xfBase from "./_xfBase.js";
  4. var XReduceBy =
  5. /*#__PURE__*/
  6. function () {
  7. function XReduceBy(valueFn, valueAcc, keyFn, xf) {
  8. this.valueFn = valueFn;
  9. this.valueAcc = valueAcc;
  10. this.keyFn = keyFn;
  11. this.xf = xf;
  12. this.inputs = {};
  13. }
  14. XReduceBy.prototype['@@transducer/init'] = _xfBase.init;
  15. XReduceBy.prototype['@@transducer/result'] = function (result) {
  16. var key;
  17. for (key in this.inputs) {
  18. if (_has(key, this.inputs)) {
  19. result = this.xf['@@transducer/step'](result, this.inputs[key]);
  20. if (result['@@transducer/reduced']) {
  21. result = result['@@transducer/value'];
  22. break;
  23. }
  24. }
  25. }
  26. this.inputs = null;
  27. return this.xf['@@transducer/result'](result);
  28. };
  29. XReduceBy.prototype['@@transducer/step'] = function (result, input) {
  30. var key = this.keyFn(input);
  31. this.inputs[key] = this.inputs[key] || [key, _clone(this.valueAcc, false)];
  32. this.inputs[key][1] = this.valueFn(this.inputs[key][1], input);
  33. return result;
  34. };
  35. return XReduceBy;
  36. }();
  37. export default function _xreduceBy(valueFn, valueAcc, keyFn) {
  38. return function (xf) {
  39. return new XReduceBy(valueFn, valueAcc, keyFn, xf);
  40. };
  41. }