Skip to content

Instantly share code, notes, and snippets.

View austinbillings's full-sized avatar
🏗️

Ʌustin Billings austinbillings

🏗️
  • Lancaster County PA
  • 23:38 (UTC -05:00)
View GitHub Profile
@austinbillings
austinbillings / getRandomId.js
Created October 2, 2022 22:54
Generates a random 12-character url-safe ID; includes test for 10mm+ uniqueness guarantee
const getRandomId = () => (Math.random() * Math.random() * 3)
.toString(36)
.replace('.', '')
.substring(0, 12)
function runRandomnessTest() {
var resultSet = new Set()
var duplicates = []
function checkResult (v, i) {
@austinbillings
austinbillings / getClosestValue.ts
Last active November 7, 2021 07:39
Gets the closest value (numerically) to an input value, from an array of values
function getClosestValue (value: number, fromList: Array<number>, tieGoesToHigher = true) {
const list = fromList.filter(x => x || x === 0).sort((a, b) => a - b)
const firstValue = list[0]
const lastValue = list[list.length - 1]
let chosen = list.includes(value)
? value
: value < firstValue
? firstValue
: value > lastValue
@austinbillings
austinbillings / getPercentageListForSize.js
Last active May 10, 2021 04:43
Evenly produce a list of percentages adding to 100 as evenly as possible for a given list size
const reduceToTotal = (o = 0, v = 0) => o += v;
export const getPercentageListForSize = (size = 1) => {
const emptyList = (new Array(size)).fill('');
const evenlySplit = emptyList.map((x, i) => 100 / size);
const flooredList = evenlySplit.map(x => Math.floor(x));
const flooredTotal = flooredList.reduce(reduceToTotal);
const remainingAfterFloored = 100 - flooredTotal;
const isString = s => typeof s === 'string';
const isArray = a => Array.isArray(a);
const isObject = o => typeof o === 'object' && Object.prototype.toString.call(o) === '[object Object]';
function hydrateDates (value) {
const TIMESTAMP_PATTERN = /[0-9]{4}-[0-9]{2}-[0-9]{2}(?:T| )[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:.[0-9]+)?)? ?(?:z|Z|(?:(?:\+|-)[0-9]{4}|(?:\+|-)[0-9]{2}:[0-9]{2})|[a-zA-Z]{3}(?:\+|-)[0-9]{2}:[0-9]{2})/;
if (!isObject(value) && !isArray(value))
return value;
var beatles = ['john', 'paul', 'george', 'ringo'];
var signs = ['aries', 'taurus', 'gemini', 'cancer', 'leo', 'virgo', 'libra', 'scorpio', 'saggitarius', 'capricorn', 'aquarius', 'pisces']
var objects = [ {a:1}, {b:2}, {c:3} ]
var isArray = Array.isArray
var isNonEmptyArray = a => isArray(a) && a.length
var isDefined = v => typeof v !== 'undefined'
function shuffle (array) {
@austinbillings
austinbillings / PromisePooling.js
Created December 8, 2020 05:52
demonstration of sharing a single promise amongst consumers
const state = { current:null } // ref pattern here (immutable parent)
state.current = new Promise((resolve, reject) => {
console.log('Original promise only running once :)');
setTimeout(function() {
console.log('Completed in da future')
resolve(true)
}, 1000);
});
@austinbillings
austinbillings / killer.slacktheme
Created September 15, 2020 07:06
Slack Theme - "Killer" (for my own records)
#1c1c1c,#3d3030,#d92e2e,#ffffff,#615c5a,#bdbdbd,#f51b1b,#24a39d,#3d3030,#bdbdbd
function chunkIntoGroups (flatList, groupSize) {
return flatList.reduce(function (out, item) {
var lastGroup = out.pop();
var lastGroupIsArray = Array.isArray(lastGroup);
var needsNewGroup = !lastGroup || lastGroup.length >= groupSize;
var newLastGroup = needsNewGroup
? lastGroupIsArray ? [lastGroup, [item]] : [[item]]
: [lastGroup.concat(item)]
@austinbillings
austinbillings / getRegexMatches.js
Created May 14, 2020 02:37
getRegexMatches(regex, text)
function getRegexMatches (regex, text) {
let cursor;
let results = [];
while ((cursor = regex.exec(text)) !== null) {
let [ usage, match ] = cursor;
results.push(match);
}
@austinbillings
austinbillings / styleDate.js
Last active November 5, 2020 21:09
V2! Better options names.
function styleDate (date, format = 'compact', locale = undefined) {
const option = s => format.split('-').map(t => t.toLowerCase()).includes(s);
const options = {
// Formatting =======================================================================
compact: option('compact'),
// compact: use numbers & slashes (e.g., 3/21/19) instead of words & spaces
precise: option('precise'),
// precise: when also using -compact, use leading-zeros and full year numbers (e.g., 03/08/1995) instead of shorter values (e.g. 2/1/99)
full: option('full'),