Skip to content

Instantly share code, notes, and snippets.

@brandonmcconnell
brandonmcconnell / log.js
Created October 13, 2023 01:54
Simple & elegant JS logging solution (`console.log` decorator)
function toHyphenated(styleKey) {
return styleKey.replace(/([A-Z])/g, "-$1").toLowerCase();
}
const log = (...args) => {
console.log(...args);
};
Object.assign(log, {
if(condition) {
return {
then(callback) {
@brandonmcconnell
brandonmcconnell / stuck.ts
Created October 4, 2023 20:41
`stuck` Svelte action
function getStickyRoot(node: Element) {
let current = node.parentElement;
while (current && current !== document.body) {
const computedStyle = window.getComputedStyle(current);
const overflow = computedStyle.getPropertyValue('overflow');
if (overflow === 'scroll' || overflow === 'auto') {
return current;
}
current = current.parentElement;
@brandonmcconnell
brandonmcconnell / getTimeSince.js
Last active July 26, 2022 04:11
getTimeSince - function for getting a human-readable value of the time since/between datetimes
const ordinalizeNumber = n => {
const rule = new Intl.PluralRules('en-US', { type: 'ordinal' }).select(n);
const suffix = ({
one: 'st',
two: 'nd',
few: 'rd',
other: 'th',
})[rule];
return `${n}${suffix}`;
}