Skip to content

Instantly share code, notes, and snippets.

@hubgit
hubgit / NOTES.md
Created June 25, 2020 20:11
Kubernetes Dashboard user
@hubgit
hubgit / ns-resolver.ts
Last active June 24, 2020 14:02
Creating a namespace resolver for document.evaluate
// from the doc or node being parsed
const namespaceResolver = doc.createNSResolver(doc)
// from XML you control
const namespaceResolverDoc = new DOMParser().parseFromString(
`<TEI xmlns="http://www.tei-c.org/ns/1.0"/>`,
'application/xml'
)
@hubgit
hubgit / execfile-promise-pipe.js
Created June 17, 2020 15:36
Pipe input to a promisified execFile command
const promise = promisify(execFile)('foo', {
encoding: 'buffer', // remove this to get the output as a string
})
promise.child.stdin.write(input)
promise.child.stdin.end()
const { stdout: output } = await promise
@hubgit
hubgit / json-ld.js
Created June 16, 2020 09:16
Fetch, extract, parse, expand, frame and compact JSON-LD
const { JSDOM } = require('jsdom')
const { compact, expand, frame } = require('jsonld')
const url = 'https://www.bbc.co.uk/schedules/p00fzl6p/2020/06/14'
// fetch and parse HTML
const { window: { document } } = await JSDOM.fromURL(url)
// select the script elements containing JSON-LD
const elements = document.querySelectorAll('script[type="application/ld+json"]')
@hubgit
hubgit / query.js
Created May 16, 2020 18:43
MongoDB aggregate query with lookup and unwind
const items = await Item.aggregate([
{
$project: {
body: 0,
},
},
{
$lookup: {
from: 'users',
localField: 'uid',
const config = { iceServers: [ { url: 'stun:stun.l.google.com:19302' } ] }
const [localConnection, remoteConnection] = [new RTCPeerConnection(config), new RTCPeerConnection(config)]
const handleIceCandidate = connection => event => event.candidate && connection.addIceCandidate(new RTCIceCandidate(event.candidate))
localConnection.onicecandidate = handleIceCandidate(remoteConnection)
remoteConnection.onicecandidate = handleIceCandidate(localConnection)
const [localVideo, remoteVideo] = [document.createElement('video'), document.createElement('video')]
@hubgit
hubgit / convert-emf-png.sh
Last active March 24, 2020 11:52
Batch convert EMF to PNG with Inkscape
export INKSCAPE=/Applications/Inkscape.app/Contents/MacOS/inkscape
# ${INKSCAPE} --help
for i in *.emf; do "${INKSCAPE}" --without-gui --export-file="${i}.png" --export-dpi=72 "${i}"; done
@hubgit
hubgit / barb-weekly-viewing-figures.js
Created February 4, 2020 18:02
Average UK weekly television viewing figures from BARB
const axios = require('axios')
const qs = require('qs')
const fs = require('fs-extra')
const thisYear = (new Date).getFullYear()
const fetch = async () => {
const years = []
for (let year = 1992; year <= thisYear; year++) {
@hubgit
hubgit / now-php.sh
Created January 27, 2020 13:28
Deploy a PHP endpoint to now.sh
# Install `now`
yarn global add now
# Install `jo`
brew install jo
# Create a new folder for the project
mkdir now-php-example && cd $_
# Create now.json