-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
149 lines (133 loc) · 3.65 KB
/
utils.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
145
146
147
148
149
import * as csv from "csv";
import pAll from "p-all";
const SAMPLE_SIZE = 50; // random rows to pick
export type Row = Record<string, any>;
const isValidValue = (val: string) =>
val && val.replace(/[\s]/g, "").length > 0;
const getRandomItems = (arr: any[], n: number) => {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len) return arr;
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
};
// extract the most present value for a given list of records and a given key
export const topKey = (arr: Row[], key: string): string | undefined => {
if (arr.length === 1) {
return arr[0][key];
}
const totals = Object.entries(
arr.filter(Boolean).reduce((a, rec) => {
if (!a[rec[key]]) {
a[rec[key]] = 0;
}
a[rec[key]] += 1;
return a;
}, {})
);
totals
.sort((a, b) => {
return a[1] - b[1];
})
.reverse();
if (totals.length && totals[0].length) {
return totals[0][0];
}
};
export const wait = (args: any) =>
new Promise((resolve) => setTimeout(() => resolve(args), 1));
export const median = (arr: number[]) => {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
// dummy delimiter detector
export const getDelimiter = async (readStream: Buffer) => {
let delimiter = ";";
const firstRow = await getCsvRow(readStream, { delimiter }, 0);
if (firstRow.length === 1) {
// row is not parsed correctly, fallback to `,`
// todo: make it better, this prevent using single-column CSVs
delimiter = ",";
}
return delimiter;
};
export const uniq = (arr: any[]) => Array.from(new Set(arr));
export // get single row from a csv
const getCsvRow = (
readStream: Buffer,
options: Record<string, any>,
index: number
): Promise<Row[]> => {
const parse = csv.parse(options);
let lines = 0;
return new Promise((resolve, reject) => {
parse.on("error", (e: Error) => {
console.log(e);
reject(e);
throw e;
});
parse.on("data", (row: any) => {
if (lines === index) {
resolve(row);
parse.end();
}
//records.push(row);
lines += 1;
});
parse.write(readStream);
parse.end();
});
};
export // get random rows from a csv
const getRandomRows = (
readStream: Buffer,
options: Record<string, any>,
sampleSize: number = SAMPLE_SIZE
): Promise<Row[]> => {
const parse = csv.parse(options);
let lines = 0;
return new Promise((resolve, reject) => {
parse.on("error", (e: Error) => {
console.log(e);
reject(e);
throw e;
});
parse.on("end", async () => {
const randomIndexes = process.env.TEST
? Array.from({ length: sampleSize }, (_, v) => v)
: getRandomItems(
Array.from({ length: lines }, (_, v) => v),
sampleSize
);
const randomRecords = await pAll(
randomIndexes.map(
(index) => () => getCsvRow(readStream, options, index)
),
{ concurrency: 1 }
);
resolve(randomRecords);
});
parse.on("data", (row: any) => {
lines += 1;
});
parse.write(readStream);
parse.end();
});
};
// return unique values for each column
export const getColumnsSamples = (rows: Row[]) => {
const firstRecord = rows.length && rows[0];
if (firstRecord) {
return Object.keys(firstRecord).map((key) => ({
name: key,
values: uniq(rows.map((rec) => rec[key]).filter(isValidValue)),
}));
}
return [];
};