| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 'use strict';
- import test from 'tape-six';
- import {Writable, Transform} from 'stream';
- import {readString, writeToArray} from './helpers.mjs';
- import parserStream from '../src/jsonl/parserStream.js';
- import stringerStream from '../src/jsonl/stringerStream.js';
- test.asPromise('jsonl stringerStream: smoke test', (t, resolve) => {
- const pattern = {
- a: [[[]]],
- b: {a: 1},
- c: {a: 1, b: 2},
- d: [true, 1, "'x\"y'", null, false, true, {}, [], ''],
- e: 1,
- f: '',
- g: true,
- h: false,
- i: null,
- j: [],
- k: {}
- },
- string = JSON.stringify(pattern);
- let buffer = '';
- readString(string)
- .pipe(parserStream())
- .pipe(
- new Transform({
- writableObjectMode: true,
- readableObjectMode: true,
- transform(chunk, _, callback) {
- this.push(chunk.value);
- callback(null);
- }
- })
- )
- .pipe(stringerStream())
- .pipe(
- new Writable({
- write(chunk, _, callback) {
- buffer += chunk;
- callback(null);
- },
- final(callback) {
- t.deepEqual(string, buffer);
- resolve();
- callback(null);
- }
- })
- );
- });
- test.asPromise('jsonl stringerStream: multiple', (t, resolve) => {
- const pattern = {
- a: [[[]]],
- b: {a: 1},
- c: {a: 1, b: 2},
- d: [true, 1, "'x\"y'", null, false, true, {}, [], ''],
- e: 1,
- f: '',
- g: true,
- h: false,
- i: null,
- j: [],
- k: {}
- };
- let string = JSON.stringify(pattern),
- buffer = '';
- string = string + '\n' + string + '\n' + string;
- readString(string + '\n')
- .pipe(parserStream())
- .pipe(
- new Transform({
- writableObjectMode: true,
- readableObjectMode: true,
- transform(chunk, _, callback) {
- this.push(chunk.value);
- callback(null);
- }
- })
- )
- .pipe(stringerStream())
- .pipe(
- new Writable({
- write(chunk, _, callback) {
- buffer += chunk;
- callback(null);
- },
- final(callback) {
- t.deepEqual(string, buffer);
- resolve();
- callback(null);
- }
- })
- );
- });
- test.asPromise('jsonl stringerStream: custom separators - one value', (t, resolve) => {
- const output = [],
- stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
- pipeline = stringer.pipe(writeToArray(output));
- pipeline.on('finish', () => {
- t.equal(output.join(''), '[1]');
- resolve();
- });
- stringer.end(1);
- });
- test.asPromise('jsonl stringerStream: custom separators - two value', (t, resolve) => {
- const output = [],
- stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
- pipeline = stringer.pipe(writeToArray(output));
- pipeline.on('finish', () => {
- t.equal(output.join(''), '[2,1]');
- resolve();
- });
- stringer.write(2);
- stringer.end(1);
- });
- test.asPromise('jsonl stringerStream: custom separators - no value', (t, resolve) => {
- const output = [],
- stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
- pipeline = stringer.pipe(writeToArray(output));
- pipeline.on('finish', () => {
- t.equal(output.join(''), '{}');
- resolve();
- });
- stringer.end();
- });
- test.asPromise('jsonl stringerStream: custom separators - no value (default)', (t, resolve) => {
- const output = [],
- stringer = stringerStream({prefix: '[', suffix: ']', separator: ','}),
- pipeline = stringer.pipe(writeToArray(output));
- pipeline.on('finish', () => {
- t.equal(output.join(''), '[]');
- resolve();
- });
- stringer.end();
- });
|