-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (81 loc) · 2.96 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
const http = require('http')
const isValid = (input) => {
let pattern = new RegExp(/^\d+$/)
return pattern.test(input)
}
const formatter = (input, firstWord) => {
if (!isValid(input)) return `${firstWord || 'Rp.'} 0`
let nominal = String(input).split('').reverse()
for (let i = 0 ; i < nominal.length; i++){
if ((i + 1) % 3 == 0 && i !== nominal.length - 1) {
nominal.splice(i + 1,0,'.')
}
}
return `${firstWord || 'Rp.'} ${nominal.join('').split('').reverse().join('')}`
}
const getData = new Promise((resolve, reject) => {
http.get('https://api.fixer.io/latest?base=IDR', res => {
let body = [],
data = {},
output = {}
res.on('data', chunk => {
body.push(chunk) //buffer data push into body value
data = Object.assign({}, JSON.parse(Buffer.concat(body).toString()))
// at this point, `body` has the entire request body stored in it as a string and transform into json
})
res.on('end',() => {
for (let prop in data.rates) {
let val = Math.round(1 / data.rates[prop])
output[prop] = formatter(val)
}
resolve(output)
})
})
})
const kurs = () => {
let data = {},
done = false
getData.then((output) => {
data = output
done = true
})
//wait until data has finish
require('deasync').loopWhile(function(){return !done;});
return data
}
const toWords = (number) => {
if (!isValid(number)) return `0 Rupiah`
return convertToWords(number)+ 'rupiah'
}
const convertToWords = (number) => {
let kamus = ["", "Satu ", "Dua ", "Tiga ", "Empat ", "Lima ", "Enam ", "Tujuh ", "Delapan ",
"Sembilan ", "Sepuluh ", "Sebelas "];
//recursive until number < 0
if(number < 12)
return kamus[number];
else if (number < 20)
return convertToWords(number - 10) + "Belas ";
else if (number < 100)
return convertToWords((number - (number % 10))/10) + "Puluh " + convertToWords(number%10);
else if (number < 200)
return "Seratus " + convertToWords(number - 100);
else if (number < 1000)
return convertToWords((number - (number % 100))/100) + "Ratus " + convertToWords(number % 100);
else if (number < 2000)
return "Seribu " + convertToWords(number - 1000);
else if (number < 1000000)
return convertToWords((number - (number % 1000))/1000) + "Ribu " + convertToWords(number % 1000);
else if (number < 1000000000)
return convertToWords((number - (number % 1000000))/1000000) + "Juta " + convertToWords(number % 1000000);
else if (number < 1000000000000)
return convertToWords((number - (number % 1000000000))/1000000000) + "Miliar " + convertToWords(number % 1000000000);
else if (number < 1000000000000000)
return convertToWords((number - (number % 1000000000000))/1000000000000) + "Triliun " + convertToWords(number % 1000000000000);
}
console.log(kurs());
module.exports = {
isValid,
formatter,
kurs,
toWords
}