-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.d.ts
68 lines (57 loc) · 1.32 KB
/
index.d.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
export interface Options<Value = string> {
/**
Separator to split columns on.
@default ' '
*/
readonly separator?: string;
/**
Headers to use instead of the existing ones.
*/
readonly headers?: readonly string[];
/**
Transform elements.
Useful for being able to cleanup or change the type of elements.
*/
readonly transform?: (
element: string,
header: string,
columnIndex: number,
rowIndex: number
) => Value;
}
/**
Parse text columns, like the output of Unix commands.
@param textColumns - Text columns to parse.
@example
```
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import parseColumns from 'parse-columns';
const execFileP = promisify(childProcess.execFile);
const {stdout} = await execFileP('df', ['-kP']);
console.log(parseColumns(stdout, {
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(item);
}
return item;
}
}));
// [
// {
// Filesystem: '/dev/disk1',
// '1024-blocks': 487350400,
// Used: 467528020,
// Available: 19566380,
// Capacity: '96%',
// 'Mounted on': '/'
// },
// …
// ]
```
*/
export default function parseColumns<Value = string>(
textColumns: string,
options?: Options<Value>
): Array<Record<string, Value>>;