Skip to content

Instantly share code, notes, and snippets.

View ianstarz's full-sized avatar

Ian Storz ianstarz

View GitHub Profile
/* will return null::json on empty resultset */
SELECT array_to_json(array_agg(row_to_json(t))) FROM t;
/*
will return '[]' on empty resultset,
null::json seems to be managed the same way than sql NULL by COALESCE()
*/
SELECT COALESCE(array_to_json(array_agg(row_to_json(t))), '[]') FROM t;
@ianstarz
ianstarz / logic-gates.js
Last active April 17, 2022 03:34 — forked from shawndumas/taggedTemplate.js
Build up all the logic gates from nand in js
const logicGates = {
nand (a, b) { return !(a && b); },
not (a) { return this.nand (a, a); },
and (a, b) { return this.not (this.nand (a, b)); },
or (a, b) { return this.nand (this.not (a), this.not(b)); },
nor (a, b) { return this.not (this.or (a, b)); },
xor (a, b) { return this.and (this.nand (a, b), this.or(a, b)); },
xnor (a, b) { return this.not (this.xor (a, b)); }
};
@ianstarz
ianstarz / guid.js
Created March 15, 2016 17:27 — forked from shawndumas/guid.js
GUID
var guid = function fn (n) {
return n ?
(n ^ Math.random() * 16 >> n/4).toString(16) :
('10000000-1000-4000-8000-100000000000'.replace(/[018]/g, fn));
};
[0, ['a', 'b', 'c'], 2, 'd', 4, 5, 'f', 7, ['g', 'h'], 9].reduce(function rec (prev, curr) {
if (/array/i.test(curr.constructor)) {
return curr.reduce(rec, prev);
} else if (/string/i.test(curr.constructor)) {
prev.push(curr);
}
return prev;
}, []);