-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcut.ts
144 lines (130 loc) · 3.28 KB
/
cut.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
import fs from 'fs';
import readline from 'readline';
function processLine(text: string, delimiter: string = '\t'): string[] {
return text.split(delimiter);
}
function getElement(arr: string[], index: number): string | null {
if (arr.length > index) {
return arr[index];
}
return null;
}
function getElements(
arr: string[],
indices: number[],
delimiter: string = '\t'
): string {
const text: string[] = [];
let i = 0;
for (i; i < indices.length; i++) {
const elem = getElement(arr, indices[i]);
if (elem !== null) {
text.push(elem);
}
}
return text.join(delimiter);
}
async function handleFieldsCommand(
arg: string,
filename: string | number,
delimiter: string = '\t'
): Promise<string> {
const fields: string[] = arg.split(',');
const args: number[] = [];
fields.forEach((field) => {
args.push(Math.max(0, parseInt(field) - 1));
});
return new Promise((res, rej) => {
let fileStream;
if (typeof filename === 'number') {
fileStream = process.stdin;
} else {
try {
fileStream = fs.createReadStream(filename);
} catch (err) {
rej(err);
return;
}
}
if (fileStream === undefined) {
rej('Invalid stream');
return;
}
const rl = readline.createInterface({
input: fileStream,
output: undefined
});
let output = '';
rl.on('line', (line: string) => {
output +=
getElements(processLine(line, delimiter), args, delimiter) + '\n';
});
rl.on('close', () => {
res(output);
});
});
}
async function processCommand(
command: string,
filename: string | number,
delimiter: string = '\t'
): Promise<string> {
const commandType = command.substring(0, 2);
const commandArg = command.substring(2);
switch (commandType) {
case '-f':
return handleFieldsCommand(commandArg, filename, delimiter);
default:
printUsage(true);
return '';
}
}
function printUsage(exit: boolean = false) {
const text = `
Usage
node cut.js [commandType] <filename>
`;
console.log(text);
if (exit) {
process.exit(1);
}
}
function parseFieldListString(str: string) {
return str.replaceAll('"', '').replaceAll(' ', ',');
}
function unixCut(argv: string[] = process.argv): Promise<string> {
let delimiter = '\t';
let command = null;
let filename = null;
for (let i = 2; i < argv.length; i++) {
if (argv[i].indexOf('-d') >= 0) {
delimiter = argv[i].substring(2);
} else if (argv[i].substring(0, 2) === '-f') {
// If the field list is whitespace separated list of fields
if (argv[i].length === 2) {
command = argv[i];
command += parseFieldListString(argv[i + 1]);
i++;
}
// If the field list is a comma separated list of fields
// This also covers the normal case when only 1 field is present
else {
command = argv[i];
}
} else {
filename = argv[i];
}
}
if (command === null) {
console.error('Invalid command %s', command);
process.exit(1);
}
if (filename === null || filename === '-') {
filename = 0;
} else if (!fs.existsSync(filename)) {
console.error('File does not exists!');
process.exit(1);
}
return processCommand(command, filename, delimiter);
}
export { unixCut };