Skip to content

Instantly share code, notes, and snippets.

View thomasxbanks's full-sized avatar

Thomas Banks thomasxbanks

View GitHub Profile
@thomasxbanks
thomasxbanks / delay.js
Created July 15, 2019 13:21
Fake an API response using a delay
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
delay(500).then(() => console.log(`hello world`))
@thomasxbanks
thomasxbanks / console.log
Created July 15, 2019 11:22
regex Search: Uncommented console.logs
\w*(?<!\/\/)console.log
const numberizePixels = (value) => {
return ~~(value.substr(0, (value.length - 2)));
};
const getComputedHeight = (element) => {
return window.getComputedStyle(element).getPropertyValue('height');
};
const calculatePixelsFromPoints = (pt) => {
var dpi = 96
return ((pt / 72) * dpi).toFixed(0)
}
let val = 0.5
console.log(`${val}pt = ${calculatePixelsFromPoints(val)}px`)
@thomasxbanks
thomasxbanks / nodeList-to-array.js
Created December 14, 2017 13:20
turns nodeLists into arrays to make forEach work in IE11
let paragraphs = [].slice.call(document.querySelectorAll('p'))
paragraphs.forEach((paragraph)=>{
// Do something to each paragraph
})
// The trick is the [].slice.call() wrapper that is shorthand for accessing the Array.prototype
// https://davidwalsh.name/nodelist-array
@thomasxbanks
thomasxbanks / colophon.js
Created June 15, 2017 16:02
Calculate the copyright date from a start date and now.
let colophon = (startDate) => {
let now = new Date()
return (startDate < now.getFullYear()) ? startDate += " - " + now.getFullYear() : now.getFullYear()
}
document.querySelector('#colophon').innerText = colophon('2014')
@thomasxbanks
thomasxbanks / modulo-counter.js
Created June 15, 2017 08:35
Assigns row number and column number to an array of objects
const array = [{},
{}, {}, {},
{}, {}, {},
{}, {}, {},
{}, {}
]
let columnCount = 4
let count = 1
let random_number = (min, max) => {
return ~~(Math.floor(Math.random() * max) + min)
}
// USAGE
// array[random_number(0, array.length)]
// if (random_number(0, 3) >= 2) {
// // do foo
// }
@thomasxbanks
thomasxbanks / sort.js
Created March 1, 2017 08:59
Sort an array of objects
'use strict'
let person = [
{ name: "Person 1", age: 28 },
{ name: "Person 2", age: 22 },
{ name: "Person 3", age: 26 }
]
person.sort(function(obj1, obj2) {
// Ascending: first age less than the previous