This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #1c1c1c,#3d3030,#d92e2e,#ffffff,#615c5a,#bdbdbd,#f51b1b,#24a39d,#3d3030,#bdbdbd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getRegexMatches (regex, text) { | |
| let cursor; | |
| let results = []; | |
| while ((cursor = regex.exec(text)) !== null) { | |
| let [ usage, match ] = cursor; | |
| results.push(match); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'), |