Skip to content

Instantly share code, notes, and snippets.

@package71
Created November 21, 2018 05:02
Show Gist options
  • Save package71/aa1c8d7d56fda9b292df6f5eb5859e7a to your computer and use it in GitHub Desktop.
Save package71/aa1c8d7d56fda9b292df6f5eb5859e7a to your computer and use it in GitHub Desktop.
for refactor
function genetationJSON(res) {
let f = [{
item: {
from: "____XML1",
to: "____XML2",
in: "____rateIn",
out: "____rateOut",
amount: "____reserve",
param: 'manual',
minamount: "____minamount"
}
}];
let from = {};
let aliases = [''];
let reserve = {};
for (let i in f) {
if (aliases.indexOf(f[i].item.from) === -1) {
aliases.push(f[i].item.from)
}
if (aliases.indexOf(f[i].item.to) === -1) {
aliases.push(f[i].item.to)
}
const from_index = aliases.indexOf(f[i].item.from);
const to_index = aliases.indexOf(f[i].item.to);
reserve[to_index] = (f[i].item.amount === 0) ? undefined : f[i].item.amount;
if (!from[from_index])
from[from_index] = {
to: {},
in_min_amount: (f[i].item.minamount === 0) ? undefined : f[i].item.minamount
};
let rate = {
in: (f[i].item.in === 1) ? undefined : f[i].item.in,
out: (f[i].item.out === 1) ? undefined : f[i].item.out,
in_min_amount: (from[from_index].in_min_amount === f[i].item.minamount) ? undefined : f[i].item.minamount
};
if (!rate.in_min_amount && !rate.in && !rate.out) {
rate = 1;
} else if (!rate.in_min_amount && (!!rate.in || !!rate.out)) {
if (rate.in)
rate = rate.in;
if (rate.out)
rate = -rate.out;
}
from[from_index].to[to_index] = rate;
}
aliases[0] = undefined;
return {
exchanges: {
currencies: {
aliases: Object.assign({}, aliases),
amounts: reserve
},
from: from,
options: ["manual"]
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment