Skip to content

Instantly share code, notes, and snippets.

@brito
brito / inventory.index.sql
Last active April 5, 2024 19:52
Sometimes you just need to figure out what's what in your tables
/*
List all schemas, tables, and columns
*/
create view inventory.index as
select nspname as _schema,
relname as _relname,
case
when relkind = 'r' then 'table'
when relkind = 'v' then 'view'
when relkind = 'm' then 'materialized view'
@brito
brito / Catalog.js
Last active December 26, 2023 22:36
function __SMOKE_TEST__Catalog(){ console.log(
Catalog([{a:1, b:2}, {b:4, c:7, d:0}, {a:0, b:2}])
)}
/*
┌─┐┌─┐┌┬┐┌─┐┬ ┌─┐┌─┐
│ ├─┤ │ ├─┤│ │ ││ ┬
└─┘┴ ┴ ┴ ┴ ┴┴─┘└─┘└─┘ 18w40*/
function Catalog(items){
return ([].concat(arguments)).reduce(catalog, {});
@brito
brito / flatten_json.sql
Created March 9, 2022 19:44
recursive introspection: flatten (json) → (key, value, json_type)
/*
Flatten JSON tables
*/
create or replace function flatten_json(data jsonb)
returns table (
key text,
value jsonb,
json_type text)
as
$$
@brito
brito / test-ts_rank.sql
Created October 19, 2022 23:31
Test multiple configurations of tsqueries and ranked search scores
-- TEST ts_rank functions and (some) configs
select *
from (select trim(regexp_replace(txt, E'\\s*\n\\s*', ' ', 'g')) as txt, tsv
from (values ('my phone will not make or receive calls'),
($$ no matter what I do, my phone will not make or receive
calls even when I've asked nicely and it all goes well in the day
and then one day I did this other unrelated thing
and it still wouldn't work AT ALL so I asked Ben
who probably doesn't either but he has interesting answers $$),
('call my phone')) test_values (txt),
@brito
brito / speech.js
Created June 25, 2022 23:57
Minimal webkit speech recognition and synthesis demo
// Voice Driven Web Apps
//developer.chrome.com/blog/voice-driven-web-apps-introduction-to-the-web-speech-api
with (new webkitSpeechRecognition)
continuous =
interimResults =
true,
onstart =
onerror =
@brito
brito / API design
Created March 21, 2013 07:30
Joshua Bloch: Bumper-Sticker API Design
All programmers are API designers:
Good programs are modular, and intermodular boundaries define APIs. Good modules get reused.
APIs can be among your greatest assets or liabilities:
Good APIs create long-term customers; bad ones create long-term support nightmares.
Public APIs, like diamonds, are forever:
You have one chance to get it right so give it your best.
APIs should be easy to use and hard to misuse:
@brito
brito / _asTable.js
Created February 28, 2022 19:19
Flatten an Object into a table with rows of columns including keys as headers
/**
* Flatten an Object into a table with rows of columns including keys as headers
* @param object to flatten
*/
function _asTable (object) {
return [].concat(object).reduce((list, item, i) => {
let labels = Object.keys(item)
if (!i && +labels[0] !== 0) list.push(labels)
list.push(labels.map(label => item[label]))
return list
@brito
brito / parsel.sql
Created December 1, 2021 01:09 — forked from molind/parsel.sql
Parallel select and Parallel query
-- since it uses dblink it should be enabled in the database.
-- CREATE EXTENSION dblink;
-- And you'll may need to grant permissions to use it to your user.
-- GRANT EXECUTE ON FUNCTION dblink_connect_u(text) TO user;
-- GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO user;
-- Usage example:
-- select g_parsel('insert into osm_polygon_extra select osm_id, st_pointonsurface( st_collect( geom ) ) from osm_polygons group by osm_id;', 'osm_polygons', 12);
@brito
brito / tally.gsheet.js
Created August 3, 2021 23:06
Count top values from a range of values (mode at the top). Useful to detect data anomalies, patterns and degree of variability in values (how different things are, how many nulls, etc)
/* Tally v 1.0 */
function tally (range, limit = 10){
let total = range.length,
values = {}
for (let [k, v] of range) {
k = ('' + k).replace(/\n[\s\S]*/, '…')
values[k] = values[k] ? values[k] + 1 : 1
}
return Object.entries(values)
@brito
brito / replaces.js
Created April 2, 2021 00:06
Transform a string using multiple regex
/*
Transform a string using multiple regex
@customfunction
*/
function replaces(operators,operands) {
return operators[0].map ?
operators.reduce((a,b) => replaces(b,a), operands)
: operands.replace(new RegExp(operators[0]), operators[1])
}