-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto.ts
144 lines (133 loc) · 2.95 KB
/
to.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const closeFile = async (rid?: number) => {
if (rid) {
try {
await Deno.close(rid);
} catch (e) {
// do nothing
}
}
return undefined;
};
interface Stream<T = any> {
iterable: AsyncIterableIterator<T>;
rid?: number;
}
export const toArray = async <T = any>({ iterable, rid }: Stream<T>) => {
let r: T[] = [];
for await (const d of iterable) {
r.push(d);
}
await closeFile(rid);
return r;
};
export const find = async <T = any>(
func: (d: T) => boolean,
) => {
return async ({ iterable, rid }: Stream<T>) => {
for await (const d of iterable) {
if (func(d)) {
await closeFile(rid);
return d;
}
}
await closeFile(rid);
return undefined;
};
};
export const reduce = <A = any, B = any>(
func: (r: B, d: A, i: number) => B,
start: B,
) => {
return async ({ iterable, rid }: Stream<A>) => {
let i = -1;
let r = start;
for await (const d of iterable) {
i++;
r = func(r, d, i);
}
await closeFile(rid);
return r;
};
};
const log = (d: any) => {
try {
console.log(d);
} catch {
// ignore "broken pipe" error
// when using "... | head -10" for example
return;
}
};
const encode = (d: string) => {
const encoder = new TextEncoder();
return encoder.encode(d);
};
export const toNdjsonStdout = async <T = any>({ iterable, rid }: Stream<T>) => {
for await (const d of iterable) {
log(JSON.stringify(d));
}
await closeFile(rid);
};
export const toNdjsonFile = async <T = any>(
{ iterable, rid }: Stream<T>,
path: string,
) => {
let created = false;
for await (const d of iterable) {
if (!created) {
created = true;
await Deno.writeFile(path, encode(JSON.stringify(d)));
} else {
await Deno.writeFile(path, encode("\n" + JSON.stringify(d)), {
create: false,
append: true,
});
}
}
await closeFile(rid);
};
const isString = (d: any): d is string => d === String(d);
const toDsvLine = (head: string[], delimiter: string, d: any) =>
head
.reduce((r: any[], key: string) => {
// @ts-ignore
const val = d[key];
r.push(isString(val) ? `"${val}"` : val);
return r;
}, [])
.join(delimiter);
export const toDsvStdout = async <T = any>(
{ iterable, rid }: Stream<T>,
delimiter = ",",
) => {
let head;
for await (const d of iterable) {
if (!head) {
head = Object.keys(d);
log(head.join(delimiter));
}
if (d) {
log(toDsvLine(head, delimiter, d));
}
}
await closeFile(rid);
};
export const toDsvFile = async <T = any>(
{ iterable, rid }: Stream<T>,
path: string,
delimiter = ",",
) => {
let head;
for await (const d of iterable) {
if (!head) {
head = Object.keys(d);
await Deno.writeFile(path, encode(head.join(delimiter)));
}
if (d) {
await Deno.writeFile(path, encode("\n" + toDsvLine(head, delimiter, d)), {
append: true,
});
}
}
await closeFile(rid);
};