Move to new proto + add magic of left-pad

This commit is contained in:
Ace Nassri 2018-06-27 13:51:08 -07:00
parent 51d5e9a3ed
commit 6f1a3d79e1
3 changed files with 24 additions and 20 deletions

View file

@ -18,6 +18,7 @@
const path = require('path');
const grpc = require('grpc');
const leftPad = require('left-pad');
const PROTO_PATH = path.join(__dirname, './proto/demo.proto');
const PORT = 31337;
@ -28,15 +29,15 @@ const client = new shopProto.CurrencyService(`localhost:${PORT}`,
const request = {
from: {
currency_code: 'USD',
currency_code: 'CHF',
units: 300,
nanos: 500000000
nanos: 0
},
to_code: 'CHF'
to_code: 'EUR'
};
function _moneyToString (m) {
return `${m.amount.decimal}.${m.amount.fractional} ${m.currency_code}`;
return `${m.units}.${leftPad(m.nanos, 9, '0')} ${m.currency_code}`;
}
client.getSupportedCurrencies({}, (err, response) => {

View file

@ -8,6 +8,7 @@
"async": "^1.5.2",
"google-protobuf": "^3.0.0",
"grpc": "^1.0.0",
"left-pad": "^1.3.0",
"request": "^2.87.0",
"xml2js": "^0.4.19"
}

View file

@ -31,7 +31,7 @@ const shopProto = grpc.load(PROTO_PATH).hipstershop;
let _data;
function _getCurrencyData (callback) {
if (!_data) {
console.log('Fetching currency data...')
console.log('Fetching currency data...');
request(DATA_URL, (err, res) => {
if (err) {
throw new Error(`Error getting data: ${err}`);
@ -53,7 +53,7 @@ function _getCurrencyData (callback) {
});
});
} else {
console.log('Using cached currency data...')
console.log('Using cached currency data...');
callback(_data);
}
}
@ -62,9 +62,10 @@ function _getCurrencyData (callback) {
* Helper function that handles decimal/fractional carrying
*/
function _carry (amount) {
amount.fractional += (amount.decimal % 1) * 100;
amount.decimal = Math.floor(amount.decimal) + Math.floor(amount.fractional / 100);
amount.fractional = amount.fractional % 100;
const fractionSize = Math.pow(10, 9);
amount.nanos += (amount.units % 1) * fractionSize;
amount.units = Math.floor(amount.units) + Math.floor(amount.nanos / fractionSize);
amount.nanos = amount.nanos % fractionSize;
return amount;
}
@ -72,7 +73,7 @@ function _carry (amount) {
* Lists the supported currencies
*/
function getSupportedCurrencies (call, callback) {
console.log('Getting supported currencies...')
console.log('Getting supported currencies...');
_getCurrencyData((data) => {
callback(null, {currency_codes: Object.keys(data)});
});
@ -82,7 +83,7 @@ function getSupportedCurrencies (call, callback) {
* Converts between currencies
*/
function convert (call, callback) {
console.log('Starting conversion request...')
console.log('Starting conversion request...');
try {
_getCurrencyData((data) => {
const request = call.request;
@ -90,22 +91,23 @@ function convert (call, callback) {
// Convert: from_currency --> EUR
const from = request.from;
const euros = _carry({
decimal: from.amount.decimal / data[from.currency_code],
fractional: from.amount.fractional / data[from.currency_code]
units: from.units / data[from.currency_code],
nanos: from.nanos / data[from.currency_code]
});
// Convert: EUR --> to_currency
const target = _carry({
decimal: euros.decimal * data[request.to_code],
fractional: euros.fractional * data[request.to_code]
const result = _carry({
units: euros.units * data[request.to_code],
nanos: euros.nanos * data[request.to_code]
});
target.fractional = Math.round(target.fractional);
result.nanos = Math.round(result.nanos);
result.currency_code = request.to_code;
console.log('Conversion request successful.')
callback(null, {currency_code: request.to_code, amount: target});
console.log('Conversion request successful.');
callback(null, result);
});
} catch (err) {
console.error('Conversion request failed.')
console.error('Conversion request failed.');
callback(err.message);
}
}