-
Notifications
You must be signed in to change notification settings - Fork 20
/
descriptiveCounter.ts
65 lines (60 loc) · 2.06 KB
/
descriptiveCounter.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
function counterDecode(inputId: string, outputId: string) {
var custom_indent: string = (<HTMLInputElement>document.getElementById(inputId)).value;
var result: string = descriptiveCounter(custom_indent);
document.getElementById(outputId).innerHTML = result;
}
export function descriptiveCounter(input: string): string {
input = input.replace(/\\t/g, " ");
input = input.replace(/\\r/g, "\r");
input = input.replace(/\\n/g, "\n");
var tokens: Array<string> = input.split("");
var result = "";
var repeatedCharCount = 0;
for (var i = 0; i < tokens.length; i++) {
var char = input.substr(i, 1);
if (char == input.substr(i + 1, 1)) {
repeatedCharCount++;
} else {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
break;
case "\n":
char = "\\n";
break;
case "\r":
char = "\\r";
break;
default:
char = "'" + char + "'";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
if (repeatedCharCount > 0) {
char += "s";
}
result += getCountText(repeatedCharCount, char);
repeatedCharCount = 0;
}
}
if (result.length < 0) {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
result = getCountText(repeatedCharCount, char);
}
result = result.replace(/^ & /, "")
return result;
}
function getCountText(count: number, char: string): string {
const dict = ["one", "two", "three", "four", "five", "six", "seven", "eight", "many"];
const ampersand = " & ";
return ampersand + dict[count] + " " + char;
}