Skip to content

Instantly share code, notes, and snippets.

View n8jadams's full-sized avatar

Nate Adams n8jadams

View GitHub Profile
@n8jadams
n8jadams / GraphQLQueryChecker.php
Last active October 11, 2019 14:12
GraphQLQueryChecker. A PHP Class that checks if a field is requested in the query. I've used this in production with webonyx/graphql-php v0.9.14 for more than a year.
<?php
/**
* Instructions:
* In an Object Resolve function, use the fourth argument ($info)
*
* Example usage:
*
* $fieldInQuery = GraphQLQueryChecker::isFieldInQuery('some.nested.fieldname', $info);
*
@n8jadams
n8jadams / focusAndOpenKeyboard.js
Last active October 11, 2019 14:11
Focus And Open Keyboard. This is a nice cross-browser-compatible function that will focus on an input element and open the keyboard (on mobile devices). Especially useful for modals on IOS devices.
/*
// Example usage
var myElement = document.getElementById('my-element');
var modalFadeInDuration = 300;
focusAndOpenKeyboard(myElement, modalFadeInDuration); // or without the second argument
*/
function focusAndOpenKeyboard(el, timeout) {
if(!timeout) {
timeout = 100;
@n8jadams
n8jadams / execAndPipeOutput.php
Last active February 9, 2021 21:18
Execute a command and pipe its output to stdout (echo it) as it's recieved, rather than wait for the command to finish executing
<?php
function execAndPipeOutput($cmd)
{
echo PHP_EOL;
$proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes);
while(($line = fgets($pipes[1])) !== false) {
fwrite(STDOUT, $line);
}
while(($line = fgets($pipes[2])) !== false) {
@n8jadams
n8jadams / mac-keybindings-on-windows.ahk
Last active February 9, 2020 14:44
These Autohotkey keybindings to help make your Windows feel like a Mac.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
; Screenshot (you still have to paste it into paint or whatever)
>#+3::Send {PrintScreen};^+3
>#+4::Send {PrintScreen};^+4
<#+3::Send {PrintScreen};^+3
<#+4::Send {PrintScreen};^+4
@n8jadams
n8jadams / extendedGlobal-extendedWindow.ts
Last active March 5, 2020 17:33
In Typescript, use the global object in Node, or the window object in the browser
/* eslint-disable @typescript-eslint/no-explicit-any */
// Nodeland
interface ExtendedGlobal extends NodeJS.Global {
someGlobalKey: string
}
export const extendedGlobal: ExtendedGlobal = global as any
// Browserland
@n8jadams
n8jadams / lint-format-staged.js
Last active July 8, 2021 17:15
Run eslint and prettier against your staged changes. Great when used as a precommit hook. (Assumes it's placed in top level of repo)
const path = require('path')
const { execSync, spawnSync } = require('child_process')
const prettier = require('prettier')
const fs = require('fs')
const micromatch = require('micromatch')
const lockfile = path.resolve(__dirname, 'linter-script-lockfile')
const eslintFileTypes = ['js', 'jsx', 'ts', 'tsx']
@n8jadams
n8jadams / lint-staged.js
Last active July 8, 2021 17:15
Run eslint against your staged changes (Assumes this is placed at the top level of your repo)
const path = require('path')
const { execSync, spawnSync } = require('child_process')
const fs = require('fs')
const micromatch = require('micromatch')
const lockfile = path.resolve(__dirname, 'linter-script-lockfile')
const eslintFileTypes = ['js', 'jsx', 'ts', 'tsx']
function readGlobsFromFile(filename) {
@n8jadams
n8jadams / react-router-dom-custom-prompt.tsx
Last active September 25, 2020 15:50
Custom <Prompt /> for React Router Dom
// Instead of using the <Prompt />'s implementation
// (which uses the browser's 'confirm()',) use your own custom component!
import React, { useState, useEffect, useRef } from 'react'
import { useHistory } from 'react-router-dom'
import { Modal } from './Modal' // your custom component
interface CustomPromptProps {
when: boolean
onConfirmNavigation?: () => void
@n8jadams
n8jadams / copy-to-clipboard.ts
Created September 25, 2020 15:42
Simple function to copy some text to the clipboard in the browser
function copyToClipboard(textToCopy: string): void {
const tmpInputEl = document.createElement('input')
tmpInputEl.value = textToCopy
tmpInputEl.type = 'text'
tmpInputEl.style.position = 'absolute'
tmpInputEl.style.left = '9000vw'
document.body.appendChild(tmpInputEl)
tmpInputEl.select()
document.execCommand('copy')
document.body.removeChild(tmpInputEl)
@n8jadams
n8jadams / sibling-gift-exchange.ts
Last active November 10, 2021 13:16
A quick typescript function to assign siblings in our yearly gift exchange
interface Participant {
readonly name: string
assignment?: Participant
}
const allParticipants: Participant[] = [
{ name: 'Nate' },
{ name: 'Kyle' },
{ name: 'Zach' },
{ name: 'Bryce' },