Skip to content

Instantly share code, notes, and snippets.

@worc
worc / truthy.cpp
Created June 14, 2023 08:10
actual real c plus plus
if (LegoOmni::GetInstance()) {
close();
MxOmni::DestroyInstance();
}
@worc
worc / encodeQueryObject.ts
Created June 2, 2023 21:44
Type algebra to detect empty objects
/**
* This came out of some work where we were sending an encoded JSON object as a query param in a network call.
* But, we can't send nothing, `param=`, or we crash the server, that's taken care of by restricting the type
* to at least `Record<string, string>`, but it left open the possibility of empty objects. Encoded as `'%7B%7D'`.
*
* So I went looking and it's definitely possible to restrict empty objects, but the type algebra is wild. And
* it makes use of something people are calling F-bounded quantification. And the error message isn't every helpful.
*
* See:
* - https://stackoverflow.com/a/72554458/769780
@worc
worc / format-number-with-si-prefix.js
Last active September 13, 2022 06:33 — forked from cho45/format-number-with-si-prefix.js
but with mu for micro
function formatN (n) {
const unitList = ['y', 'z', 'a', 'f', 'p', 'n', 'μ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
const zeroIndex = 8;
const nn = n.toExponential(2).split(/e/);
let u = Math.floor(+nn[1] / 3) + zeroIndex;
if (u > unitList.length - 1) {
u = unitList.length - 1;
} else
if (u < 0) {
u = 0;
@worc
worc / click_away_effect.js
Created April 21, 2020 22:41
ok, so i've definitely done custom hooks with a useState before
function clickAwayListener (elementId, callback) {
return event => {
const outsideClick = !document.getElementById(elementId).contains(event.target)
if (outsideClick) {
callback()
}
}
}
export function useClickAway (elementId, callback) {
@worc
worc / dropzone.js
Last active January 23, 2024 20:18
react-dropzone formData example
// there's a sandbox to try this out on the react-dropzone website:
// https://react-dropzone.js.org/
import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps} = useDropzone();
const files = acceptedFiles.map(file => (
@worc
worc / muse-worc.zsh-theme
Created February 6, 2020 18:36
a small edit to the muse theme, adds a newline after the working directory and git prompt
#!/usr/bin/env zsh
#local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"
setopt promptsubst
autoload -U add-zsh-hook
PROMPT_SUCCESS_COLOR=$FG[117]
PROMPT_FAILURE_COLOR=$FG[124]
PROMPT_VCS_INFO_COLOR=$FG[242]
@worc
worc / rotate.js
Created January 30, 2020 01:15
hm....
var e = this.velocity
, t = Date.now()
, n = t - this.lastFrame;
this.lastFrame = t,
e *= n / 16,
this.rotation = this.rotation + e;
var r = "rotate(" + this.rotation + "deg)";
this.$el.css({
"-webkit-transform": r,
"-moz-transform": r,
@worc
worc / deleteLinkedInConversations.js
Created December 17, 2019 21:23
a quick and bashy way to delete all linked in conversations
function openDropdown () {
document.querySelector('li-icon[type="ellipsis-horizontal-icon"]').click()
return new Promise((resolve) => {
setTimeout(() => {
resolve('opened dropdown')
}, 125)
})
}
function clickDelete () {
@worc
worc / numeronym.js
Last active January 31, 2020 18:23
const numeronym = word => word.length > 1 ? `${word[0]}${word.length-2}${word[word.length-1]}` : `${word}`
@worc
worc / click_away_effect.js
Created May 6, 2019 16:41
a react hook for binding an event to a click outside an element
import { useEffect } from 'react'
function clickAwayListener (elementId, callback) {
return event => {
const outsideClick = !document.getElementById(elementId).contains(event.target)
if (outsideClick) {
callback()
}
}
}