Skip to content

Instantly share code, notes, and snippets.

View jwrigh26's full-sized avatar

Justin Wright jwrigh26

View GitHub Profile
@jwrigh26
jwrigh26 / TimerWithGCD.md
Last active September 5, 2016 19:29
Creating a timer with Grand Central Dispatch

##Creating a timer with Grand Central Dispatch

At the following is the implementation file of a sample class that shows, how to make a timer with the help of Grand Central Dispatch. The timer fires on a global queue, just change the queue to the main queue or any custom queue and the timer fires on this queue and not on the global queue anymore.

This was modified for swift 2.2 Also created an example of a background global queue variable. As well as a callback in the start timer so you can do some cool stuff everytime the timer is called.

@jwrigh26
jwrigh26 / useReducer
Last active May 3, 2019 23:08
useReducer in a custom hook example
import {useReducer} from 'react';
export const rowAction = {
add: 'ACTION_ADD',
approve: 'ACTION_APPROVE',
edit: 'ACTION_EDIT',
note: 'ACTION_NOTE',
reset: 'ACTION_RESET',
};
@jwrigh26
jwrigh26 / useLongPress
Created September 24, 2019 17:56
React gist for a custom hook: useLongPress
import {useState, useEffect, useCallback} from 'react';
export function useLongPress(callback = () => {}, ms = 400) {
const [startLongPress, setStartLongPress] = useState(false);
useEffect(() => {
let timerId;
if (startLongPress) {
timerId = setTimeout(callback, ms);
} else {
@jwrigh26
jwrigh26 / useScrollListener
Created September 24, 2019 17:57
React custom hook for listening to window scroll event
import {useEffect} from 'react';
export default function useScrollListener(callback = () => {}, ms = 150) {
let onFinishedId;
let scrollStarted = false;
let scrollDirection = 'down';
let scrollTop = 0;
let prevScrollTop = scrollTop;
useEffect(() => {
@jwrigh26
jwrigh26 / getScrollbarWidth
Created September 25, 2019 16:16
Get Scrollbar width
function getScrollbarWidth() {
// Creating invisible container
const outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.overflow = 'scroll'; // forcing scrollbar to appear
outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
document.body.appendChild(outer);
// Creating inner element and placing it in the container
const inner = document.createElement('div');
{
"Print to console": {
"prefix": "lgvar",
"body": [
"console.log('$1', $1);$0"
],
"description": "Log output to console"
},
"Print String to console": {
"prefix": "lg",
{
"breadcrumbs.enabled": true,
"editor.tabSize": 2,
"eslint.packageManager": "yarn",
"files.autoSave": "onFocusChange",
"javascript.format.enable": false,
// "editor.lineNumbers": "relative",
"editor.formatOnSave": false,
"eslint.autoFixOnSave": true,
// Set the default
@jwrigh26
jwrigh26 / gist:82b71da5b5994112f816a3b4e86babc5
Created March 21, 2020 00:36
Local & Session Storage Helper
export default function storage(isSession = false) {
const manager = isSession ? sessionStorage : localStorage;
const hasManager = isSession ? window.sessionStorage : window.localStorage;
function getItem(id, json = true) {
if (hasManager) {
return json ? JSON.parse(manager.getItem(id)) : manager.getItem(id);
}
return undefined;
}
@jwrigh26
jwrigh26 / removeItem.js
Last active June 3, 2021 22:06
Remove Object Properties with Destructing
// Helper method to delete without mutating
// Why not use delete? It's a mutable operation (side-effects).
// By using object destructuring, we have a one-liner approach.
// Inspired by: https://ultimatecourses.com/blog/remove-object-properties-destructuring
const removeItem = (key, { [key]: _, ...obj }) => obj;
// How does this work?
// We pass in the name of the key we wish to remove from the object.
// For the second param, we pass in the object
@jwrigh26
jwrigh26 / Regex Example
Created July 30, 2021 16:52
An example of using regex in React/node.js
useEffect(() => {
const pathname = location?.pathname;
const pathNameChanged = hasValue(pathname) && pathname !== prevPathname;
const managedPaths = {
// regex looks for the id and value of foo
create: () => {
const regex = new RegExp(`^(?:.*)(${id})(?:(?:.*)(${foo}))?`);
const results = regex.exec(pathname) ?? [];