-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
executable file
·165 lines (160 loc) · 4.82 KB
/
extension.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as vscode from "vscode";
import * as _ from "lodash";
import * as math from "mathjs";
declare const global: any;
global._ = _;
global.math = math;
global.n2w = (function() {
const n = (function() {
let o = [
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine"
];
let t = [
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
];
let n = [
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
].map(v => o.map(x => v + (x && " " + x)));
return _([o, t, ...n])
.flatten()
.value();
})();
const fg = [
"",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion",
"Quintillion",
"Sextillion",
"Septillion",
"Octillion",
"Nonillion",
"Decillion",
"Undecillion",
"Duodecillion",
"Tredecillion",
"Quattuordecillion",
"Quinquadecillion",
"Sedecillion",
"Septendecillion",
"Octodecillion",
"Novendecillion",
"Vigintillion",
"Unvigintillion",
"Duovigintillion",
"Tresvigintillion",
"Quattuorvigintillion",
"Quinquavigintillion",
"Sesvigintillion",
"Septemvigintillion",
"Octovigintillion",
"Novemvigintillion",
"Trigintillion",
"Untrigintillion",
"Duotrigintillion",
"Trestrigintillion",
"Quattuortrigintillion",
"Quinquatrigintillion",
"Sestrigintillion",
"Septentrigintillion",
"Octotrigintillion",
"Noventrigintillion",
"Quadragintillion",
"Unquadragintillion",
"Duoquadragintillion",
"Tresquadragintillion",
"Quattorquadragintillion",
"Quinquaquadragintillion",
"Sesquadragintillion"
];
const d3 = (x, y = ("000" + x).slice(-3)) =>
`${+y[0] ? n[y[0]] + " Hundred" : ""} ${
+y[1] + +y[2] ? n[+(y[1] + y[2])] : ""
}`.trim();
return (x, y = ((x += ""), "0".repeat(3 - (x.length % 3)) + x)) =>
y
.match(/.{3}/g)
.map((v, i, k) =>
(d3(v) && d3(v) + " " + fg[k.length - i - 1]).trim()
)
.join(" ")
.trim();
})();
export function activate(context: vscode.ExtensionContext) {
const js2text = vscode.commands.registerTextEditorCommand(
"extension.js2text",
(textEditor, edit) => {
const selectionsTransformed = textEditor.selections.map(
selection => {
const text = textEditor.document.getText(selection);
let copyOfEval = eval;
let replacementText = copyOfEval(text) as any;
if (typeof replacementText != "string") {
replacementText = JSON.stringify(replacementText);
}
return {
selection,
replacementText
};
}
);
textEditor.edit(editBuilder => {
selectionsTransformed.forEach(x => {
editBuilder.replace(x.selection, x.replacementText);
});
});
}
);
context.subscriptions.push(js2text);
const mathJs2text = vscode.commands.registerTextEditorCommand(
"extension.mathJs2text",
(textEditor, edit) => {
const selectionsTransformed = textEditor.selections.map(
selection => {
const text = textEditor.document.getText(selection);
let replacementText = math.eval(text) as any;
if (typeof replacementText != "string") {
replacementText = JSON.stringify(replacementText);
}
return {
selection,
replacementText
};
}
);
textEditor.edit(editBuilder => {
selectionsTransformed.forEach(x => {
editBuilder.replace(x.selection, x.replacementText);
});
});
}
);
context.subscriptions.push(mathJs2text);
}