Skip to content

Instantly share code, notes, and snippets.

View richardscarrott's full-sized avatar

Richard Scarrott richardscarrott

View GitHub Profile
@richardscarrott
richardscarrott / validators.js
Last active April 11, 2016 10:02
Validators
'use strict';
/**
* Validators are functions accepting `state` and `prop` where `state` is an object
* containing the entire form state, e.g. `{ email: 'email', password: 'password' }`
* and `prop` refers to the property on `state` to be validated.
*
* Validators are expected to return a falsy value if valid and a truthy value if
* invalid. This is to allow information re: the error to be returned. e.g.
*
@richardscarrott
richardscarrott / react-loadable
Created April 3, 2017 23:01
react-loadable flushModuleIds / flushServerSideRequires
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = Loadable;
@richardscarrott
richardscarrott / index.html
Last active June 29, 2017 19:36
iOS body scroll rubber banding bug
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style>
* {
@richardscarrott
richardscarrott / Button.jsx
Last active March 8, 2018 13:36
React Component Design
const Button = ({ tagName: TagName = 'button', ...rest }) => <TagName className="button" {...rest} />;
const App = () => {
return (
<>
<Button onClick={() => console.log('clicked')}>renders HTMLButtonElement (default)</Button>
<Button tagName="a" href="http://google.com">renders HTMLAnchorElement (for functional purposes)</Button>
<Button tagName="h3">renders HTMLHeadingElement (for weird SEO purposes)</Button>
</>
);
@richardscarrott
richardscarrott / index.js
Created December 12, 2019 18:59
js-string-memory-usage
// node --max-old-space-size=100 --expose-gc index.js
const prettyBytes = require('pretty-bytes');
const forceGC = () => {
if (global.gc) {
global.gc();
} else {
console.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
}
@richardscarrott
richardscarrott / closest.ts
Created January 24, 2020 12:23
closest([1, 2, 6, 0], 5) // 6
const closest = (values: number[], val: number) => {
if (!values.length) {
throw new VError(
{ info: { values, val } },
'Expected values to contain at least one value'
);
}
return values.reduce(
(acc, currVal) =>
Math.abs(acc.delta) > Math.abs(currVal - val)
@richardscarrott
richardscarrott / test.js
Created February 6, 2020 16:30
High precision time
const work = () => {
for (let i = 0; i < 1000000; i++) {}
};
const start = process.hrtime.bigint();
work();
const end = process.hrtime.bigint();
console.log(
`Benchmark took ${Number(end - start) / 1000 / 1000 / 1000} seconds`
);
@richardscarrott
richardscarrott / benchmark.js
Last active May 11, 2020 08:04
Memoize benchmark
// Originally from fast-memoize; updated to include `mem` and test multiple and non-primitive arguments.
const ora = require("ora");
const Table = require("cli-table2");
const debug = require("logdown")();
const lruMemoize = require("lru-memoize").default;
const iMemoized = require("iMemoized");
const Benchmark = require("benchmark");
const underscore = require("underscore").memoize;
const lodash = require("lodash").memoize;
@richardscarrott
richardscarrott / server.js
Created January 9, 2020 10:39
HTTP retry
const express = require('express');
console.log(process.version);
const app = express();
app.get('/hello', (req, res) => {
console.log('HANDLING REQUEST');
res.setHeader('content-type', 'application/json');
res.flushHeaders(); // `flushHeaders` to prevent http client from retrying and therefore invoking this handler multiple times.