serialize-chunks.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict'
  2. var assert = require('assert')
  3. var codes = require('../character/codes.js')
  4. var values = require('../character/values.js')
  5. var fromCharCode = require('../constant/from-char-code.js')
  6. function _interopDefaultLegacy(e) {
  7. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  8. }
  9. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  10. function serializeChunks(chunks) {
  11. var index = -1
  12. var result = []
  13. var chunk
  14. var value
  15. var atTab
  16. while (++index < chunks.length) {
  17. chunk = chunks[index]
  18. if (typeof chunk === 'string') {
  19. value = chunk
  20. } else if (chunk === codes.carriageReturn) {
  21. value = values.cr
  22. } else if (chunk === codes.lineFeed) {
  23. value = values.lf
  24. } else if (chunk === codes.carriageReturnLineFeed) {
  25. value = values.cr + values.lf
  26. } else if (chunk === codes.horizontalTab) {
  27. value = values.ht
  28. } else if (chunk === codes.virtualSpace) {
  29. if (atTab) continue
  30. value = values.space
  31. } else {
  32. assert__default['default'].equal(
  33. typeof chunk,
  34. 'number',
  35. 'expected number'
  36. )
  37. // Currently only replacement character.
  38. value = fromCharCode(chunk)
  39. }
  40. atTab = chunk === codes.horizontalTab
  41. result.push(value)
  42. }
  43. return result.join('')
  44. }
  45. module.exports = serializeChunks