Skip to content

Instantly share code, notes, and snippets.

View toboid's full-sized avatar

Toby toboid

  • London
  • 06:51 (UTC)
View GitHub Profile
@toboid
toboid / next_handlers.js
Last active August 29, 2015 14:01
Recursively run a set of handlers
var handlers = [];
handlers.push(function(next) {
console.log ('first handler');
next();
});
handlers.push(function(next) {
console.log ('second handler');
next();
@toboid
toboid / gist:c766cf9e43626f70fe99
Last active August 29, 2015 14:10
Error handing with standard A+ promises
var Promise = require('promise');
// Step 1 fails and rejects it's promise
function step1() {
return new Promise(function(resolve, reject) {
reject('Something went wrong');
});
}
// Step 2 doesn't do anything to handle error
@toboid
toboid / promisify-basic.js
Last active June 18, 2017 15:17
util.promisify basic
const {promisify} = require('util');
const fs = require('fs');
const readFile = promisify(fs.readFile);
readFile('./foo.txt').then(
fileContents => console.log(fileContents.toString()),
error => console.error(error)
);
@toboid
toboid / promisify-async.js
Created June 18, 2017 15:19
util.promisify usage with async
const {promisify} = require('util');
const fs = require('fs');
const readFile = promisify(fs.readFile);
// await can only be used within an async function
(async () => {
try {
const fileContents = await readFile('./foo.txt');
console.log(fileContents.toString());
} catch (ex) {
@toboid
toboid / promisify-multiple-args.js
Created June 18, 2017 15:20
util.promisify where callback takes multiple arguments
const {promisify} = require('util');
const child = require('child_process');
const exec = promisify(child.exec);
exec('echo "Hello world"').then(
result => console.log(result), // Prints { stdout: 'Hello world\n', stderr: '' }
error => console.error(error)
);
@toboid
toboid / nvm_global.sh
Created June 18, 2017 15:24
Use nvm to find which node versions a global module is installed for
nvm_global () {
PKG_NAME=$1
VERSIONS=()
I=0
for VER in $(nvm_ls)
do
nvm exec --silent ${VER} npm ls -g ${PKG_NAME} --depth=0 >/dev/null 2>&1
if [ $? -eq 0 ]; then
@toboid
toboid / sitemaps.test.js
Created June 18, 2017 15:26
Validating xml sitemaps in node.js
const assert = require('assert');
const fs = require('fs');
const libxmljs = require('libxmljs');
// Read the sitemap and schema from the file system
// Could just as easily get these over HTTP
const sitemap = fs.readFileSync('../sitemap.xml');
const schema = fs.readFileSync('./schemas/sitemap.xsd');
// Parse the sitemap and schema
@toboid
toboid / sitemap.xsd
Created June 18, 2017 15:28
Import Google news XSD to the main sitemap schema
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema ...>
<xsd:import
namespace="http://www.google.com/schemas/sitemap-news/0.9"
schemaLocation="./test/schemas/sitemap-news.xsd"/>
...
</xsd:schema>
@toboid
toboid / standard-deviation.js
Last active September 2, 2017 16:23
Calculating variance and standard deviation
const orderCounts = [3, 34, 15, 55, 6, 7];
const mean = sample => sample.reduce((sum, x) => sum + x, 0) / sample.length;
const square = x => x * x;
const difference = x => y => x - y;
const variance = sample => mean(sample.map(difference(mean(sample))).map(square));
// Variance
const σ2 = variance(orderCounts);
// Standard deviation
@toboid
toboid / pipe.js
Last active July 26, 2017 13:41
Pipe in JS
function pipe (...funcs) {
if (!funcs.length) {
throw new Error('pipe requires at least 1 argument');
}
return (...args) => funcs.slice(1).reduce((accum, func) => func(accum), funcs[0](...args))
}
// Usage
pipe((a, b) => a + b)(1, 2); // => 3