Skip to content

Instantly share code, notes, and snippets.

View quickshiftin's full-sized avatar

Nathan Nobbe quickshiftin

View GitHub Profile
@quickshiftin
quickshiftin / shared-promise.js
Created December 15, 2023 17:15
Multiple clients that need access to an async resource can share a single call via promises
// if several clients latch on to the same promise, do they all receive the results?
let thePromise = null;
const getSharedPromise = () => {
// first time invocation, spin up the async activity
// (connect to db, fetch crypto price quote, etc, etc)
if(thePromise === null) {
thePromise = new Promise((resolve, reject) => {
setTimeout(() => {
@quickshiftin
quickshiftin / feline-bmi.js
Created June 18, 2018 01:21
Calculate your cat's FBMI
/**
* https://www.embracepetinsurance.com/waterbowl/article/measuring-my-cats-body-mass-index-the-fbmi
*
* ribCage float Circumference of your cat's rib cage in inches. (The level of the 9th rib is ideal).
* legLength float Length in inches of your cat's rear leg from knee to ankle.
*
* If the result is >= 42, your cat is overweight!
*/
function catBmi(ribCage, legLength) {
return (ribCage / .7062 - legLength) / .9156 - legLength;
@quickshiftin
quickshiftin / magento2-get-access-token.php
Created April 26, 2018 15:29
Example fetching an OAuth access token in Magento2
// Using thephpleague/oauth1-client
// https://github.com/thephpleague/oauth1-client
require_once __DIR__.'/../../vendor/autoload.php';
// Create server
$server = new League\OAuth1\Client\Server\Magento(array(
'identifier' => $data['consumerKey'],
'secret' => $data['consumerSecret'],
'callback_uri' => "http://mage-access.local/oath.php/",
@quickshiftin
quickshiftin / copy-to-clipboard.js
Created April 9, 2018 15:26
Copy to clipboard in Javascript using document.execCommand('copy')
/**
* @param {string} value The text to copy to the clipboard
* This method is designed to circumvent the inability to copy test from an inactive textarea,
* Hopefully it gets the keyboard to drop off on mobile too
*/
copyToClipboard = () => {
const tmpTextArea = document.createElement('textarea');
const newContent = document.createTextNode(this.props.getText());
tmpTextArea.appendChild(newContent);
tmpTextArea.style.width = 0;
@quickshiftin
quickshiftin / substr-to.php
Last active March 13, 2018 23:53
substr_to - find substring up to given character in php
<?php
/**
* This is such a common thing, I decided to put it in a gist so I don't have to keep reinventing it every time LOL.
*/
function substr_to($haystack, $needle) {
$iPos = strpos($haystack, $needle);
if($iPos === false) {
return false;
}
return substr($haystack, 0, $iPos);
@quickshiftin
quickshiftin / launch-php-xdebug-cli.sh
Created November 4, 2017 16:58
Launch xdebug on a CLI script on a server you are connected to via SSH
#!/bin/bash
php -d xdebug.remote_autostart=1 -d xdebug.remote_host=$(echo $SSH_CLIENT | sed 's/ .*//') path-to-php-script.php
@quickshiftin
quickshiftin / letsencrypt-revoke-cert.sh
Last active October 21, 2017 21:50
Revoke Letsencrypt certificate
#/bin/bash
# http://disq.us/p/1k1ww01
cert_path=$1 # eg /etc/letsencrypt/live/example.com/cert.pem
key_path=$2 # eg /etc/letsencrypt/live/example.com/privkey.pem
certbot revoke --cert-path "$1" --key-path "$2"
//--------------------------------------------------------------------
// http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Pretend calls to a service
//--------------------------------------------------------------------
function getDeparture() {
return new Promise((resolve) => {
setTimeout(function() {
#!/bin/bash
# Locate files relative to current working directory
# Potentially faster than find for deep directories
function rlocate
{
locate "$1" | grep "$(pwd)"
}
@quickshiftin
quickshiftin / thisdir.sh
Created October 5, 2015 15:07
echo the name of the current director (without leading directories)
alias thisdir='basename "$(pwd)"'