-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
73 lines (65 loc) · 1.69 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
export function parseNumber(value: string) {
if (value == null) {
return null;
}
try {
if (value.includes(".")) {
return parseFloat(value);
}
return parseInt(value);
} catch {
return null;
}
}
export function similarity(left: string, right: string) {
var longer = left;
var shorter = right;
if (left.length < right.length) {
longer = right;
shorter = left;
}
var longerLength = longer.length;
return (longerLength - editDistance(longer, shorter)) / longerLength;
}
export function editDistance(left: string, right: string) {
left = left.toLowerCase();
right = right.toLowerCase();
var costs: number[]= [];
for (var i = 0; i <= left.length; i++) {
var lastValue = i;
for (var j = 0; j <= right.length; j++) {
if (i == 0) {
costs[j] = j;
} else {
if (j > 0) {
var newValue = costs[j - 1];
if (left.charAt(i - 1) != right.charAt(j - 1)) {
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) {
costs[right.length] = lastValue;
}
}
return costs[right.length];
}
export function escapeUri(input: string) {
return encodeURI(input);
}
// https://en.wikipedia.org/wiki/ANSI_escape_code
export function colorError(message: string) {
return "\x1b[31m" + message + "\x1b[0m";
}
export function colorWarning(message: string) {
return "\x1b[33m" + message + "\x1b[0m";
}
export function colorQuery(message: string) {
return "\x1b[35m" + message + "\x1b[0m";
}
export function colorDebug(message: string) {
return "\x1b[36m" + message + "\x1b[0m";
}