Skip to content

Instantly share code, notes, and snippets.

View cahilfoley's full-sized avatar
🚀

Cahil Foley cahilfoley

🚀
  • Rio Tinto
  • Perth, Western Australia
View GitHub Profile
node -e "require('readline').createInterface({input:process.stdin,output:process.stdout,historySize:0}).question('Input> ',p => { b64=Buffer.from(p.trim()).toString('base64');console.log(b64);process.exit(); })"
@cahilfoley
cahilfoley / isPortAvailable.ts
Last active May 13, 2020 04:45
Async function to check if port is available in node.js
/**
* This is a modern rewrite of this gist https://gist.github.com/timoxley/1689041 using promises
*/
import { createServer } from 'net'
export async function isPortAvailable(port: number): Promise<boolean> {
return await new Promise((resolve, reject) => {
const testServer = createServer()
@cahilfoley
cahilfoley / Semaphore.ts
Created February 25, 2020 03:10
Simple TypeScript implementation of a semaphore
import { cpus } from 'os'
/**
* A lock that is granted when calling [[Semaphore.acquire]].
*/
type Lock = {
release: () => void
}
/**
@cahilfoley
cahilfoley / ElevatorSaga.js
Created August 2, 2019 14:49
Elevator Saga
{
init: function(elevators, floors) {
const priorities = {
CAPACITY: 'CAPACITY',
DISTANCE: 'DISTANCE',
WAITING: 'WAITING'
}
const priority = priorities.DISTANCE
const needsPickup = floors.reduce((acc, floor) => {
acc[floor.floorNum()] = {
@cahilfoley
cahilfoley / useBoundingRect.ts
Last active June 14, 2019 13:32
[React Hooks] Random React hooks that I've come across and thought were usefull #react #hooks
import { useCallback, useLayoutEffect, useState, useRef } from 'react'
/**
* Returns the bounding client rect of a HTML element, uses the `ResizeObserver` api if available to detect changes to the
* size. Falls back to listening for resize events on the window.
*/
export function useBoundingRect<T extends HTMLElement>(): [React.Ref<T>, ClientRect | DOMRect] {
const ref = useRef<T>()
const [rect, setRect] = useState()
@cahilfoley
cahilfoley / SharedEventListener.ts
Last active June 14, 2019 13:31
[Browser Utilities] Random utility functions that are useful when working in the browser #browser
type EventHandler<E extends keyof DocumentEventMap> = (event: DocumentEventMap[E]) => void
export class SharedEventListener<E extends keyof DocumentEventMap> {
private subscribers: EventHandler<E>[]
public readonly target: HTMLElement
public readonly event: E
constructor(event: E, target = document.documentElement) {
this.event = event
this.target = target
@cahilfoley
cahilfoley / boxstarter_dev_env.ps1
Last active November 12, 2019 11:35
[Development Environment] A Boxstarter script to setup a standardised development environment #boxstarter #dev #env #setup
# First run this command from an elevated powershell to install chocolatey
# Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
cinst Boxstarter -y
Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowFileExtensions -DisableOpenFileExplorerToQuickAccess
Enable-RemoteDesktop
Disable-BingSearch
Disable-UAC
@cahilfoley
cahilfoley / detect-browser.js
Last active June 14, 2019 13:21
[Platform Detection] Snippets to detect browser, operating system and device information from JavaScript #browser #platform #featuredetection
/**
* Gets whether running on a certain browser according to browser data.
* Functions to test for a certain browser (each returns bool):
* * isOpera
* * isFirefox
* * isSafari
* * isIE
* * isEdge
* * isChrome
* * isBlink
@cahilfoley
cahilfoley / config.md
Last active June 14, 2019 13:14
[Linux Snippets] Useful snippets for developing in linux #linux #node
@cahilfoley
cahilfoley / ensureAdmin.ps1
Last active June 14, 2019 13:19
[Powershell] Usefully scripts when working with Powershell #powershell #admin
#########################################################
# Ensure PS script is running with elevated permissions #
#########################################################
# Works in Powershell >= v4.0, add to top of file
#Requires -RunAsAdministrator
# Works in all versions of powershell, add to top of file