1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.transformToNDJson = void 0;
|
4 | const stream_1 = require("stream");
|
5 | const js_lib_1 = require("@naturalcycles/js-lib");
|
6 |
|
7 |
|
8 |
|
9 | function transformToNDJson(opt = {}) {
|
10 | const { strict = true, separator = '\n', sortObjects = false, useFlatstr = false } = opt;
|
11 | return new stream_1.Transform({
|
12 | writableObjectMode: true,
|
13 | readableObjectMode: false,
|
14 | transform(chunk, _, cb) {
|
15 | try {
|
16 | if (sortObjects) {
|
17 | chunk = (0, js_lib_1._sortObjectDeep)(chunk);
|
18 | }
|
19 | if (useFlatstr) {
|
20 | cb(null, flatstr(JSON.stringify(chunk) + separator));
|
21 | }
|
22 | else {
|
23 | cb(null, JSON.stringify(chunk) + separator);
|
24 | }
|
25 | }
|
26 | catch (err) {
|
27 | console.error(err);
|
28 | if (strict) {
|
29 | cb(err);
|
30 | }
|
31 | else {
|
32 | cb();
|
33 | }
|
34 | }
|
35 | },
|
36 | });
|
37 | }
|
38 | exports.transformToNDJson = transformToNDJson;
|
39 |
|
40 |
|
41 |
|
42 | function flatstr(s) {
|
43 |
|
44 | s | 0;
|
45 | return s;
|
46 | }
|