Skip to content

Instantly share code, notes, and snippets.

View toboid's full-sized avatar

Toby toboid

  • London
  • 23:31 (UTC)
View GitHub Profile
@toboid
toboid / for-of-with-index.js
Created August 21, 2023 12:27
Index in a JS for...of loop
const arr = [1, 8, 3, 9];
for (const [i, element] of arr.entries()) {
// i is the index
// element is the array element
}
@toboid
toboid / react-test-utils-simulate.js
Created December 26, 2017 21:53
Using ReactTestUtils.renderIntoDocument and ReactTestUtils.Simulate
import React from "react";
import ReactTestUtils from "react-dom/test-utils";
const PageLink = ({ page, onClick }) => (
<a href={`?page=${page}`} onClick={onClick}>
Page {page}
</a>
);
// ReactTestUtils.renderIntoDocument doesn't work with functional components
@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
@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 / 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 / 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 / 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 / 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 / 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-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)
);