|
| 1 | +import "should"; |
| 2 | +import { generate } from "csv-generate"; |
| 3 | +import { parse } from "../lib/index.js"; |
| 4 | + |
| 5 | +describe("info bytes_records", function () { |
| 6 | + it("info only", async function () { |
| 7 | + const parser = parse({ |
| 8 | + info: true, |
| 9 | + }); |
| 10 | + parser.write(Buffer.from("1,2\n")); |
| 11 | + parser.write(Buffer.from("3,4\n")); |
| 12 | + parser.write(Buffer.from("5,6\n")); |
| 13 | + parser.write(Buffer.from("7,8\n")); |
| 14 | + parser.end(); |
| 15 | + const bytes = []; |
| 16 | + for await (const { info } of parser) { |
| 17 | + bytes.push(info.bytes_records); |
| 18 | + } |
| 19 | + bytes.should.eql([4, 8, 12, 16]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("info with bom, columns and from_line", async function () { |
| 23 | + const parser = parse({ |
| 24 | + columns: true, |
| 25 | + bom: true, |
| 26 | + from_line: 2, |
| 27 | + info: true, |
| 28 | + }); |
| 29 | + parser.write(Buffer.from("\ufeffa,b\n")); |
| 30 | + parser.write(Buffer.from("1,2\n")); |
| 31 | + parser.write(Buffer.from("3,4\n")); |
| 32 | + parser.write(Buffer.from("5,6\n")); |
| 33 | + parser.write(Buffer.from("7,8\n")); |
| 34 | + parser.end(); |
| 35 | + const bytes = []; |
| 36 | + for await (const { info } of parser) { |
| 37 | + bytes.push(info.bytes_records); |
| 38 | + } |
| 39 | + bytes.should.eql([15, 19, 23]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("remain constant inside fields", async function () { |
| 43 | + const parser = generate({ |
| 44 | + length: 3, |
| 45 | + seed: 1, |
| 46 | + columns: 2, |
| 47 | + fixed_size: true, |
| 48 | + }).pipe( |
| 49 | + parse({ |
| 50 | + cast: (value, info) => { |
| 51 | + return info.bytes_records; |
| 52 | + }, |
| 53 | + }), |
| 54 | + ); |
| 55 | + await Array.fromAsync(parser).should.finally.eql([ |
| 56 | + [0, 0], |
| 57 | + [18, 18], |
| 58 | + [46, 46], |
| 59 | + ]); |
| 60 | + }); |
| 61 | +}); |
0 commit comments