Skip to content

Instantly share code, notes, and snippets.

View dualcyclone's full-sized avatar

Luke Dyson dualcyclone

View GitHub Profile
@dualcyclone
dualcyclone / hexColorConverter.ts
Created October 7, 2021 13:30
Convert hex colour to RGB values, and utility to create a CSS RGBA value from a hex and provided alpha value
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
const rgbRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
/**
* Converts a CSS hex value into an RGB array
*/
export const hexToRgb = (hex: string): number[] | null => {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const result = rgbRegex.exec(
hex.replace(shorthandRegex, (_, red: string, grn: string, blu: string) => `${red}${red}${grn}${grn}${blu}${blu}`)
@dualcyclone
dualcyclone / MultiExtend.js
Last active November 20, 2018 10:17
Helper to extend a ES6 class from multiple source classes
const multiExtend = (targetInstance, SourceClass, ...args) => {
const sourceInstance = new SourceClass(...args)
const sourceDescriptors = Reflect.ownKeys(SourceClass)
.filter(key => !['constructor', 'name'].includes(key))
.reduce((acc, key) => ([...acc, { key, descriptor: Object.getOwnPropertyDescriptor(SourceClass, key) }]), [])
const sourceInstanceDescriptors = Reflect.ownKeys(sourceInstance)
.reduce((acc, key) => ([...acc, { key, descriptor: Object.getOwnPropertyDescriptor(sourceInstance, key) }]), [])
sourceDescriptors.concat(sourceInstanceDescriptors).forEach(({ key, descriptor }) => {
Object.defineProperty(targetInstance, key, descriptor)
@dualcyclone
dualcyclone / Websocket.js
Created November 8, 2018 11:05
WebSocket with readyState$ and readyState utility streams
import { interval } from 'rxjs'
import { distinctUntilChanged, mapTo, share } from 'rxjs/operators'
export const WEBSOCKET_READYSTATES = {
NOOP: null, // Unused in example, but useful to identify when a websocket doesn't exist (ie, not operational)
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3
}
@dualcyclone
dualcyclone / BetterTouchTool config screenshot.png
Last active July 31, 2018 08:25
BetterTouchTool configuration including Google Photos, Google Keep, and Deezer now playing with controllers
BetterTouchTool config screenshot.png
@dualcyclone
dualcyclone / test-using-sinon-useFakeTimers.js
Last active January 16, 2023 07:19 — forked from wrumsby/test-simple.js
Example of how to use Sinon's useFakeTimers.
/*global define, describe, it, before, after */
define(['chai', 'sinon', 'boilerplate/simple'], function (chai, sinon, simple) {
'use strict';
var assert = chai.assert;
describe('simple', function () {
describe('stamp', function () {
var clock;
var mockDate;
@dualcyclone
dualcyclone / get_source_branch.sh
Created April 4, 2017 10:57
A script to find the source branch for a branch in Git.
#!/bin/sh
# Script to find the parent branch for a given branch name
# If no branch name is specified, then it'll use the existing branch
# If you specify a branch to find the source of that doesn't exist, the script will exit with an error exit code
currentbranch=`git branch | grep '*' | sed 's/\* //'`
exists=`git show-ref refs/heads/$1`
if [ "$1" != "" ] && [ -z "$exists" ]; then
echo "You've specified a branch that doesn't exist"