Skip to content

Instantly share code, notes, and snippets.

View liamnewmarch's full-sized avatar
🤖
beep boop

Liam Newmarch liamnewmarch

🤖
beep boop
View GitHub Profile
@liamnewmarch
liamnewmarch / binary-text.js
Last active November 9, 2016 14:42
Convert ascii text to and from binary.
class BinaryText {
static encode(string) {
return string.split('').map(char => {
const charCode = char.charCodeAt(0);
return charCode.toString(2);
}).join('');
}
static decode(binary) {
return binary.match(/.{8}/g).map(byte => {
@liamnewmarch
liamnewmarch / date-format.js
Created November 10, 2016 16:24
A simple class for formatting dates.
class DateFormat {
constructor(format) {
this.format = format instanceof String ? format : String(format);
}
get _formatMap() {
return ['yyyy', 'yy', 'mm', 'm', 'dd', 'd', 'hh', 'h', 'ii', 'i', 'ss', 's'];
}
format(date) {
@liamnewmarch
liamnewmarch / crosshairs.js
Created September 25, 2018 11:30
Bookmarklet for quickly adding crosshairs to a page for visually checking alignment
// 1. Create a new bookmark – usually by right-clicking the bookmark toolbar.
// 2. Name it (e.g. ‘crosshair’) and paste the following line in the URL field:
javascript:((d,p)=>{c=d.body.appendChild(d.createElement('canvas')),a=c.getContext('2d'),d.onmousemove=(e)=>{c.width=w=innerWidth;c.height=h=innerHeight;with(c.style)position='absolute',top=left=0,width=w+p,height=h+p,pointerEvents='none';with(a)clearRect(0,0,w,h),fillStyle='#0002',fillRect(0,e.pageY,w,1),fillRect(e.pageX,0,1,h)}})(document, 'px')
@liamnewmarch
liamnewmarch / hue-rotate.js
Last active September 25, 2018 12:11
Hue rotate bookmarklet for Chrome.
javascript:((h,s)=>{(loop=()=>{requestAnimationFrame(loop),s.filter=`hue-rotate(${h=(h+10)%360}deg)`})()})(0,document.all[0].style);
@liamnewmarch
liamnewmarch / store.js
Created January 14, 2020 15:59
Simple event driven store based on Proxy
/**
* Usage example
*
* ```
* const myStore = new Store({
* count: 1,
* });
*
* myStore.addEventListener('change', () => {
* console.log(store.state.count);
@liamnewmarch
liamnewmarch / konami.js
Last active February 14, 2020 09:45
Listens for the Konami code, runs a callback when detected.
/**
* Konami
*
* Listens for the [Konami code][1], runs a callback when detected.
*
* [1]: https://en.wikipedia.org/wiki/Konami_Code
*
* Example usage:
*
* import { konami } from './konami.js/';
@liamnewmarch
liamnewmarch / watchMediaQuery.js
Created April 23, 2020 16:34
Get the initial state of a media query and watch for changes with one easy function.
function watchMediaQuery(media, truthy = true, falsy = false) {
const listeners = [];
const mediaQuery = matchMedia(`(${media}: ${truthy})`);
mediaQuery.addListener(() => {
for (const listener of listeners) listener();
});
return (fn) => {
const listener = () => fn(mediaQuery.matches ? truthy : falsy)
listeners.push(listener);
listener();
@liamnewmarch
liamnewmarch / temp.sh
Created April 27, 2020 17:18
Small utility to start a shell in a temporary directory and delete it afterwards
#!/usr/bin/env bash
BLUE='\033[1;34m'
RESET='\033[0m'
WHITE='\033[1;37m'
echo -e "Creating temp dir..."
tempdir=$(mktemp -d)
pushd $tempdir &>/dev/null
echo -e "${BLUE}!${WHITE} Starting a new shell. Type ${BLUE}exit${WHITE} to return.${RESET}"
@liamnewmarch
liamnewmarch / convert.js
Created March 27, 2018 15:30
Conversion between camelCase and kebab-case, using the DOM.
/**
* Convert kebab-case to camelCase.
* @param {string} kebabString The kebab-case string to convert.
* @return {string} The string converted to camelCase.
*/
function camelCase(kebabString) {
const div = document.createElement('div');
div.setAttribute(`data-${kebabString}`, '');
return Object.keys(div.dataset)[0];
}
@liamnewmarch
liamnewmarch / roman-numerals.js
Last active April 7, 2021 08:13
Convert numbers to roman numerals.
/**
* RomanNumerals singleton class.
*
* Usage:
*
* RomanNumerals.convert(12); // 'XII'
*/
class RomanNumerals {
static numeralValues = {
M: 1000,