Skip to content

Instantly share code, notes, and snippets.

View john-doherty's full-sized avatar
🎯
Focusing

John Doherty john-doherty

🎯
Focusing
View GitHub Profile
@john-doherty
john-doherty / javascript-promise-timeout.js
Last active December 17, 2021 02:06
Adds a timeout to a JavaScript promise, rejects if not resolved within timeout period (uses requestAnimationFrame for better accuracy)
(function () {
'use strict';
/**
* wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time
* @param {integer} ms - milliseconds to wait before rejecting promise if not resolved
* @param {Promise} promise to monitor
* @example
* promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json'))
@john-doherty
john-doherty / how-to-git-push-to-dokku-droplet-mac.md
Last active March 12, 2019 14:39
How to git push to a Dokku Droplet from a Mac

Digital Ocean Droplet

Digital Ocean offers a pre-installed Dokku image. You can run this image on any sized droplet, although larger droplets will allow you to run larger applications.

When choosing your Droplet configuration please disable IPv6. There are known issues with IPv6 on Digital Ocean and Docker, and many have been reported to the Dokku issue tracker. If you would like to run Dokku on an IPv6 Digital Ocean Droplet, please consult this guide.

Dokku setup using a Mac

  1. Login to your Digital Ocean account
@john-doherty
john-doherty / javascript-trim-svg-whitespace.js
Created October 21, 2016 13:39
Trim whitespace from SVG elements
function trimSvgWhitespace() {
// get all SVG objects in the DOM
var svgs = document.getElementsByTagName("svg");
// go through each one and add a viewbox that ensures all children are visible
for (var i=0, l=svgs.length; i<l; i++) {
var svg = svgs[i],
box = svg.getBBox(), // <- get the visual boundary required to view all children
# Disable DPMS.
xset s off # don't activate screensaver
xset -dpms # disable DPMS (Energy Star) features.
xset s noblank # don't blank the video device
# Create a RAM disk to use as a FIFO for streaming.
if [ ! -d /iwk ]; then
mkdir /tmp/rdisk
fi
mount -t tmpfs -o size=25M tmsfs /tmp/rdisk
@john-doherty
john-doherty / javascript-http-get.js
Last active September 28, 2018 18:30
Simple, pure JavaScript HTTP Get function (IE8+, Chrome, Safari, Firefox, PhoneGap/Cordova)
/**
* GET contents of a URL
* @access private
* @param {string} url - url to get
* @param {function} error - function to call if there is an error
* @param {function} callback - function to call if success
* @returns {void}
*/
function httpGet(url, error, callback) {
@john-doherty
john-doherty / javascript-get-styles-by-selector.js
Last active August 17, 2017 11:26
Get all css style blocks matching a css selector from stylesheets
/**
* Get all CSS style blocks matching a CSS selector from stylesheets
* @param {string} className - class name to match
* @param {boolean} startingWith - if true matches all items starting with selector, default = false (exact match only)
* @example getStylesBySelector('pure-form .pure-form-html ')
* @returns {object} key/value object containing matching styles otherwise null
*/
function getStylesBySelector(className, startingWith) {
@john-doherty
john-doherty / javascript-clean-csv-string.js
Last active March 28, 2024 01:31
Remove empty values, trim whitespace and removes duplicate from a comma separated string in JavaScript
/**
* Removes empty values, trims whitespace and removes duplicate from a comma separated string in JavaScript
* @example
* cleanCsvString('one, ,, , two,two,two, three'); // returns 'one,two,three'
* cleanCsvString('one, ,, , two,two,two, three', false); // returns 'one,two,two,two,three'
* @param {string} str - string to modify
* @param {boolean} removeDuplicates - should remove duplicate items? (default = true)
* @returns {string} cleaned CSV string
*/
function cleanCsvString(str, removeDuplicates) {
@john-doherty
john-doherty / disable-mac-swipe-navigation.js
Created February 12, 2018 11:42
Disable Mac swipe navigation in pure JavaScript
(function (window) {
if ((/Macintosh/gi).test(navigator.userAgent) && (/Chrome|Safari|Firefox/gi).test(navigator.userAgent)) {
history.pushState(null, null, location.href);
window.onpopstate = function(event) {
history.go(1);
};
}
@john-doherty
john-doherty / settimeout-using-requestanimationframe.js
Created April 24, 2018 17:54
JavaScript window.setTimeout replacement using requestAnimationFrame for better performance
(function (window) {
'use strict';
var oldSetTimeout = window.setTimeout;
/**
* Behaves the same as setTimeout but uses requestAnimationFrame() for better performance
* @param {function} fn The callback function
* @param {int} delay The delay in milliseconds
@john-doherty
john-doherty / yc-startup-descriptions.js
Last active March 17, 2021 11:24
Calculate the average number of words used to describe Y Combinator startup
// 1) visit yclist.com
// 2) open the browser console
// 3) run the following
// get all table rows
var rows = Array.prototype.slice.call(document.querySelectorAll('#companies tbody tr'));
// extract descriptions (use lastChild to skip aquired etc)
var descriptions = rows.map(function (row) { return row.cells[5].lastChild.textContent.trim(); });