Jelajahi Sumber

Added utilities to fix UTF-8 streams and read a stream by lines.

Eugene Lazutkin 3 tahun lalu
induk
melakukan
7ea3008350
2 mengubah file dengan 55 tambahan dan 0 penghapusan
  1. 31 0
      src/utils/fixUtf8Stream.js
  2. 24 0
      src/utils/lines.js

+ 31 - 0
src/utils/fixUtf8Stream.js

@@ -0,0 +1,31 @@
+'use strict';
+
+const {StringDecoder} = require('string_decoder');
+
+const {none, flushable} = require('../defs');
+
+const fixUtf8Stream = () => {
+  const stringDecoder = new StringDecoder();
+  let input = '';
+  return flushable(chunk => {
+    if (chunk === none) {
+      const result = input + stringDecoder.end();
+      input = '';
+      return result;
+    }
+    if (typeof chunk == 'string') {
+      if (!input) return chunk;
+      const result = input + chunk;
+      input = '';
+      return result;
+    }
+    if (chunk instanceof Buffer) {
+      const result = input + stringDecoder.write(chunk);
+      input = '';
+      return result;
+    }
+    throw new TypeError('Expected a string or a Buffer');
+  });
+};
+
+module.exports = fixUtf8Stream;

+ 24 - 0
src/utils/lines.js

@@ -0,0 +1,24 @@
+'use strict';
+
+const {none, flushable} = require('../defs');
+
+const lines = () => {
+  let rest = '';
+  return flushable(function* (value) {
+    if (value === none) {
+      if (!rest) return;
+      const result = rest;
+      rest = '';
+      yield result;
+      return;
+    }
+    const lines = value.split('\n');
+    rest += lines[0];
+    if (lines.length < 2) return;
+    lines[0] = rest;
+    rest = lines.pop();
+    yield* lines;
+  });
+};
+
+module.exports = lines;