Skip to content

Instantly share code, notes, and snippets.

View pinkhominid's full-sized avatar

John Ericson pinkhominid

View GitHub Profile
@pinkhominid
pinkhominid / index.js
Created February 11, 2019 15:43
Clean Fetch Example (per Google Devs)
function logResult(result) {
console.log(result);
}
function logError(error) {
console.log('Looks like there was a problem: \n', error);
}
function validateResponse(response) {
if (!response.ok) {
@pinkhominid
pinkhominid / clear-style.css
Last active April 2, 2021 03:00
Clear/reset default styles of common HTML elements
/* Anchors */
a.clear-style {
color: inherit;
text-decoration: none;
}
/* Buttons */
button.clear-style,
input[type=button].clear-style,
input[type=reset].clear-style {
@pinkhominid
pinkhominid / example.js
Last active March 18, 2019 22:10
Parsing a URL (2019-edition)
const encodedDestHref = encodeURIComponent('https://example.com/#/hash/path?hparam1=a&hparam2=b');
location.href = `https://example.com/path?dest=${encodedDestHref}`;
const searchParams = new URLSearchParams(location.search);
const destHref = searchParams.get('dest');
const destUrl = new URL(destHref);
const hashRelativeUrl = new URL(destUrl.hash.substr(1), destUrl.origin); //hack
console.log(hashRelativeUrl.pathname); // '/hash/path'
console.log(hashRelativeUrl.search); // '?hparam1=a&hparam2=b'
@pinkhominid
pinkhominid / index.js
Created May 21, 2019 19:05
One-line AngularJS get service instance (e.g. $http)
angular.element(document.body).injector().get('$http')
@pinkhominid
pinkhominid / check-sha.sh
Created May 24, 2019 01:53
Bash Check SHA256 Example
shasum -a 256 -c <<< 'd42fc3297cff5ffae69b4dd4df628d50b520b26f24d7e7671c0a6b6f30d66b28 *tidy-5.6.0-macos.dmg'
tidy-5.6.0-macos.dmg: OK
@pinkhominid
pinkhominid / index.js
Created June 5, 2019 22:25
fetchJson
function fetchJson(url, options) {
return fetch(url, { cache: 'no-store', ...options })
.then(res => { if (res.ok) return res; throw res; })
.then(res => res.json());
}
@pinkhominid
pinkhominid / example.sh
Last active July 8, 2019 19:46
Quick Git branch checkout when you don't know the complete name
# where xyz is a branch name fragment, e.g. 525 (using a ticket number)
git checkout `git branch | grep xyz`
@pinkhominid
pinkhominid / routes.js
Created July 25, 2019 13:05
UI-Router Boilerplate
(function() {
'use strict';
angular
.module('theApp')
.config(config);
config.$inject = ['$stateProvider', '$locationProvider', '$urlRouterProvider'];
function config($stateProvider, $locationProvider, $urlRouterProvider) {
@pinkhominid
pinkhominid / hide-broken-img.js
Created July 31, 2019 02:26
One-line hide a broken image
window.addEventListener('error', e=>{if (e.target.tagName !== 'IMG') return; e.target.style.visibility = 'hidden';}, true)
@pinkhominid
pinkhominid / fetch-json-retry.js
Last active February 10, 2021 19:52
fetch json with retry
function fetchJSON(url, options, retries = 0) {
return fetch(url, { cache: 'no-store', ...options })
.then(res => {
if (res.ok) return res.json();
if (retries > 0) return fetchJSON(url, options, --retries);
throw res;
});
}