Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
@OliverJAsh
OliverJAsh / utils.js
Last active July 7, 2021 20:53
JS utils
const head = array => array[0];
const zip = (...arrays) => (
head(arrays).map((value, index) => arrays.map(otherArray => otherArray[index]))
);
const map = (collection, mapFn) => (
Object.keys(collection).map(key => mapFn(collection[key], key))
);
const reduce = (collection, reduceFn, seed) => (
Object.keys(collection).reduce((acc, key) => {
const value = collection[key];
@OliverJAsh
OliverJAsh / foo.md
Last active May 31, 2021 07:25
JavaScript function declarations vs. expressions
@OliverJAsh
OliverJAsh / foo.ts
Created December 10, 2017 21:34
Luxon TypeScript typings
declare module 'luxon' {
namespace luxon {
type DateTimeFormat = any;
type ZoneOptions = {
keepCalendarTime?: boolean;
};
type ToFormatOptions = {
round: boolean;
};
@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 / foo.md
Last active July 17, 2020 16:01
My frustrations writing demos/tests for redux-react connected components

My frustrations writing demos/tests for redux-react connected components

I'm writing demos for my React components, to showcase them in a specific states (e.g. Storybook).

However, the components I'm trying to write demos for are React Redux connected components, and this is making the demos much more difficult to achieve. To illustrate why, I'll start with an example.

Note: the problems I describe here also apply to writing tests (which demos are just one form of).

Example

@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]>) =>
import * as Reader from 'fp-ts/lib/Reader';
import { RouteData } from 'helpers/routes/types';
import { pipe, pipeWith } from 'pipe-ts';
// This will soon be part of fp-ts core
// https://github.com/gcanti/fp-ts/issues/904#issuecomment-619346296
const chainW: <Q, A, B>(
f: (a: A) => Reader.Reader<Q, B>,
) => <R>(ma: Reader.Reader<R, A>) => Reader.Reader<R & Q, B> = Reader.chain as Unrestricted;
import { from } from 'ix/iterable';
import { map } from 'ix/iterable/operators';
const compare = (a: string, b: string) =>
from(a).pipe(map((value, index) => value === b[index]));
for (const v of compare('yes', 'yas')) {
console.log(v);
}
// true
describe.only('createDataCron', () => {
it(
'init',
marbles(m => {
const source$ = m.cold('--a| ');
const sourceS = ' ^------';
const ms = m.time(' ------|');
const expected = ' --a| ';
const cron = createDataCron(source$, ms);
describe.only('createDataCron', () => {
it(
'init',
marbles(m => {
const source$ = m.cold('--a| ');
const sourceS = ' ^------';
const ms = m.time(' ------|');
const expected = ' --a| ';
const cron = createDataCron(source$, ms);