Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

John Doherty john-doherty

🎯
Focusing
View GitHub Profile
@john-doherty
john-doherty / add-query-params-to-url.js
Last active Jan 18, 2022
Add query string parameters to a URL in JavaScript (works with browser/node, merges duplicates and works with absolute and relative URLs)
View add-query-params-to-url.js
/**
* Adds query params to existing URLs (inc merging duplicates)
* @param {string} url - src URL to modify
* @param {object} params - key/value object of params to add
* @example
* // returns /guides?tag=api
* addQueryParamsToUrl('/guides?tag=hardware', { tag:'api' })
* @example
* // returns https://orcascan.com/guides?tag=api
* addQueryParamsToUrl('https://orcascan.com/guides?tag=hardware', { tag: 'api' })
@john-doherty
john-doherty / how-to-download-a-file-with-node.js
Last active Oct 2, 2021
How to download a file with Node.js
View 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-isurl.js
Created Mar 23, 2021
Check if a string is a URL in JavaScript
View javascript-isurl.js
/**
* 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 / javascript-is-empty.js
Last active Feb 19, 2021
Check if a variable is really empty in JavaScript
View javascript-is-empty.js
/**
* 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 / javascript-wait-for-events.js
Last active Feb 11, 2022
Wait for multiple JavaScript events to fire
View javascript-wait-for-events.js
/**
* Waits for multiple DOM events to fire
* @example
* waitForEvents(window, 'DOMContentLoaded,online').then(function(result) {
* // do stuff
* });
* @param {object} eventTarget - element to listen for events on (default document)
* @param {Array<string>|string} eventNames - array or csv string of event names
* @returns {Promise<Array>} resolves with array of event objects
*/
@john-doherty
john-doherty / javascript-promises-in-sequence.js
Last active Jan 21, 2021
Execute promises sequentially, returning the result of each similar to Promise.all
View javascript-promises-in-sequence.js
/**
* Executes an array of functions that return a promise in sequence
* @example
* promisesInSequence([
* function() { return Promise.resolve(1); }
* function() { return Promise.resolve(2); },
* function() { return Promise.resolve(3); }
* ])
* .then(function(result) {
* console.log(result.join(',')); // output = 1,2,3
@john-doherty
john-doherty / puppeteer-waitforevent.js
Last active Jan 1, 2021
puppeteer wait for an event to fire
View puppeteer-waitforevent.js
/**
* Wait for the browser to fire an event (including custom events)
* @param {string} eventName - Event name
* @param {integer} seconds - number of seconds to wait.
* @returns {Promise} resolves when event fires or timeout is reached
*/
async function waitForEvent(eventName, seconds) {
seconds = seconds || 30;
@john-doherty
john-doherty / get-nearest-html-attribute.js
Created Aug 29, 2020
Gets attribute off HTML element or nearest parent
View get-nearest-html-attribute.js
/**
* 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 / cordova-file-storage.js
Last active Apr 30, 2022
An easy way to read/write files in Cordova
View cordova-file-storage.js
var fileStorage = {
/**
* Saves a file on the device
* @param {string} name - filename (can include sub folders)
* @param {string} data - file contents
* @param {boolean} useSandbox - uses protected sandbox if true, otherwise external (default false)
* @returns {Promise} executes .then with saved file path as first param
*/
write: function (name, data, useSandbox) {
@john-doherty
john-doherty / javascript-string-to-json-safe-key.js
Created Jun 16, 2019
Converts a string to JSON safe key (property name) by removing unsafe characters and converting to camel case
View javascript-string-to-json-safe-key.js
/**
* 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) {