Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@geraintluff
geraintluff / json-simplified.js
Created February 20, 2024 10:26
Simple RegEx for slightly prettier JSON-ish output (unquoted keys, single-item objects/arrays on one line)
JSON.simplified = v => {
return JSON.stringify(v, null, '\t')
.replace(/(\n\t*)"([a-z][a-z0-9_]*)"/ig, '$1$2')
.replace(/(\n[\sa-z0-9_:]+)\{\n\t*(.*)\n\t+\}/ig, '$1{$2}')
.replace(/(\n[\sa-z0-9_:]+)\[\n\t*(.*)\n\t+\]/ig, '$1[$2]');
};
JSON.parseSimplified = t => {
return JSON.parse(t
.replace(/(\n[\sa-z0-9_:]+)([\[\{])([^\n])/ig, '$1$2\n$3')
.replace(/(\n\t*)([a-z][a-z0-9_]*):/ig, '$1"$2":')
@geraintluff
geraintluff / data.json
Last active March 25, 2023 07:14
Additional Relative JSON Pointer examples
{
"test": ["foo", "bar"],
"child": {
"grandchild": 12345
},
"sibling": "sibling value",
"awkwardly/named~variable": true
}
@geraintluff
geraintluff / error-schema.json
Last active September 7, 2022 01:27
A small set of useful functions for writing JSON APIs in PHP.
{
"title": "Error (HTTP)",
"type": "object",
"properties": {
"statusCode": {"type": "integer"},
"statusText": {"type": "string"},
"message": {"type": "string"},
"data": {"title": "Supplementary data"}
},
"required": ["statusCode", "statusText", "message"]
@geraintluff
geraintluff / base64-web-safe.js
Created October 9, 2015 15:36
Convert base64 to and from web-safe variant
// Convert from normal to web-safe, strip trailing "="s
function webSafe64(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Convert from web-safe to normal, add trailing "="s
function normal64(base64) {
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4);
}
@geraintluff
geraintluff / unit-range-reciprocal.hpp
Last active June 6, 2022 10:56
A map from [0-1] to an arbitrary range, specified using low/mid/high values. Pulled out from some Signalsmith Audio internal code.
class UnitRangeMapReciprocal {
double vMin, vTopFactor, vBottomFactor;
public:
UnitRangeMapReciprocal() : UnitRangeMapReciprocal(0, 0.5, 1) {}
UnitRangeMapReciprocal(double min, double mid, double max) {
vMin = min;
double k = (mid - min)/(max - mid);
vTopFactor = max*k - min;
vBottomFactor = k - 1;
}
@geraintluff
geraintluff / csv-writer.h
Last active January 25, 2022 13:56
Example of outputting CSV from C++, and picking it up with Python (Matplotlib)
#ifndef UTIL_CSV_WRITER
#define UTIL_CSV_WRITER
/* Optional helper class for outputting CSV data. Use like:
CsvWriter csv("out"); // no .csv suffix
// write individual values
csv.write("foo", "bar");
csv.line();
@geraintluff
geraintluff / Pretty JSON.md
Last active October 19, 2021 21:35
My personal opinion on what nice JSON looks like:

My personal opinion on what nice JSON looks like:

  • Scalars as per normal
  • Empty objects/arrays as {} or []
  • Single-entry objects on a single line if value is single-line
  • Single-entry arrays are just wrapped in [...] (even if value is not single-line)
  • Arrays <= 5 items on a single line if all values are single-line
  • JSON.stringify() style indenting otherwise

Indenting customisable, but defaults to tabs - I code in proportional font, so four-space looks a bit narrow, and two-space just looks ridiculous.

{
"https://twitter.com/every_peano/status/590740112625008641": {
"exact": 0
},
"https://twitter.com/every_peano/status/1058469466714132480": {
"relative": "https://twitter.com/every_peano/status/592203098388566016",
"N": 30900
},
"https://twitter.com/every_peano/status/1057001290117595139": {
"relative": "https://twitter.com/every_peano/status/1058469466714132480",
@geraintluff
geraintluff / generate.py
Last active August 29, 2017 22:39
Generate JSON Schemas from schema.org types
import urllib.request as request
import re
import os
import copy
import json
outputprefix = "schemas/"
outputsuffix = '.json'
urlprefix = "http://json-schema.org/schemas/"
urlsuffix = '.json'
@geraintluff
geraintluff / ffmpeg-install.sh
Created March 30, 2016 10:55 — forked from clayton/ffmpeg-install.sh
Install FFMPEG on OS X with HomeBrew to convert Mp4 to WebM
# Installation
brew install ffmpeg --with-vpx --with-vorbis --with-libvorbis --with-vpx --with-vorbis --with-theora --with-libogg --with-libvorbis --with-gpl --with-version3 --with-nonfree --with-postproc --with-libaacplus --with-libass --with-libcelt --with-libfaac --with-libfdk-aac --with-libfreetype --with-libmp3lame --with-libopencore-amrnb --with-libopencore-amrwb --with-libopenjpeg --with-openssl --with-libopus --with-libschroedinger --with-libspeex --with-libtheora --with-libvo-aacenc --with-libvorbis --with-libvpx --with-libx264 --with-libxvid
# Easy Peasy
ffmpeg -i video.mp4 video.webm