Skip to content

Instantly share code, notes, and snippets.

View gabemeola's full-sized avatar
🐛

Gabe gabemeola

🐛
View GitHub Profile
@gabemeola
gabemeola / getValue.d.ts
Last active January 28, 2019 22:26
Gets object type value if found. Otherwise uses default
type GetValue<Obj, Key extends keyof Obj | undefined, Default> = Key extends undefined
? Default
: Obj[Key];
/** Usage */
type BlueDefault = '#0078d7';
type BlueShades = {
50: '#e6f1fb',
@gabemeola
gabemeola / betterFetch.ts
Last active July 10, 2022 15:55
Better... Fetch
type FetchRes<T> = Response & {
data: T;
};
/**
* A Fetch Request that errors on non 2xx status codes
*/
export default function betterFetch<T>(
input: RequestInfo,
init?: RequestInit
@gabemeola
gabemeola / wait.js
Created December 20, 2018 22:20
Resolves passed function after timeout
/**
* Resolves passed function after timeout
*
* @param {number} timeout
* @param {Function} fn
* @return {Promise<any>}
*/
export default function wait(timeout, fn) {
return new Promise((resolve) => {
setTimeout(() => resolve(fn()), timeout);
@gabemeola
gabemeola / memoizeAsync.js
Last active March 10, 2022 05:53
Memoize an Async function
/**
* Memoize a async function (any function which returns a promise)
*
* @param {Function} func - Function which returns a promise
* @param {boolean} [cacheError=false] - Should cache promise rejections
* @param {Function} [resolver] - Resolves cache key. Uses first arg as default
* @returns {Function.<Promise>} - Returns memoized function
*/
export default function memoizeAsync(func, cacheError = false, resolver) {
const cache = new Map();
/**
* Coverts a Javascript string to a
* unsigned 8 byte int ArrayBuffer
*
* @param {string} string - Binary String
* @returns {Uint8Array}
*/
export default function stringToArrayBuffer(string) {
const { length } = string;
const buffer = new Uint8Array(length);
@gabemeola
gabemeola / combineReducersWithRoot.js
Created October 25, 2018 20:09
Combines Redux reducers with a top level root reducer
/**
* Combines reducers with root reducer
*
* @param {function(Object, Object): Object} rootReducer
* @param {Object<string, function(Object, Object): Object>} [reducers={}]
* @returns {function(Object, Object): Object}
*/
export default function combineReducersWithRoot(rootReducer, reducers = {}) {
const entries = Object.entries(reducers);
@gabemeola
gabemeola / truncateToDecimal.js
Created October 18, 2018 21:48
truncate float to decimal point
export default function truncateToDecimal(number, precision = 0) {
const floatStr = number.toString();
const index = floatStr.indexOf('.');
// Return number if not a float
if (index === -1) {
return number;
}
// Truncate the string starting at the decimal plus any added precision
// Increment by 1 to include the specified precision
const truncated = floatStr.substring(0, index + precision + 1);
@gabemeola
gabemeola / middleware.go
Last active September 18, 2018 21:30
Creates a connect style middleware chain
package middlware
import (
"fmt"
"log"
"net/http"
)
// MiddlewareNext invokes the next middleware
type MiddlewareNext func()
@gabemeola
gabemeola / clickKeyPress.js
Last active May 14, 2019 23:37
Higher order function which handles key presses
/**
* Higher order function which handles
* key presses "onKeyDown".
*
* Passes event to function.
*
* @param {Function} fn - Function to be invoked with event
* @return {function(Event): any}
*/
const clickKeyPress = (fn) => (ev) => {
@gabemeola
gabemeola / getCommentsByText.js
Last active August 2, 2018 20:02
Searches for a comment in target document
/**
* Searches for a comment in target document
*
* @author Gabe M
* @param {string} commentText - Comment text to search for
* @param {Node} [target=document] - Parent target to search through
* @return {Array<Node>}
*/
function getCommentsByText(commentText, target = document) {
const nodeList = [];