-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalpha.ts
79 lines (66 loc) · 1.75 KB
/
alpha.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
import { getLines } from './helpers';
import commandLineArgs, { OptionDefinition } from "command-line-args";
function alpha () {
const optionDefinitions: OptionDefinition[] = [
{ name: 'pipeline', alias: 'p', type: Boolean },
]
const options = commandLineArgs(optionDefinitions);
if (options["pipeline"]) {
let tokens = getLines('Enter the tokens for alphabetical ordering (pipeline mode):')
tokens.sort((a, b) => {
let ar = getRank(a)
let br = getRank(b)
if (ar < br) {
return -1
}
if (ar > br) {
return 1
}
return a.localeCompare(b)
}).forEach(t => console.log(t))
} else {
let tokens = getLines('Enter the tokens for alphabetical ordering:')
tokens.sort().forEach(t => console.log(t))
}
}
const ruleRanks = [
isPrimaryKey,
(t) => !isPipelineToken(t),
isHookStep,
]
function getRank(token: string) {
for (const rule of ruleRanks) {
if (rule(token)) {
return ruleRanks.indexOf(rule)
}
}
return null;
}
function isPipelineToken(token: string) {
return isPrimaryKey(token) || isHookStep(token);
}
function isPrimaryKey(token: string) {
const pipelineTokens = [
"across",
"do",
"get",
"in_parallel",
"load_var",
"put",
"set_pipeline",
"task",
"try",
];
return pipelineTokens.includes(token)
}
function isHookStep(token: string) {
const pipelineTokens = [
"ensure",
"on_abort",
"on_error",
"on_failure",
"on_success",
];
return pipelineTokens.includes(token)
}
alpha()