Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Created December 30, 2018 20:50
Show Gist options
  • Save chrispsn/324d578f9571b8195876da6839e1b245 to your computer and use it in GitHub Desktop.
Save chrispsn/324d578f9571b8195876da6839e1b245 to your computer and use it in GitHub Desktop.
JSON example
'use strict';
const _CELLS = {
B2: ({
l: [1,1],
n: false,
get v() {
return `{"some": "simple value", "another": "simple_value", "nested": [1, 2, 3], "obj": {"hello": "world!", "it's": "me"}}`;
}
}),
D2: ({
get v() {
return JSON.parse(B2);
},
l: [1,3],
n: false
}),
G2: ({
get v() {
return D2.obj;
},
l: [1,6],
n: false
})
}
/* Mesh boilerplate - do not change. 2018-11-13-3 */
// Cell props: v = value or formula (fn), l = grid coordinates,
// f = format fn, s = transpose?, t = is table?, n = show name?
const g = (function () {return this || (1, eval)('this')}())
g._sc = function(x, d) {
if (d === 5) return null;
if (typeof x === 'function') return "ƒ";
if (x instanceof RegExp || x instanceof Date
|| x === null || x === undefined) return x;
if (Array.isArray(x)) return x.map(function(a){return _sc(a,d+1)});
if (typeof x === 'object') {const n={};for(let k in x){n[k]=_sc(x[k],d+1)};return n};
return x;
}
g.find = function(a, p, options) {
const l = a.length, o = options || {};
for (let k = 0; k < l; k++) {
const v = a[k];
if (p(v)) return (o.index ? k : v);
}
return o.default;
}
g._defProp = Object.defineProperty, g._OUTPUT = {}, g._STACK = []
g._defCell = function(k, c) {
return _defProp(g, k, {get: function() {
if (_STACK.length > 0) {
const top = _STACK[_STACK.length-1];
const edges = ('deps' in c) ? c.deps : (c.deps = new Set());
edges.add(top);
}
_STACK.push(k);
const v = ('r' in c) ? c.r : c.v;
if (!(k in _OUTPUT)) {
const f = c.f;
const o = _OUTPUT[k] = {
t: c.t, s: c.s, n: c.n, l: c.l,
v: _sc(v,0), f: f ? f(v) : f
};
}
_STACK.pop();
return v;
}, configurable: true})
}
g._makeTable = function(defs, length, rows) {
const t = [], proto = {};
for (let k in defs) _memoProp(defs, proto, k);
for (let i=0,l=length!==null?length:rows.length;i<l;i++) {
const r = rows[i] || {}; t.push(r);
Object.setPrototypeOf(r, proto);
_defProp(r, 't', {enumerable: false, value: t});
_defProp(r, 'i', {enumerable: false, value: i});
};
return t
}
g._getGetter = function(o, k) {return Object.getOwnPropertyDescriptor(o, k).get}
g._memoProp = function(source, dest, k) {
const getter = _getGetter(source, k);
if (getter !== undefined) {
_defProp(dest, k, {
get: function() {
const v = getter.call(this);
_defProp(this, k, {value: v});
return v;
}, enumerable: true
})
} else dest[k] = source[k];
}
g._defCells = function(c) {for (let k in c) _defCell(k, c[k])}
g._extraValues = function(vs) {for (let k in vs) {if (_CELLS[k].r !== vs[k]) {_uncache(k); _CELLS[k].r = vs[k]}}}
g._calcSheet = function(c) {for (let k in c) {let v = g[k]; if (c[k].t) _calcTable(v)}}
g._calcTable = function(t) {for (let i in t){let r=t[i];for(let h in r)r[h]}}
g._uncache = function(k) {const c = _CELLS[k]; delete c.r; delete _OUTPUT[k]; if ('deps' in c) c.deps.forEach(_uncache)}
/* END Mesh boilerplate */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment