Skip to content

Instantly share code, notes, and snippets.

@zachvictor
zachvictor / compactify.js
Last active October 11, 2022 22:19
JSON stringify function that compacts numeric arrays, putting brackets and values on the same line
function compactify(ugly) {
// Step 1: JSON stringify with indentation (4 spaces)
const first_pass = JSON.stringify(JSON.parse(ugly), null, 4)
// Step 2: Put comma-sep. numbers on same line
const comma_num_newline_ptn = /,\n +((\d\.)?\d+)/g
const num_same_line = first_pass.replace(comma_num_newline_ptn, ', $1')
// Step 3: Put flat arrays on same line
const flat_array_ptn = /\[\n\s+(([\d.]+, )+[\d.]+)\n\s+\]/g
@zachvictor
zachvictor / empty-array-json.go
Last active July 27, 2022 19:30
Empty JSON array via Go json.Marshal or []byte literal
package main
import (
"encoding/json"
"fmt"
"github.com/kr/pretty"
)
func main() {
@zachvictor
zachvictor / urtype.js
Last active July 11, 2021 10:38
urtype() is a one-line wrapper for Object.prototype.toString.call() that extracts the second word as the "urtype" (Anglophone misappropriation of Germanic prefix -- guilty as charged)
const urtype = (x) => Object.prototype.toString.call(x).split(' ')[1].slice(0, -1)
// examples
[
['undefined primitive', undefined],
['undefined via void operator', void ''],
['null', null],
['boolean primitive', true],
['number primitive', 123],
['string primitive', 'I feel pretty'],
@zachvictor
zachvictor / cal.sql
Created January 29, 2021 02:42
PostgreSQL query to generate a calendar
-- Generate a calendar in PostgreSQL
-- Rather than create a calendar table and insert rows into it statically,
-- this approach materializes a recursive [iterative] query.
CREATE MATERIALIZED VIEW cal AS
WITH RECURSIVE dd(d) AS (
VALUES ('2005-01-01'::DATE)
UNION ALL
SELECT d + 1
@zachvictor
zachvictor / query_selector_fn_alias.js
Last active August 19, 2020 09:06
Alias querySelector and querySelectorAll (for console debugging, etc.)
Object.getOwnPropertyNames(window).filter(name => name.match(/^HTML[a-zA-Z]*Element$/)).forEach(h => {
[{abbr: 'qs', name: 'querySelector'}, {abbr: 'qsa', name: 'querySelectorAll'}].forEach(fn => {
try {
window[h]['prototype'][fn.abbr] = window[h]['prototype'][fn.name]
console.log(`${h}: added alias ${fn.abbr} for ${fn.name} method.`)
} catch (e) {
console.log(`${h} has no ${fn.name} method.`, e.message)
}
})
})
@zachvictor
zachvictor / iframe_killer.js
Last active August 17, 2020 04:59
JavaScript for browser console kills on page iframes + uses MutationObserver to kill new iframes
(function () {
function __kill_added_iframe_mutob_callback(mList, ob){
const addedNodesToRemove = []
mList.forEach(mRec => {
mRec.addedNodes.forEach(addedNode => {
if (addedNode.tagName.match(/^iframe$/i)) {
addedNodesToRemove.push(addedNode)
}
})
@zachvictor
zachvictor / CoolorsPalette.js
Last active February 1, 2020 08:49
Translate Coolors.co palette to hex and rgb values in JavaScript/JSON object
var cp = (function () {
class CoolorsPalette {
constructor() {
// Row spans all palette columns: i.e., one shade of each color
this.rows = Array.from(document.querySelectorAll('#palette-shades div.palette-shades-row'))
const numCols = this.rows[0].querySelectorAll('div.palette-shades-col').length
this.colors = Array(numCols).fill([])
this.rows.forEach(row => {
Array
.from(row.querySelectorAll('div.palette-shades-col'))