-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
401 lines (363 loc) · 14.1 KB
/
index.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
'use strict';
const alfy = require('alfy');
const osLocale = require('os-locale');
const newList = []; // used in updateList()
const query = [];
const output = [];
const promises = [];
const api = 'https://open.er-api.com/v6';
const currencies = ["AED", "AUD", "BGN", "BRL", "CAD", "CHF", "CNY", "COP", "CZK", "DKK", "EUR", "GBP", "HKD", "HRK", "HUF", "IDR", "ILS", "INR", "ISK", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PLN", "RON", "RSD", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "UAH", "USD", "VND", "ZAR"];
const favoriteCurrency = alfy.cache.get('favoriteCurrency'); // this is where custom list is stored
const lastUpdate = new Date(alfy.cache.get('updateDate'));
const nextUpdate = new Date(alfy.cache.get('nextUpdate'));
const l = alfy.cache.get('locale') || 'en-GB';
const locale = l.replace('_', '-');
const baseCurrency = alfy.cache.get('baseCurrency');
const q = alfy.input.toUpperCase().replace('$', 'USD').replace('€', 'EUR').replace('£', 'GBP').replace('¥', 'JPY').replace('₩', 'KRW').replace('₽', 'RUB').replace('₹', 'INR').split(/(\d*[.,]?\d+)([A-z]{1,3})/);
q.forEach(item => item.split(" ").filter(item => item.length > 0).forEach(item => query.push(item)));
function getOSLocale() {
return osLocale().then(locale => alfy.cache.set('locale', locale));
}
function cacheRates(data) {
const rates = data['rates'];
Object.keys(rates).forEach(cur => alfy.cache.set(cur, rates[cur]));
const lastUpdateTime = new Date(data['time_last_update_utc']);
const nextUpdateTime = new Date(data['time_next_update_utc']);
alfy.cache.set('nextUpdate', nextUpdateTime);
alfy.cache.set('updateDate', lastUpdateTime);
alfy.cache.set('baseCurrency', data['base_code']);
}
function updateRates(base) {
return alfy.fetch(`${api}/latest/${base}`).then(cacheRates);
}
function addBaseOutput() {
/*
Added Base Output For New Feature
Which lead users to customize their favorite currency list
*/
// 'curcon LIST' to add favorite currency list
output.push({
title: 'Add a currency to favorite list',
subtitle: `You can customize your favorite currency list`,
arg: 'LIST ',
autocomplete: 'LIST ',
icon: {
path: 'settings.png'
}
});
// 'curcon RMVE' to remove specific currency from the list
output.push({
title: 'Remove a currency from favorite list',
subtitle: `Remove a currency from your favorite currency list`,
arg: 'RMVE ',
autocomplete: 'RMVE ',
icon: {
path: 'settings.png'
}
});
// 'curcon REST' to reset favorite currency list
output.push({
title: 'Reset custom favorite currency list',
subtitle: `Reset your favorite currency list`,
arg: 'REST ',
autocomplete: 'REST ',
icon: {
path: 'settings.png'
}
});
output.push({
title: 'Set/Update base currency',
subtitle: `current base currency is: ${baseCurrency}`,
arg: 'BASE ',
autocomplete: 'BASE ',
icon: {
path: 'settings.png'
}
});
output.push({
title: `Type amount to see current rates for ${baseCurrency}`,
subtitle: `rates last updated: ${lastUpdate.toLocaleString(locale)}, next update: ${nextUpdate.toLocaleString(locale)}`,
arg: '1',
autocomplete: '1',
icon: {
path: `flags/${baseCurrency}.png`
}
});
}
function addSetBaseCurrencyListOutput(currency) {
output.push({
title: `Set base currency to: ${currency}`,
icon: {
path: `flags/${currency}.png`
},
arg: `BASE ${currency}`
})
}
function addUpdateRatesOutput(currency) {
output.push({
title: `Set ${currency} as base and update rates`,
icon: {
path: `flags/${currency}.png`
},
arg: ''
})
}
function addCurrencyListOutput(multiplier, currency) {
if (currency !== baseCurrency) {
const rate = alfy.cache.get(currency);
const amount = Number(multiplier.replace(',', '')); // I guess 1,000$ should be 1000$, not 1.000$
const baseAmount = Number(amount / rate);
const currencyAmount = Number(rate * amount);
output.push({
title: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`,
icon: {
path: `flags/${currency}.png`
},
arg: `${amount.toLocaleString(locale)} ${currency}`,
autocomplete: `${amount.toLocaleString(locale)} ${currency}`,
text: {
copy: `${baseAmount.toLocaleString(locale)}`,
largetype: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`
}
});
// This will show the opposite conversion
output.push({
title: `${amount.toLocaleString(locale)} ${baseCurrency} = ${currencyAmount.toLocaleString(locale)} ${currency}`,
icon: {
path: `flags/${baseCurrency}.png`
},
arg: `${currencyAmount.toLocaleString(locale)}`,
variables: {
function: 'copy'
},
text: {
copy: `${baseAmount.toLocaleString(locale)}`,
largetype: `${amount.toLocaleString(locale)} ${baseCurrency} = ${baseAmount.toLocaleString(locale)} ${currency}`
}
});
}
}
function addCurrencyOutput(multiplier, currency) {
const rate = alfy.cache.get(currency);
const amount = Number(multiplier.replace(',', '')); // I guess 1,000$ should be 1000$, not 1.000$
const baseAmount = Number(amount / rate);
const currencyAmount = Number(rate * amount);
output.push({
title: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`,
subtitle: `${amount.toLocaleString(locale)} ${baseCurrency} = ${currencyAmount.toLocaleString(locale)} ${currency}`,
icon: {
path: `flags/${currency}.png`
},
arg: `${baseAmount.toLocaleString(locale)}`,
variables: {
function: 'copy'
},
text: {
copy: `${baseAmount.toLocaleString(locale)}`,
largetype: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`
}
});
}
/*
Added few functions
*/
// Show baseCurrency => currency as subtitle in main page
// I used this because it's a bit messy if it's showed as a title instad of subtitle
// in main page
function addCurrencyMainListOutput(multiplier, currency) {
if (currency !== baseCurrency) {
var rate = alfy.cache.get(currency);
var amount = Number(multiplier.replace(',', ''));
var baseAmount = Number(amount / rate);
var currencyAmount = Number(rate * amount);
output.push({
title: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`,
subtitle: `${amount.toLocaleString(locale)} ${baseCurrency} = ${currencyAmount.toLocaleString(locale)} ${currency}`,
icon: {
path: `flags/${currency}.png`
},
arg: `${amount.toLocaleString(locale)} ${currency}`,
autocomplete: `${amount.toLocaleString(locale)} ${currency}`,
text: {
copy: `${baseAmount.toLocaleString(locale)}`,
largetype: `${amount.toLocaleString(locale)} ${currency} = ${baseAmount.toLocaleString(locale)} ${baseCurrency}`
}
});
}
}
// Update Favorite List
function updateList(list) {
if (favoriteCurrency == "" || typeof favoriteCurrency == "undefined") { // if it hasn't been set yet
newList.push(list);
alfy.cache.set('favoriteCurrency', newList);
}
else {
favoriteCurrency.push(list);
alfy.cache.set('favoriteCurrency', favoriteCurrency);
}
}
function reomoveList(list) {
// remove 'list' from favoriteCurrency
for(let i = 0; i < favoriteCurrency.length; i++) {
if(favoriteCurrency[i] === list) {
favoriteCurrency.splice(i, 1);
break;
}
}
alfy.cache.set('favoriteCurrency', favoriteCurrency);
}
function addUpdateListOutput(currency) {
output.push({
title: `Add ${currency} to favorite currency list`,
icon: {
path: `flags/${currency}.png`
},
arg: ''
});
}
function addRemoveListOutput(currency) {
output.push({
title: `Remove ${currency} from favorite currency list`,
icon: {
path: `flags/${currency}.png`
},
arg: ''
});
}
function addfavoriteCurrencyListOutput(currency) {
output.push({
title: `Add ${currency} to favorite currency list`,
icon: {
path: `flags/${currency}.png`
},
arg: `LIST ${currency}`
});
}
// Remove a Currency from list
function removeFavoriteCurrencyListOutput(currency) {
output.push({
title: `Remove ${currency} from favorite currency list`,
icon: {
path: `flags/${currency}.png`
},
arg: `RMVE ${currency}`
});
}
// Reset Favorite List
function resetfavoriteCurrency() {
output.push({
title: `Reset customized query list`,
icon: {
path: 'settings.png'
},
arg: `REST GO` // double check the reset
});
}
function resetConfirmed() {
alfy.cache.set('favoriteCurrency', ""); // reset cache to ""
}
function addResetOutput() {
output.push({
title: `Reset completed, press Enter to continue`,
icon: {
path: `settings.png`
},
arg: 'LIST ' // There must be at least one currency in favoriteCurrency
});
}
if (nextUpdate < new Date()){
promises.push(getOSLocale());
promises.push(updateRates(baseCurrency));
output.push({
title: `INFO: Rates for ${baseCurrency} updated: ${new Date(alfy.cache.get('updateDate')).toLocaleString(locale)}`,
subtitle: `next update: ${new Date(alfy.cache.get('nextUpdate')).toLocaleString(locale)}`,
icon: {
path: 'settings.png'
}
});
}
if (query[0] !== 'BASE' && (!baseCurrency || !lastUpdate)) {
promises.push(getOSLocale());
if (!query[0]) {
currencies.forEach(currency => addSetBaseCurrencyListOutput(currency));
} else if (query[0].match('^[A-Z]{1,2}$')) {
currencies.filter(currency => currency.includes(query[0])).forEach(currency => addSetBaseCurrencyListOutput(currency));
} else if (currencies.includes(query[0])) {
addSetBaseCurrencyListOutput(query[0]);
}
}
/*
Check if the favorite currency list is set or not
before all
*/
else if ((favoriteCurrency == "" || typeof favoriteCurrency == "undefined") && query[0] !== 'LIST' && query[0] !== 'BASE') {
promises.push(getOSLocale());
if (!query[0]) {
currencies.forEach(currency => addfavoriteCurrencyListOutput(currency));
} else if (query[0].match('^[A-Z]{1,2}$')) {
currencies.filter(currency => currency.includes(query[0])).forEach(currency => addfavoriteCurrencyListOutput(currency));
} else if (currencies.includes(query[0])) {
addfavoriteCurrencyListOutput(query[0]);
}
}
else if (!query[0]) {
favoriteCurrency.forEach(currency => addCurrencyMainListOutput('1', currency));
// show favorite currency list in first page
addBaseOutput();
// show settings below the currencies list
} else if (query[0] === 'BASE' && !query[1]) {
currencies.forEach(currency => addSetBaseCurrencyListOutput(currency));
} else if (query[0] === 'BASE' && query[1]) {
if (query[1].match('^[A-Z]{1,2}$')) {
currencies.filter(currency => currency.includes(query[1])).forEach(currency => addSetBaseCurrencyListOutput(currency));
} else if (currencies.includes(query[1])) {
promises.push(getOSLocale());
promises.push(updateRates(query[1]));
addUpdateRatesOutput(query[1]);
}
}
/*
Add / Remove / Reset favorite currency list
*/
else if (query[0] === 'LIST' && !query[1]) {
currencies.forEach(currency => addfavoriteCurrencyListOutput(currency));
} else if (query[0] === 'LIST' && query[1]) {
if (query[1].match('^[A-Z]{1,2}$')) {
currencies.filter(currency => currency.includes(query[1])).forEach(currency => addfavoriteCurrencyListOutput(currency));
} else if (currencies.includes(query[1])) {
promises.push(getOSLocale());
promises.push(updateList(query[1]));
addUpdateListOutput(query[1]);
}
} else if (query[0] === 'RMVE' && !query[1]) {
if (typeof favoriteCurrency != "undefined" && favoriteCurrency != "") { // only if there's a value in list
favoriteCurrency.forEach(currency => removeFavoriteCurrencyListOutput(currency));
}
} else if (query[0] === 'RMVE' && query[1]) {
if (typeof favoriteCurrency != "undefined" && favoriteCurrency != "") {
if (query[1].match('^[A-Z]{1,2}$')) {
favoriteCurrency.filter(currency => currency.includes(query[1])).forEach(currency => removeFavoriteCurrencyListOutput(currency));
} else if (favoriteCurrency.includes(query[1])) {
promises.push(getOSLocale());
reomoveList(query[1]);
addRemoveListOutput(query[1]);
}
}
} else if (query[0] === 'REST' && !query[1]) {
resetfavoriteCurrency();
} else if (query[0] === 'REST' && query[1] == 'GO') {
resetConfirmed();
addResetOutput(query[1]);
} else if (query[0].match(/\d*([.,]?\d+)/) && !query[1]) {
favoriteCurrency.forEach(currency => addCurrencyListOutput(query[0], currency));
// if query only with numbers, show the results of which currency only in favoriteCurrency
} else if (query[0].match(/\d*([.,]?\d+)/) && query[1]) {
if (query[1].match('^[A-Z]{1,2}$')) {
currencies.filter(currency => currency.includes(query[1])).forEach(currency => addCurrencyListOutput(query[0], currency));
} else if (currencies.includes(query[1]) && query[1] !== baseCurrency) {
addCurrencyOutput(query[0], query[1]);
}
}
Promise.all(promises).then(() => {
alfy.output(output)
});