1 | import { Transform } from 'stream'
|
2 | import { _sortObjectDeep } from '@naturalcycles/js-lib'
|
3 | import { TransformTyped } from '../stream.model'
|
4 |
|
5 | export interface TransformToNDJsonOptions {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 | strict?: boolean
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | sortObjects?: boolean
|
19 |
|
20 | |
21 |
|
22 |
|
23 | separator?: string
|
24 |
|
25 | |
26 |
|
27 |
|
28 | useFlatstr?: boolean
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function transformToNDJson<IN = any>(
|
35 | opt: TransformToNDJsonOptions = {},
|
36 | ): TransformTyped<IN, string> {
|
37 | const { strict = true, separator = '\n', sortObjects = false, useFlatstr = false } = opt
|
38 |
|
39 | return new Transform({
|
40 | writableObjectMode: true,
|
41 | readableObjectMode: false,
|
42 | transform(chunk: IN, _, cb) {
|
43 | try {
|
44 | if (sortObjects) {
|
45 | chunk = _sortObjectDeep(chunk as any)
|
46 | }
|
47 |
|
48 | if (useFlatstr) {
|
49 | cb(null, flatstr(JSON.stringify(chunk) + separator))
|
50 | } else {
|
51 | cb(null, JSON.stringify(chunk) + separator)
|
52 | }
|
53 | } catch (err) {
|
54 | console.error(err)
|
55 |
|
56 | if (strict) {
|
57 | cb(err as Error)
|
58 | } else {
|
59 | cb()
|
60 | }
|
61 | }
|
62 | },
|
63 | })
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function flatstr(s: any): string {
|
70 |
|
71 | s | 0
|
72 | return s
|
73 | }
|