Skip to content

Instantly share code, notes, and snippets.

View karol-majewski's full-sized avatar

Karol Majewski karol-majewski

View GitHub Profile
@karol-majewski
karol-majewski / create-reducer.ts
Last active May 5, 2018 00:38
Equivalent for `handleActions` from the redux-actions` library.
import { AnyAction, Reducer } from 'redux';
/**
* Equivalent for `handleActions` from the redux-actions` library.
* @see https://redux.js.org/recipes/reducing-boilerplate#generating-reducers
*
* @template T Reducer shape.
*/
export function createReducer<T>(handlers: Record<string, Reducer<T>>, initialState: T): Reducer<T> {
return (state: T = initialState, action: AnyAction): T => {
/**
* TypeScript version of @istarkov's cancellable Promise wrapper.
*
* @see https://github.com/facebook/react/issues/5465#issuecomment-157888325
*/
const makeCancelable = <T>(promise: Promise<T>): { promise: Promise<T>; cancel(): void } => {
let hasCanceled = false;
const wrappedPromise = new Promise<T>((resolve, reject) => {
promise.then(
@karol-majewski
karol-majewski / flatMap.ts
Last active February 2, 2023 13:48
The simplest flatMap implementation in TypeScript
export function flatMap<T, U>(array: T[], callbackfn: (value: T, index: number, array: T[]) => U[]): U[] {
return Array.prototype.concat(...array.map(callbackfn));
}
@karol-majewski
karol-majewski / redux-performance-mark.ts
Last active July 6, 2018 10:51 — forked from clarkbw/redux-performance-mark.js
A User Timing middleware for Redux to create performance markers for dispatched actions. Remade with TypeScript (Redux v.4).
import { AnyAction, Middleware } from 'redux';
export const userTimingMiddleware: Middleware = () => {
return next => {
return <A extends AnyAction>(action: A): A => {
if (performance.mark === undefined) {
return next(action);
}
performance.mark(`${action.type}_start`);

Applying default values

If our dependencies look like this:

declare class Repository<T> {
  find<T>(options: FindOptions): Promise<T>;
}

interface FindOptions {
enum PokemonType {
Water,
Rock,
Fire,
Air,
Electric,
Poison
}
class Pokemon {
@karol-majewski
karol-majewski / ConditionalWrapper.tsx
Last active November 5, 2018 17:25
Wrapping React Elements conditionally
/**
* TypeScript version of the solution proposed by @kitze.
*
* @see https://gist.github.com/kitze/23d82bb9eb0baabfd03a6a720b1d637f
*/
import React from 'react';
interface Props<T = {}> {
condition: boolean;
render: (children: React.ReactElement<T> | null) => React.ReactElement<T> | null;
@karol-majewski
karol-majewski / find-and-highlight.ts
Last active January 6, 2019 14:41
Finds & highlights given phrases inside a Web document
type IdentityFunction<T> = (argument: T) => T;
type InnerHTMLTransformer = IdentityFunction<string>;
type RegularExpressionFlags =
| 'i'
| 'g'
| 'ig';
type Phrase = string | RegExp;
@karol-majewski
karol-majewski / gist:db48d658cbda8cf6613a0525af57d682
Created November 11, 2018 03:22
Chrome DevTools blackbox patterns
/node_modules/
webpack:///webpack
^webpack://.*/process\-update\.js$
^webpack://.*/client\.js$
^debugger://
\(webpack\)-hot-middleware
webpack/bootstrap
/jquery\.min\.js$
/jquery\-migrate\-1\.4\.1\.min\.js$
{
"compilerOptions": {
/* Basic Options */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [ /* Specify library files to be included in the compilation. */
"dom",
"esnext"
],
// "allowJs": true, /* Allow javascript files to be compiled. */