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 / add-query-params-to-url.js
Last active January 18, 2022 23:25
Add query string parameters to a URL in JavaScript (works with browser/node, merges duplicates and works with absolute and relative URLs)
/**
* 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 / local-storage-helper.js
Last active March 10, 2024 19:41
localStorage helper (takes care of casting types)
/**
* local storage helper (takes care of casting types)
*/
var localStorageHelper = {
/**
* Check if item exists in local storage.
* @param {string} key - variable name
* @return {boolean} true if key exists, otherwise false
*/
@john-doherty
john-doherty / url-to-filename.js
Last active May 23, 2024 13:49
Convert a URL into a readable filename safe string
/**
* Convert a URL into a readable filename safe string
* - sort query params
* - remove protocol
* - replace / with !
* - replace ? with !!
* - replace remaining illegal characters with -
* @example
* INPUT: convertUrlToReadableFilename('https://orcascan.com/guides/how-to-scan-barcodes-into-microsoft-excel-59fd67f9');