Explorar o código

Changed parser to return {key, value}.

This is done to avoid sending `null` to a stream.
Eugene Lazutkin %!s(int64=3) %!d(string=hai) anos
pai
achega
f63a2cbed2
Modificáronse 3 ficheiros con 11 adicións e 7 borrados
  1. 6 3
      src/jsonl/parser.js
  2. 3 2
      tests/test-jsonl-parser.mjs
  3. 2 2
      tests/test-jsonl-stringer.mjs

+ 6 - 3
src/jsonl/parser.js

@@ -7,11 +7,14 @@ const lines = require('../utils/lines');
 
 const parse = reviver => string => JSON.parse(string, reviver);
 
-const parser = options =>
-  asStream(
-    gen(fixUtf8Stream(), lines(), parse(options && options.reviver)),
+const parser = options => {
+  const reviver = options && options.reviver;
+  let counter = 0;
+  return asStream(
+    gen(fixUtf8Stream(), lines(), string => ({key: counter++, value: JSON.parse(string, reviver)})),
     Object.assign({writableObjectMode: false, readableObjectMode: true}, options)
   );
+};
 
 parser.parse = parse;
 

+ 3 - 2
tests/test-jsonl-parser.mjs

@@ -34,7 +34,7 @@ const roundtrip = (t, resolve, len, quant) => {
       new Writable({
         objectMode: true,
         write(chunk, _, callback) {
-          result.push(chunk);
+          result.push(chunk.value);
           callback(null);
         },
         final(callback) {
@@ -112,7 +112,8 @@ test.asPromise('jsonl parser: read file', (t, resolve) => {
     .pipe(
       new Writable({
         objectMode: true,
-        write(_1, _2, callback) {
+        write(chunk, _, callback) {
+          t.equal(count, chunk.key);
           ++count;
           callback(null);
         },

+ 2 - 2
tests/test-jsonl-stringer.mjs

@@ -33,7 +33,7 @@ test.asPromise('jsonl stringer: smoke test', (t, resolve) => {
         writableObjectMode: true,
         readableObjectMode: true,
         transform(chunk, _, callback) {
-          this.push(chunk);
+          this.push(chunk.value);
           callback(null);
         }
       })
@@ -80,7 +80,7 @@ test.asPromise('jsonl stringer: multiple', (t, resolve) => {
         writableObjectMode: true,
         readableObjectMode: true,
         transform(chunk, _, callback) {
-          this.push(chunk);
+          this.push(chunk.value);
           callback(null);
         }
       })