Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

John Doherty john-doherty

🎯
Focusing
Block or report user

Report or block john-doherty

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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) {
@john-doherty
john-doherty / remove-invalid-xml-characters.js
Last active Mar 8, 2019
JavaScript function that removes invalid XML characters from a string according to the spec
View remove-invalid-xml-characters.js
/**
* Removes invalid XML characters from a string
* @param {string} str - a string containing potentially invalid XML characters (non-UTF8 characters, STX, EOX etc)
* @param {boolean} removeDiscouragedChars - should it remove discouraged but valid XML characters
* @return {string} a sanitized string stripped of invalid XML characters
*/
function removeXMLInvalidChars(str, removeDiscouragedChars) {
// remove everything forbidden by XML 1.0 specifications, plus the unicode replacement character U+FFFD
var regex = /((?:[\0-\x08\x0B\f\x0E-\x1F\uFFFD\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))/g;
@john-doherty
john-doherty / yc-startup-descriptions.js
Created Sep 16, 2018
Calculate the average number of words used to describe YC startups via yclist.com
View yc-startup-descriptions.js
// 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 / settimeout-using-requestanimationframe.js
Created Apr 24, 2018
JavaScript window.setTimeout replacement using requestAnimationFrame for better performance
View settimeout-using-requestanimationframe.js
(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 / disable-mac-swipe-navigation.js
Created Feb 12, 2018
Disable Mac swipe navigation in pure JavaScript
View disable-mac-swipe-navigation.js
(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 / cordova-file-storage.js
Created Dec 28, 2017 — forked from Montoya/fileStorage.js
Cordova File API Wrapper
View cordova-file-storage.js
/* modified from https://codingwithspike.wordpress.com/2014/12/29/using-deferreds-with-the-cordova-file-api/ */
/* tested and working in iOS and Android on latest Cordova (5.2.0) and File plugin (4.0.0) */
/* uses dataDirectory which is not synced to iCloud on iOS. You can replace each reference to syncedDataDirectory, but then you will need to set cordova.file.syncedDataDirectory = cordova.file.dataDirectory on Android to maintain compatibility */
window.fileStorage = {
/**
* Saves a file into the dataDirectory on the device
* @param {string} name - filename (can include sub folders)
* @param {string} data - file contents
@john-doherty
john-doherty / javascript-clean-csv-string.js
Last active Oct 11, 2018
Remove empty values and duplicates from a comma separated string in JavaScript
View javascript-clean-csv-string.js
/**
* Remove empty values and duplicates from a comma separated string
* @param {string} value - comma separated value to clean
* @returns {string} cleaned comma separated value
*/
function cleanCSV(value) {
return (value || '').split(',').filter(Boolean).filter(function(item, index, all) {
return (index === all.indexOf(item));
});
}
@john-doherty
john-doherty / javascript-get-styles-by-selector.js
Last active Aug 17, 2017
Get all css style blocks matching a css selector from stylesheets
View javascript-get-styles-by-selector.js
/**
* 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 Sep 28, 2018
Simple, pure JavaScript HTTP Get function (IE8+, Chrome, Safari, Firefox, PhoneGap/Cordova)
View javascript-http-get.js
/**
* 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) {
View raspberry-pi-disable-key.sh
# 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
You can’t perform that action at this time.