Skip to content

Instantly share code, notes, and snippets.

View austinbillings's full-sized avatar
πŸ—οΈ

Ι…ustin Billings austinbillings

πŸ—οΈ
  • Lancaster County PA
  • 03:43 (UTC -05:00)
View GitHub Profile
@austinbillings
austinbillings / memoize.js
Created April 16, 2025 15:14
tiny memoize, 2025 edition
export function memoize (callback) {
const cache = new Map()
return (...args) => {
const key = JSON.stringify(args)
if (cache.has(key)) return cache.get(key);
const result = callback(...args)
cache.set(key, result);
return result;
}
}
@austinbillings
austinbillings / prepExpression.js
Created April 14, 2025 20:02
Given a JS expression or snippet, wraps it in an IIFE and prepends `return` if the code doesn't contain it.
function prepExpression (e) {
const returnText = 'return '
const inlineReturnPattern = /(?:\)|\n| |\}|\;|\:|\])return(?: |\(|\{|\[)/
const expression = e.startsWith(returnText) || inlineReturnPattern.test(e) ? e : returnText.concat(e)
return `(function () {${expression};})();`
}
@austinbillings
austinbillings / FindAndWithout.js
Last active April 14, 2025 19:59
Declarative find() and without()
function find (collection = [], matchObject = {}) {
return (collection || [])
.find(obj => Object.keys(matchObject).every(key => obj[key] === matchObject[key])) || null;
}
function without (collection = [], matchObject = {}) {
return collection.filter(obj => {
return Object
.keys(matchObject)
.every(key =>
@austinbillings
austinbillings / Logger.js
Last active April 14, 2025 19:53
Über-simple logger factory
// -- Example usage:
// const demo = logger('@example')
// -- can be used directly,
// demo('oh my goodness')
// -- or you can call the normal console fns
// demo.info('Interesting message text', 'someVar to dump to console', [1,2,3,4])
const logger = (tagText) => {
const l = (method = 'log') => (message, ...otherArgs) => {
const tag = `[${tagText}]`
// Demo usage (run to see output)
// Print 0-120:
// (new Array(120)).fill({}).map((x,i) => ordinal(i))
// Print bolded html ordinal
// ordinal(4, v => `<b>${v}</b>`)
export function ordinal (number, wrapSuffix) {
const chars = number.toString().split('');
const lastDigit = chars.pop()
const secondToLastDigit = chars.pop();
@austinbillings
austinbillings / Unstyle.js
Created April 9, 2025 14:02
A simple IIFE that strips out <style>, <link> and inline CSS to expose the bare markup.
(() => {
function strip (...selectors) { selectors.forEach(s => Array.from(document.querySelectorAll(s)).forEach(x => x.remove())) };
function destyle (x) { x.removeAttribute('style'); x.childNodes.forEach(c => c.nodeType == 1 ? destyle(c) : null) };
strip('style', 'link');
destyle(document.body);
})();
@austinbillings
austinbillings / sentence-splitting.js
Last active March 12, 2025 21:23
Safely split strings into sentences, being careful not to accidentally cut off on a "St.", "Dr.", "Inc.", or similar 'false ending'
export function splitBySentences (givenText = '', additionalFalseEndings = []) {
const text = givenText.trim()
const initialisms = 'abcdefghijklmnopqrstuvwxyz'.split('')
const commonFalseEndings = ['St', 'Rd', 'Ave', 'Mr', 'Mrs', 'Dr', 'Prof', 'Esq', 'Ln', 'Inc', 'Corp', 'Blvd', 'Pl', 'Av', 'Ct']
const falseEndings = [...commonFalseEndings, ...initialisms, ...additionalFalseEndings]
.map(c => [c, c.toUpperCase(), c.toLowerCase()])
.flat()
.reduce((o, v) => o.includes(v) ? o : o.concat(v), [])
.map(c => ` ${c}. `)
const toUniqueSymbol = (str) => `β€ΉΒ«!#βˆžβˆ†${falseEndings.indexOf(str)}βˆ†βˆž#!Β»β€Ί`
const cursiveLetters = {
A: 'π’œ',
B: '𝐡',
C: 'π’ž',
D: 'π’Ÿ',
E: '𝐸',
F: '𝐹',
G: '𝒒',
H: '𝐻',
@austinbillings
austinbillings / blockLetters.js
Created November 19, 2022 17:20
Output text as πŸ„±πŸ„»πŸ„ΎπŸ„²πŸ„Ί πŸ„»πŸ„΄πŸ…ƒπŸ…ƒπŸ„΄πŸ…πŸ…‚
const blockLetters = { a: 'πŸ„°', b: 'πŸ„±', c: 'πŸ„²', d: 'πŸ„³', e: 'πŸ„΄', f: 'πŸ„΅', g: 'πŸ„Ά', h: 'πŸ„·', i: 'πŸ„Έ', j: 'πŸ„Ή', k: 'πŸ„Ί', l: 'πŸ„»', m: 'πŸ„Ό', n: 'πŸ„½', o: 'πŸ„Ύ', p: 'πŸ„Ώ', q: 'πŸ…€', r: 'πŸ…', s: 'πŸ…‚', t: 'πŸ…ƒ', u: 'πŸ…„', v: 'πŸ……', w: 'πŸ…†', x: 'πŸ…‡', y: 'πŸ…ˆ', z: 'πŸ…‰' }
const asBlockLetters = (text) => text.split('').map(c => c.toLowerCase() in blockLetters ? blockLetters[c.toLowerCase()] : c).join('');
asBlockLetters('BLOCK LETTERS')
@austinbillings
austinbillings / numberToText.js
Last active October 19, 2022 00:48
Prints textual number for given value ("four hundred twenty")
function reverse (list) {
return typeof list === 'string'
? list.split('').reduce((o, v) => `${v}${o}`, '')
: Array.isArray(list)
? list.reduce((o, v) => [v, ...o], [])
: TypeError('No array or string given');
}
function chunkIntoN (list, n) {
return (typeof list === 'string' ? list.split('') : list ? list : []).reduce((o, x) => {