Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
LukeChannings / convertMongoURIToMongoClient.js
Created August 21, 2014 16:25
convertMongoURIToMongoClient
function convertMongoURIToMongoClient(u) {
u = u.replace('mongodb://','')
var a = u.split('/')
, b = (a.length && a[0].split('@'))
, c = (b.length && b[0].split(':'))
, d = (b.length === 2 && b[1].split(','))
return d.map(function(_d) {
var d = _d.split(':')
return (function(o,s) {
@LukeChannings
LukeChannings / Vagrantfile
Created October 3, 2014 16:05
Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "web", primary: true do |web|
web.vm.hostname = "dev-env"
web.vm.box = "trusty32"
web.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box"
@LukeChannings
LukeChannings / handlebars-select-helper.js
Last active August 5, 2024 20:15
A select helper for handlebars
Handlebars.registerHelper("select", function(value, options) {
return options.fn(this)
.split('\n')
.map(function(v) {
var t = 'value="' + value + '"'
return ! RegExp(t).test(v) ? v : v.replace(t, t + ' selected="selected"')
})
.join('\n')
})
@LukeChannings
LukeChannings / rotate-points.js
Created November 3, 2015 15:02
Rotate 2D points
/**
* rotate points around an origin
* @param {Array<object>} ps a list of points
* @param {Number} a angle in degrees
* @param {Object} o origin object
* @return {Array<object>} a list of rotated points
*/
function rotatePoints(ps, a, o) {
return ps.map(function(p) {
return {
// ensure the keys being passed is an array of key paths
// example: 'a.b' becomes ['a', 'b'] unless it was already ['a', 'b']
const keys = ks => Array.isArray(ks) ? ks : ks.split('.')
// traverse the set of keys left to right,
// returning the current value in each iteration.
// if at any point the value for the current key does not exist,
// return the default value
const deepGet = (o, kp, d) => keys(kp).reduce((o, k) => o && o[k] || d, o)
@LukeChannings
LukeChannings / tmux.conf
Last active January 18, 2016 19:23
tmux.conf
# status bar
set-option -g status-utf8 on
### COLOUR (Solarized dark)
# default statusbar colors
set-option -g status-bg black #base02
set-option -g status-fg yellow #yellow
set-option -g status-attr default
@LukeChannings
LukeChannings / flattenVNodePrototype.js
Created April 15, 2016 18:18
Function to prepare a virtual-dom tree to be sent via postMessage
const flattenVNodePrototype = (o) => {
const proto = Object.getPrototypeOf(o)
return !proto ? o : Object.assign(o, proto, o.children && {
children: o.children.map(flattenVNodePrototype)
})
}
@LukeChannings
LukeChannings / annoying.js
Created April 22, 2016 21:04
annoying.js
// silly
const a = [1, 2, 3]
let b = []
a.forEach((item, index) => b[index] = item * 2)
// repugnant
const a = [1, 2, 3]
let b = []
@LukeChannings
LukeChannings / serveVideo.js
Created May 12, 2016 15:42
serveVideo.js
const fs = require('fs')
const http = require('http')
const VIDEO_PATH = './dive-desktop.mp4'
const VIDEO_LENGTH = 13063532
http.createServer(function serveVideo(req, res) {
if (req.headers.range) {
const range = req.headers.range.replace('bytes=', '').split('-')
const start = +range[0]
@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 {