Skip to content

Instantly share code, notes, and snippets.

View richardscarrott's full-sized avatar

Richard Scarrott richardscarrott

View GitHub Profile
@xialvjun
xialvjun / setIn.js
Last active February 23, 2021 18:23
immutable version of lodash/set
// https://github.com/lodash/lodash/issues/1696
import {clone, setWith, curry} from 'lodash/fp';
// export const setIn = curry((path, value, obj) =>
// setWith(clone, path, value, clone(obj)),
// );
export const setIn = curry((obj, path, value) =>
setWith(clone, path, value, clone(obj)),
);
@etagwerker
etagwerker / curl_to_ab.py
Last active April 11, 2020 11:15 — forked from ctolsen/curl_to_ab.py
"Copy to cURL" in Chrome to Apache Bench command -- Script for Python3
#!/usr/bin/env python3
import sys
import os
def curl_to_ab(curl_cmd: list, num: int=200, cur: int=4) -> str:
"""
Translate a cURL command created by Chrome's developer tools into a
command for ``ab``, the ApacheBench HTTP benchmarking tool.
type BatchLoadFn<K, V> = (keys: Array<K>) => Promise<Array<V | Error>>;
type LoadAllFn<K, V> = () => Promise<Array<[K, V]>>;
type Options<K, V> = {
batch?: boolean,
cache?: boolean,
cacheMap?: CacheMap<K, Promise<V>>,
cacheKeyFn?: (key: any) => any
};
@jondlm
jondlm / README.md
Last active January 3, 2018 05:59
React shallow render lifecycle breakdown

React introduced shallow rendering in 0.13. This is an excellent feature that I wish was included earlier in React. It aims to solve the problem of unit testing components without going through a real, or jsdom mocked, DOM. I couldn't find any info online about what lifecycle events it actually fires. So I did some testing of my own. To reproduce, put component.js and test.js into a folder and run node test.js.

TLDR; shallow rendering only invokes the following lifecycle hooks (in order):

  1. getDefaultProps
  2. getInitialState
  3. componentWillMount stops here until re-render
  4. componentWillReceiveProps
  5. shouldComponentUpdate
  6. componentWillUpdate
@ChrisPenner
ChrisPenner / heyting-validation.js
Created August 23, 2017 14:32
Heyting Algebra Validation
const all = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a && b, true)
const any = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a || b, false)
const oneOf = (...preds) => (obj) => preds.map(f => f(obj)).reduce((a, b) => a ? !b : b, false)
const has = (prop) => (obj) => obj[prop] !== undefined
const not = (pred) => (obj) => !pred(obj)
const equals = (prop, val) => (obj) => obj[prop] === val
const implies = (f, g) => (obj) => !f(obj) || g(obj);
const validate = all(implies(has('selectedIndex'), equals('isOpen', true)))
@lightsofapollo
lightsofapollo / gist:1577413
Created January 8, 2012 05:59
Jasmine reporter to find slow running tests
SlowDescribeReporter.prototype = {
threshold: 400,
reportRunnerStarting: function(runner){
var i, len, suites = runner.suites_, self = this;
this.topLevels = {};
for(i = 0, len = suites.length; i < len; i++){
(function(exec){
@michalkot
michalkot / weightedRandom.js
Last active November 26, 2015 13:55
weightedRandom
function weightedRandom(ar) {
var ar = ar.sort(x => x.w);
var sum = ar.reduce((p, c) => p + c.w, 0);
// normalise
var soFar = 0;
ar.map(o => {
o.w = o.w / sum;
o.w += soFar;
soFar += o.w;
});