defs.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 = {
  23. none,
  24. stop,
  25. Stop,
  26. finalSymbol,
  27. manySymbol,
  28. flushSymbol,
  29. finalValue,
  30. many,
  31. flushable,
  32. isFinalValue,
  33. isMany,
  34. isFlushable,
  35. getFinalValue,
  36. getManyValues,
  37. final
  38. };