Skip to content

Instantly share code, notes, and snippets.

View tho-graf's full-sized avatar

Thomas Graf tho-graf

View GitHub Profile
function* range(fromInclusive, toExclusive, step = 1) {
if (toExclusive == undefined) {
toExclusive = fromInclusive;
fromInclusive = 0;
}
for (let i = fromInclusive; (toExclusive - i) * step > 0; i += step) {
yield i;
}
}
@tho-graf
tho-graf / gist:6224f1a2747bca17682f304c127ba2f3
Created June 7, 2019 18:46
Optional Properties in Object (ES6)
const shouldAddB = true;
let obj = {a:1, ...(shouldAddB && {b:2})};
import * as React from "react";
type Value = boolean;
type ContextWithSetter = React.Context<{
value: Value;
setValue: (value: Value) => void;
}>;
const { Provider, Consumer }: ContextWithSetter = React.createContext({
getPackageNames(input) {
// you don't need the global array and mutate it later. The map method will create a new array for you ;)
if (!input) {
return Promise.resolve({ options: [] });
}
return fetch(`https://api.npms.io/v2/search?q=${input}`)
.then(response => response.json())
// the map method will
// 1. create a new array
@tho-graf
tho-graf / gist:95a0204b3736ced4f50b2d0050b6b817
Last active February 3, 2018 15:57
Setup Function for Enzyme Tests
function setup(propOverrides = {}) {
const defaultProps = {
onChange: jest.fn(),
renderBlockContent: jest.fn(),
value: [{ content: 'Test 1' }, { content: 'Test 2' }]
};
const mergedProps = {
...defaultProps,
...propOverrides