Skip to content

Instantly share code, notes, and snippets.

@DanielBodnar
Last active July 24, 2016 04:27
Show Gist options
  • Save DanielBodnar/49308e8d9e4013bbd3b952ee13c57972 to your computer and use it in GitHub Desktop.
Save DanielBodnar/49308e8d9e4013bbd3b952ee13c57972 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
class Account{
constructor(conf){
this.name = conf.name;
this.currency = currencies[this.name];
this.balance = conf.balance;
}
}
class Currency {
get mv() {
return this._mv;
}
set mv(value) {
value[this.name] = 1;
this._mv = value;
this.save();
}
convertTo(dest, size) {
if(dest === this.name){
return size;
}
let result = parseFloat((size * this.mv[dest]).toFixed(currencies[dest].precision));
console.log('Converting', size, this.name, 'to', dest, 'at exchange rate of', this.mv[dest], '/', this.name+':', result);
return result;
}
save(){
//console.log('saving:', this);
}
constructor(_currency) {
this.name = _currency.name;
this.precision = _currency.precision;
this.mv = _currency.mv;
}
}
let accounts = [];
let currencies = {
USD: new Currency({
name: 'USD',
precision: 2,
mv: {
BTC: 0.0015229972586,
ETH: 0.08183306055646
}
}),
BTC: new Currency({
name: 'BTC',
precision: 10,
mv: {
USD: 655.72,
ETH: 45.80852038479157
}
}),
ETH: new Currency({
name: 'ETH',
precision: 6,
mv: {
BTC: 0.02183,
USD: 12.22
}
})
}
accounts.push(new Account({
name: 'USD',
balance: 0
}));
accounts.push(new Account({
name: 'BTC',
balance: 4.7591933178383075
}));
accounts.push(new Account({
name: 'ETH',
balance: 0
}));
let accts = (() => {
let _accts = {};
accounts.forEach(a => _accts[a.name] = a);
return _accts;
})()
let netWorth = {};
let getWorth = (acct) => Object.keys(currencies).map(c => currencies[c].convertTo(acct.name, accts[c].balance)).reduce(( total, cur ) => total + cur);
accounts.forEach((acct) => {
netWorth[acct.name] = getWorth(acct);
});
console.log(netWorth);
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"babel-runtime": "6.9.2"
}
}
'use strict';
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
var Account = function Account(conf) {
(0, _classCallCheck3.default)(this, Account);
this.name = conf.name;
this.currency = currencies[this.name];
this.balance = conf.balance;
};
var Currency = function () {
(0, _createClass3.default)(Currency, [{
key: 'convertTo',
value: function convertTo(dest, size) {
if (dest === this.name) {
return size;
}
var result = parseFloat((size * this.mv[dest]).toFixed(currencies[dest].precision));
console.log('Converting', size, this.name, 'to', dest, 'at exchange rate of', this.mv[dest], '/', this.name + ':', result);
return result;
}
}, {
key: 'save',
value: function save() {
//console.log('saving:', this);
}
}, {
key: 'mv',
get: function get() {
return this._mv;
},
set: function set(value) {
value[this.name] = 1;
this._mv = value;
this.save();
}
}]);
function Currency(_currency) {
(0, _classCallCheck3.default)(this, Currency);
this.name = _currency.name;
this.precision = _currency.precision;
this.mv = _currency.mv;
}
return Currency;
}();
var accounts = [];
var currencies = {
USD: new Currency({
name: 'USD',
precision: 2,
mv: {
BTC: 0.0015229972586,
ETH: 0.08183306055646
}
}),
BTC: new Currency({
name: 'BTC',
precision: 10,
mv: {
USD: 655.72,
ETH: 45.80852038479157
}
}),
ETH: new Currency({
name: 'ETH',
precision: 6,
mv: {
BTC: 0.02183,
USD: 12.22
}
})
};
accounts.push(new Account({
name: 'USD',
balance: 0
}));
accounts.push(new Account({
name: 'BTC',
balance: 4.7591933178383075
}));
accounts.push(new Account({
name: 'ETH',
balance: 0
}));
var accts = function () {
var _accts = {};
accounts.forEach(function (a) {
return _accts[a.name] = a;
});
return _accts;
}();
var netWorth = {};
var getWorth = function getWorth(acct) {
return (0, _keys2.default)(currencies).map(function (c) {
return currencies[c].convertTo(acct.name, accts[c].balance);
}).reduce(function (total, cur) {
return total + cur;
});
};
accounts.forEach(function (acct) {
netWorth[acct.name] = getWorth(acct);
});
console.log(netWorth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment