serialize-chunks.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export default serializeChunks
  2. import assert from 'assert'
  3. import codes from '../character/codes.mjs'
  4. import values from '../character/values.mjs'
  5. import fromCharCode from '../constant/from-char-code.mjs'
  6. function serializeChunks(chunks) {
  7. var index = -1
  8. var result = []
  9. var chunk
  10. var value
  11. var atTab
  12. while (++index < chunks.length) {
  13. chunk = chunks[index]
  14. if (typeof chunk === 'string') {
  15. value = chunk
  16. } else if (chunk === codes.carriageReturn) {
  17. value = values.cr
  18. } else if (chunk === codes.lineFeed) {
  19. value = values.lf
  20. } else if (chunk === codes.carriageReturnLineFeed) {
  21. value = values.cr + values.lf
  22. } else if (chunk === codes.horizontalTab) {
  23. value = values.ht
  24. } else if (chunk === codes.virtualSpace) {
  25. if (atTab) continue
  26. value = values.space
  27. } else {
  28. assert.equal(typeof chunk, 'number', 'expected number')
  29. // Currently only replacement character.
  30. value = fromCharCode(chunk)
  31. }
  32. atTab = chunk === codes.horizontalTab
  33. result.push(value)
  34. }
  35. return result.join('')
  36. }