Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
@OliverJAsh
OliverJAsh / README.md
Last active February 24, 2022 05:26
Flexbox gutters

Flexbox gutters

Problem

  • We use flexbox for our layout.
  • We want to add horizontal and vertical gutters inbetween these items.
  • Items wrap according to contents (flex-wrap: wrap), therefore we can't decide when/where to apply gutters using breakpoints.

Solution

@OliverJAsh
OliverJAsh / _git-merge-pr
Last active March 6, 2019 15:19
`git merge-pr`: CLI for merging GitHub PRs (via API) with completions
#compdef git-merge-pr
_git-merge-pr () {
# Note: requires hub and jq to be installed
# https://developer.github.com/v3/pulls/#list-pull-requests
# hub api /repos/{owner}/{repo}/pulls
PRS=$(
hub api /repos/{owner}/{repo}/pulls \
| jq 'map({title, number, head})'
@OliverJAsh
OliverJAsh / foo.tsx
Last active August 15, 2023 06:43
TypeScript React HOC using `forwardRef`
import * as React from 'react';
import { Component, ComponentClass, createRef, forwardRef, Ref } from 'react';
const myHoc = <ComposedComponentProps extends {}>(
ComposedComponent: ComponentClass<ComposedComponentProps>,
) => {
type ComposedComponentInstance = InstanceType<typeof ComposedComponent>;
type WrapperComponentProps = ComposedComponentProps & {
wrapperComponentProp: number;
@OliverJAsh
OliverJAsh / foo.ts
Created January 3, 2019 15:14
TypeScript object filter
const tuple = <T extends Array<unknown>>(...args: T): T => args;
// `Object.keys` does not return the keys as string literals, only strings. Use this helper as a
// workaround. https://github.com/Microsoft/TypeScript/pull/12253#issuecomment-263132208
const keys = <O extends object>(obj: O) => Object.keys(obj) as Array<keyof O>;
// `Object.entries` is ES2017, so we must define our own version.
const entries = <K extends string, V>(obj: Record<K, V>) =>
keys(obj).map(key => tuple(key, obj[key]));
const fromEntries = <K extends string, V>(arr: Array<[K, V]>) =>
@OliverJAsh
OliverJAsh / foo.ts
Created November 2, 2018 11:04
React Redux: infer Redux props from mappers
import React, { SFC } from 'react';
import { MapDispatchToPropsParam, MapStateToPropsParam } from 'react-redux';
import { StateRoot } from 'reducers/types';
import { AnyAction, Dispatch } from 'redux';
//
// Helpers
//
const createSubType = <T extends any>() => <SubType extends T>(subType: SubType) => subType;
@OliverJAsh
OliverJAsh / foo.ts
Last active September 16, 2018 14:26
TypeScript tagged union with generic
export type Node<P> = {
value: P;
children: Array<Tree<P>>;
};
type Leaf<P> = {
value: P;
};
enum TreeTag {
@OliverJAsh
OliverJAsh / README.md
Last active September 14, 2018 06:45
FP list combinators demo with fp-ts
tsc && node main.js
@OliverJAsh
OliverJAsh / form.ts
Created August 22, 2018 12:34
Simple react-redux using React's new Context API
const store = configureAndCreateReduxStore();
const { ReduxProvider, ReduxConsumer } = createReactRedux(store);
export const FormConnected: React.SFC<OwnProps> = ownProps => (
<ReduxConsumer>
{pipe(
({ state, dispatch }) => ({
...ownProps,
...mapStateToProps(state, ownProps),
@OliverJAsh
OliverJAsh / index.ts
Last active September 25, 2018 07:35
TypeScript higher-order function for applying defaults to function with named parameters
import { Diff } from 'typelevel-ts';
// Higher-order function
const withDefaultsFn = <DefaultParams extends object, Params extends DefaultParams, Result>(
fn: (params: Params) => Result,
defaultParams: DefaultParams,
) => (inputParams: Diff<Params, keyof DefaultParams>): Result =>
fn({
// We cast spreaded objects to any to workaround TS issue when spreading generic objects
// https://github.com/Microsoft/TypeScript/issues/14409
@OliverJAsh
OliverJAsh / foo.ts
Last active November 7, 2019 06:42
TypeScript URL helpers
import urlHelpers, { Url, UrlWithParsedQuery, UrlWithStringQuery } from 'url';
import queryStringHelpers from 'querystring';
import { Option } from 'funfix-core';
import pipe from 'lodash/flow';
import { ParsedUrlQuery } from 'querystring';
const isNonEmptyString = (str: string) => str.length > 0;
// https://github.com/gcanti/fp-ts/blob/15f4f701ed2ba6b0d306ba7b8ca9bede223b8155/src/function.ts#L127
const flipCurried = <A, B, C>(fn: (a: A) => (b: B) => C) => (b: B) => (a: A) =>