defs.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const none = Symbol.for('object-stream.none');
  3. const stop = Symbol.for('object-stream.stop');
  4. const finalSymbol = Symbol.for('object-stream.final');
  5. const manySymbol = Symbol.for('object-stream.many');
  6. const flushSymbol = Symbol.for('object-stream.flush');
  7. const finalValue = value => ({[finalSymbol]: 1, value});
  8. const many = values => ({[manySymbol]: 1, values});
  9. const isFinalValue = o => o && o[finalSymbol] === 1;
  10. const isMany = o => o && o[manySymbol] === 1;
  11. const isFlushable = o => o && o[flushSymbol] === 1;
  12. const getFinalValue = value => value.value;
  13. const getManyValues = value => value.values;
  14. const flushable = (write, final = null) => {
  15. const fn = final ? value => (value === none ? final() : write(value)) : write;
  16. fn[flushSymbol] = 1;
  17. return fn;
  18. };
  19. class Stop extends Error {}
  20. // old aliases
  21. const final = finalValue;
  22. module.exports.none = none;
  23. module.exports.stop = stop;
  24. module.exports.Stop = Stop;
  25. module.exports.finalSymbol = finalSymbol;
  26. module.exports.manySymbol = manySymbol;
  27. module.exports.flushSymbol = flushSymbol;
  28. module.exports.finalValue = finalValue;
  29. module.exports.many = many;
  30. module.exports.flushable = flushable;
  31. module.exports.isFinalValue = isFinalValue;
  32. module.exports.isMany = isMany;
  33. module.exports.isFlushable = isFlushable;
  34. module.exports.getFinalValue = getFinalValue;
  35. module.exports.getManyValues = getManyValues;
  36. module.exports.final = final;