-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsort.ts
41 lines (39 loc) · 1.07 KB
/
sort.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
import fs from 'fs';
import { Algorithm } from './enum';
import { mergeSort } from './merge_sort';
import { radixSort } from './radix_sort';
import { quicksort } from './quick_sort';
import { heapSort } from './heap_sort';
import { randomSort } from './random_sort';
function unixSort(
filename: string,
unique: boolean = false,
algorithm?: Algorithm
): string[] {
if (!fs.existsSync(filename)) {
throw new Error('File does not exists');
}
const fileContents = fs.readFileSync(filename).toString().trimEnd();
let arr = fileContents.split(/\n|\r|\r\n/);
if (unique) {
arr = [...new Set(arr)];
}
if (algorithm !== undefined) {
switch (algorithm) {
case Algorithm.RADIX_SORT:
return radixSort(arr);
case Algorithm.MERGE_SORT:
return mergeSort(arr);
case Algorithm.QUICK_SORT:
return quicksort(arr);
case Algorithm.HEAP_SORT:
return heapSort(arr);
case Algorithm.RANDOM_SORT:
return randomSort(arr);
default:
return arr.sort();
}
}
return arr.sort();
}
export { unixSort };