Skip to content

Instantly share code, notes, and snippets.

View isthatcentered's full-sized avatar

Edouard isthatcentered

  • Lille, remote
View GitHub Profile
@isthatcentered
isthatcentered / opaque.ts
Created March 14, 2019 11:59
Typescript - Opaque type
export type Opaque<T, K> = K & { __TYPE__: T };
// Create a new opaque type
type WaffleId = Opaque<number, "waffledId">;
// This function now handles the knowledge of what a waffleId is, change will only happen there if needed
function makeWaffleId( number: number ): WaffleId
{
// ...do stuff
@isthatcentered
isthatcentered / cloneChildAndAddProps.ts
Last active March 14, 2019 11:00
React - Cloning children with new props / passing props to children
export interface PassThroughProps
{
children: ReactElement
}
export function PassThrough( props: PassThroughProps )
{
const { children, ...propsToPass } = props
@isthatcentered
isthatcentered / breakfastReducer.ts
Last active April 11, 2019 06:44
Typescript / React Redux - Ensure all actions handled in switch case
type exampleReducerAction = Waffled | Pancaked | NOT_HANDLED_IN_SWITCH_CASE
function breakfastReducer( state: any, action: exampleReducerAction ): any
{
// Note, for some reason action.type doesn't work
switch ( action.type ) {
case "PancakedAction":
return "Gimme!"
@isthatcentered
isthatcentered / tsconfig.json
Last active June 7, 2019 20:00
Typescript declaration merging / type augmentations
{
"//1//":"This doesn't seem to be needed",
"compilerOptions": {
// ... your setup
"typeRoots": ["./custom.d.ts"] // Aka add these types on top of the other ones
}
}
@isthatcentered
isthatcentered / mocking.spec.tsx
Last active September 14, 2018 08:50
Mocking react 16 context in jest (typescript version)
import * as React from "react"
import { mount } from "enzyme"
import StoreProvider from "./StoreProvider"
import Mock = jest.Mock
jest.mock( "./StoreProvider", () => ({
Timer*
Idle
start->Running
Running
stop->Idle
pause->Paused
tick->done?
Paused
start->Running
stop->Idle
Traffic Light
Regular
power outage->Power failure
Green*
tick->Yellow
Yellow
tick->Red
Red
tick->Green
Timer*
Idle*
start->Running
Running
reset->Idle
tick-> SpecifiedTimeElapsed?
# Transient state define system’s logic without dropping into code
SpecifiedTimeElapsed?
@isthatcentered
isthatcentered / gulpfile.js
Last active January 26, 2018 17:23
gulp sass browser-sync
/**
* NPM dependencies to install
* (npm i -D gulp gulp-sass ... )
*/
const gulp = require( "gulp" ),
path = require( "path" ),
sass = require( "gulp-sass" ),
sourcemaps = require( "gulp-sourcemaps" ),
autoprefixer = require( "gulp-autoprefixer" ),
browserSync = require( "browser-sync" ).create() // Local server
@isthatcentered
isthatcentered / debounce.js
Created November 18, 2017 15:44
ES6 debounce function
/**
* debounce
* Ensures a function won't be called before a defined amout of time
* Ex:
* on window resize, ensure a function won't be called
* until the user stopped resizing window for {time param}
*
* @param { function } fn Callback to be executed after debounce
* @param { int } time Time to wait before function execution
* @return {function(...[*])}