Skip to content

Instantly share code, notes, and snippets.

View ngsctt's full-sized avatar

Nathan ngsctt

View GitHub Profile
@ngsctt
ngsctt / toggle-wifi.sh
Created April 27, 2018 07:21
Toggle WiFi power (macOS)
#!/bin/bash
if [[ $(networksetup -getairportpower en0) == *On ]]
then
networksetup -setairportpower en0 off
else
networksetup -setairportpower en0 on
fi
@ngsctt
ngsctt / bookmarklet.js
Last active May 26, 2022 10:49
How to make a bookmarklet that finds the book in a product page on Booko.com.au
(function(){
var r, l, s, a, v, i; // (1)
r = /(?:97[89])?[-\s]?[0-9]{1}[-\s]?[0-9]{3}[-\s]?[0-9]{3}[-\s]?[0-9]{2}[-\s]?[0-9X]{1/; // (2)
l = window.location.href.match(r); // (3)
s = window.getSelection().toString().match(r); // (4)
a = document.activeElement;
if (a.value) { v = a.value.slice(a.selectionStart, a.selectionEnd).match(r) } // (5)
i = ""; // (6)
if (l) { i = l[0] }
if (s) { i = s[0] }
@ngsctt
ngsctt / comphash.sh
Last active October 21, 2019 10:00
Bash functions to: (1) visually compare two strings, and (2) compare a string with a computed SHA/MD5 checksum
compstring () {
STR_A_IN=$1
STR_B_IN=$2
STR_A_OUT=""
STR_B_OUT=""
POINTER=""
LENGTH=${#STR_A_IN}
if [ $# -lt 2 ] || [ -z $1 ]|| [ -z $2 ]; then
echo "You must provide two strings to compare"
return 1
@ngsctt
ngsctt / hotkey eventListener.js
Created October 29, 2019 07:25
Listen for simple hotkeys
const specialKey = navigator.userAgent.indexOf('Macintosh') != -1 ? "metaKey" : "ctrlKey";
const otherKeys = ['altGraphKey','altKey','ctrlKey','metaKey','shiftKey'].filter(k => k !== specialKey);
document.addEventListener('keydown', event => {
if (event.code === key && event[specialKey] === true && !otherKeys.some(k => event[k] === true)) {
// Do something
event.preventDefault();
}
});
@ngsctt
ngsctt / makefile
Created January 22, 2020 09:18
Makefile for simple pug-based templating projects
SHELL := /bin/zsh
DEST ?= print
PORT ?= 8888
WATCH ?= *.pug, *.less
BUILD ?= pug --out $(DEST) [^_]*.pug
includes := $(shell cat _include)
build: copy convert
@ngsctt
ngsctt / romanise.js
Created January 22, 2020 16:55
JavaScript to convert numbers between 1 and 1,999,999 to roman numerals
function romanise (number, upper=true) {
if (number > 1999999) {
console.warn('WARN: attempted to convert number over 1,999,999 to roman numeral');
return number;
}
const letters = upper ? [
['X', 'V', 'I'],
['C', 'L', 'X'],
['M', 'D', 'C'],
['X̅', 'V̅', 'M'],
@ngsctt
ngsctt / convert_to_pnpm.md
Last active February 5, 2020 10:22
A simple shell script to convert existing NPM-installed modules to PNPM modules

What

A simple shell script to convert existing NPM-installed modules to PNPM modules

Why

NPM results in a lot of modules being repeatedly installed on the machine, whereas PNPM caches them locally, saving space for pointless duplicates

How

source convert_to_pnpm.sh
namespace "version" do
path = 'lib/gem-name/version.rb'
content = File.read(path)
regex = /(VERSION\s*=\s*['"])([0-9.]+)(['"])/
match = content.match(regex)
version = match[2]
vparts = version.split('.')
major = vparts[0] ? Integer(vparts[0]) : 0
minor = vparts[1] ? Integer(vparts[1]) : 0
patch = vparts[2] ? Integer(vparts[2]) : 0
@ngsctt
ngsctt / isoTimeStamp.js
Last active July 18, 2020 08:18
JavaScript function to generate an ISO 8601 or RFC 3339 timestamp. MIT licence.
function isoTimeStamp(date, options = {}) {
console.log(options)
let defaults = {
date_time: 'T',
time_offset: ''
}
Object.assign(options, Object.assign(defaults, options));
console.log(options)
const year = date.getFullYear();
const month = String(date.getMonth()).padStart(2, '0');