Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
LukeChannings / validateHtml.js
Last active July 19, 2018 16:23
HTML Validator snippet
;(function validateHtml() {
const getErrorExampleSnippet = (html, start, end) => {
const lines = html.split('\n')
const linesWeCareAbout = lines.slice(start.line - 1, end.line)
const region = linesWeCareAbout.map((l, i, ls) => {
const isFirstLine = i === 0
const isLastLine = ls.length - 1 === i
@LukeChannings
LukeChannings / browser.swift
Last active July 11, 2018 16:49
browser.swift
import Foundation
print("Scanning for chromecasts")
class BrowserDelegate : NSObject, NetServiceBrowserDelegate {
var chromeCasts: [NetService] = []
func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
let existingChromeCast = chromeCasts.filter({ $0.name == service.name })
// See-Saw problem.
//
// There are 12 people on an island.
// One of these 12 people weighs either more or less than the others.
// You have a see-saw, you can balance infinite people on either side,
// but can only use it a maximum of 3 times. How do you find the person?
// implement a function that returns the height of the odd person.
runPuzzle((people, seeSaw) => {
@LukeChannings
LukeChannings / webpack.config.js
Created December 19, 2017 10:29
.storybook/webpack.config.js
const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const webpack = require('webpack')
const genDefaultConfig = require('@storybook/react/dist/server/config/defaults/webpack.config.js')
module.exports = (storybookBaseConfig, env) => {
const baseConfig = genDefaultConfig(storybookBaseConfig, env)
return Object.assign({}, baseConfig, {
node: {
fs: 'empty',
@LukeChannings
LukeChannings / tagged-regex.js
Last active October 26, 2017 16:14
Tagged regular expressions
const tre = (strings, ...values) => {
const flags = values
.filter(v => v instanceof RegExp)
.map(re => re.flags)
.reduce(
(a, b) =>
a +
b
.split("")
.filter(char => !a.includes(char))
@LukeChannings
LukeChannings / generate-self-signed-certificate.sh
Created July 23, 2017 20:52
Generate self-signed certificates that actually work in Chrome on macOS
#!/usr/bin/env bash
set -e -o pipefail
trap 'rm -rf ssl' INT
export CN="$1"
export C="${C-GB}"
export ST="${ST-England}"
export L="${L-London}"
@LukeChannings
LukeChannings / pythonic-errors.js
Last active June 16, 2017 00:23
pythonic-errors.js
class ErrorMissingForename extends Error {}
class ErrorMissingSurname extends Error {}
const requiredParam = (ErrorType = Error) => {
throw new ErrorType()
}
function concatName (
forename = requiredParam(ErrorMissingForename),
surname = requiredParam(ErrorMissingSurname)
@LukeChannings
LukeChannings / input.html
Created March 21, 2017 11:02
User input from page as stdout
<form name="userinput" action="http://localhost:6789/input" method="GET">
<textarea name="data" id="data" cols="30" rows="10"></textarea>
<button>Go!</button>
</form>
@LukeChannings
LukeChannings / play-video.py
Created February 7, 2017 20:09
play-video.py
#!/usr/bin/env python
import sys
import pychromecast
URL = sys.argv[1]
chromecasts = pychromecast.get_chromecasts()
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Living Room")
@LukeChannings
LukeChannings / spotify-osa.js
Last active September 23, 2016 10:35
Control Spotify with OSA
#!/usr/bin/env osascript -l JavaScript
const Spotify = Application('Spotify')
const log = console.log.bind(console)
function run ([command, ...args]) {
const availableCommands = Object.keys(commands).filter(name => !name.includes('Usage'))
if (availableCommands.includes(command)) commands[command](...args)
else {