Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Last active June 17, 2018 12:55
Show Gist options
  • Select an option

  • Save chrispsn/d4570a45a3463beb1ee2551fccae316c to your computer and use it in GitHub Desktop.

Select an option

Save chrispsn/d4570a45a3463beb1ee2551fccae316c to your computer and use it in GitHub Desktop.
Generating tables of arbitrary length.
/* Mesh boilerplate - do not change. 2018-06-16-1 */
'use strict';
var _defProp = Object.defineProperty, _OUTPUT = {};
function _isFn(value) {return typeof value === 'function'};
function _defCell(k, c) {
return _defProp(self, k, {configurable: true, get() {
if (k in _OUTPUT) return _OUTPUT[k].v;
var o = _OUTPUT[k] = {};
var t = o.t = c.t; o.s = c.s; o.n = c.n;
var v = c.v; v = _isFn(v)? (t? _makeTable(v()) : v()) : v;
var f = c.f; o.f = (f) ? c.f(v) : String(v);
var l = c.l; o.l = _isFn(l) ? l() : l;
o.v = v; return v;
}})
};
function _makeTable(s) {
var t = [], cols = [], MAX = Math.max;
for (var h in s) {
if (h != 'length') cols.push([h,s[h].values,s[h].default])
};
t.length = (s.length === undefined)
? cols.reduce(function(a,e){return MAX(a,e[1].length)},0)
: s.length;
for (var i = 0, length = t.length, r; i < length; i++) {
t[i] = r = {};
cols.forEach(function(col) {
var h = col[0], c = col[1][i], s = r, j = i;
if (c === undefined) c = col[2];
_defProp(r, h, {get: function() {
delete this[h];
return this[h] = (_isFn(c)) ? c.call(t, s, j) : c
}, enumerable: true, configurable: true});
})
};
return t;
};
function _calcTable(t){for(var i in t){var r=t[i];for(var h in r)r[h]}};
self.onmessage = function(e) {
for (var k in _CELLS) _defCell(k, _CELLS[k]);
for (var k in _CELLS) var v = self[k]; if (_CELLS[k].t) _calcTable(v);
postMessage(_OUTPUT);
};
// Cell props: v = value or formula (fn), l = grid coordinates,
// f = format fn, s = transpose?, t = is table?, n = show name?
/* END Mesh boilerplate */
var curr_format = new Intl.NumberFormat('en-AU', { style: 'currency', currency: 'AUD' }).format
var _CELLS = {
"term": {v: 5, l: [1,1], f: function(v) {return v + " years"}, s: null, n: null, t: null},
"rate": {v: 0.10, l: [3,1], f: function(v) {return v * 100 + "%"}, s: null, n: null, t: null},
"startBalance": {v: 200000, l: [2,1], f: curr_format, s: null, n: null, t: null},
"payment": {v: function() {return startBalance * rate * Math.pow(1 + rate, term) / (Math.pow(1 + rate, term) - 1)},
l: [4,1], f: curr_format, s: null, n: null, t: null},
"cashflows": {
v: function() {return {
length: term,
start_balance: {
default: function(row, i) {return i === 0 ? startBalance : this[i - 1].end_balance},
values: []
},
interest: {
default: function(row, i) {return row.start_balance * rate},
values: []
},
payment: {
default: function(row, i) {return payment},
values: []
},
end_balance: {
default: function(row, i) {return row.start_balance + row.interest - row.payment},
values: []
},
}}, l: [1,4], f: function(v) {
return v.map(function(r) {
return {
start_balance: curr_format(r.start_balance),
interest: curr_format(r.interest),
payment: curr_format(r.payment),
end_balance: curr_format(r.end_balance),
}
})
}, s: null, n: null, t: true},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment