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-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-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 / 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-string-to-json-safe-key.js
Created June 16, 2019 18:12
Converts a string to JSON safe key (property name) by removing unsafe characters and converting to camel case
/**
* Converts a string to JSON safe key (property name) by removing unsafe
* characters and converting to camel case
* @param {string} src - string to convert
* @Example
* stringToJsonSafeKey('r@ndAm wo5d') // rndamWo5d
* @returns {string} id/key safe string
*/
function stringToJsonSafeKey(src) {
@john-doherty
john-doherty / get-nearest-html-attribute.js
Created August 29, 2020 14:20
Gets attribute off HTML element or nearest parent
/**
* Gets attribute off HTML element or nearest parent
* @param {object} el - HTML element to retrieve attribute from
* @param {string} attributeName - name of the attribute
* @param {any} defaultValue - default value to return if no match found
* @returns {any} attribute value or defaultValue
*/
function getNearestAttribute(el, attributeName, defaultValue) {
// walk up the dom tree looking for data-action and data-trigger
@john-doherty
john-doherty / javascript-is-empty.js
Last active February 19, 2021 10:51
Check if a variable is really empty in JavaScript
/**
* Checks if a JavaScript value is empty
* @example
* isEmpty(null); // true
* isEmpty(undefined); // true
* isEmpty(''); // true
* isEmpty([]); // true
* isEmpty({}); // true
* @param {any} value - item to test
* @returns {boolean} true if empty, otherwise false
@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(); });
@john-doherty
john-doherty / javascript-isurl.js
Created March 23, 2021 16:17
Check if a string is a URL in JavaScript
/**
* Checks if a string is a URL
* @example isUrl('https://orcascan.com') // true;
* @param {string} str - value to test
* @returns {boolean} true if URL otherwise false
*/
function isUrl(str) {
return /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(str);
}
@john-doherty
john-doherty / how-to-download-a-file-with-node.js
Last active October 2, 2021 20:19
How to download a file with Node.js
var fs = require('fs-extra');
var fetch = require('node-fetch');
/**
* Download a file to disk
* @example downloadFile('https://orcascan.com', './barcode-tracking.html')
* @param {string} fileUrl - url of file to download
* @param {string} destPath - destination path
* @returns {Promise} resolves once complete, otherwise rejects
*/
@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'))