Skip to content

Instantly share code, notes, and snippets.

View nielk's full-sized avatar
🦊

Alexandre Oger nielk

🦊
View GitHub Profile
const obj = {
'a': (a: string, b: number): string => `${a}: ${b}`,
'b': (b: number): string => `${b}`,
'c': (): number => 42,
}
const getFn = <K extends keyof typeof obj>(key: K, ...params: Parameters<typeof obj[K]>) => {
return (obj[key] as (...args: Parameters<typeof obj[K]>) => ReturnType<typeof obj[K]>)(...params)
}
const obj = {
'a': (a: string, b: number): string => `${a}: ${b}`,
'b': (b: number): string => `${b}`,
'c': (): string => 'c',
}
const getFn = <K extends keyof typeof obj>(key: K) => {
return obj[key]
}
@nielk
nielk / User.ts
Created April 11, 2023 04:31
Typesafe getContext setContext (typescript)
export type User = {
name: string,
shortName: string,
fullName: string,
role: string,
lineNames: ReadonlyArray<string>
}
import * as React from 'react'
import { Middleware } from 'redux'
// https://github.com/shiningjason/react-enhanced-reducer-hook
function compose(...fns: any): any {
if (fns.length === 0) {
return (arg: any) => arg
}
if (fns.length === 1) {
@nielk
nielk / get.ts
Last active January 24, 2018 10:01
safe-access-object.ts
function get<T,
K0 extends keyof T,
K1 extends keyof T[K0],
K2 extends keyof T[K0][K1],
K3 extends keyof T[K0][K1][K2],
K4 extends keyof T[K0][K1][K2][K3],
K5 extends keyof T[K0][K1][K2][K3][K4]>
(obj: T, k0: K0, k1?: K1, k2?: K2, k3?: K3, k4?: K4, k5?: K5) {
return Array.from(arguments).slice(1).reduce((prefix, val) => (prefix && prefix[val]) ? prefix[val] : null, obj)
}

Description

Related PRs

Quality checklist

Code

  • Tests has been added to cover my changes.
  • All new and existing tests passed (npm run test).
@nielk
nielk / gist:5241379423fd2a303d03
Created March 22, 2016 14:38
imagemagick compare images
compare -dissimilarity-threshold 1 -fuzz '10%' -metric RMSE 2.png 3.png lol.png
@nielk
nielk / package.json
Last active August 29, 2015 14:26
npm build workflow
{
"name": "lorem-ipsum",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npm run js:watch & npm run less:watch & npm run jade:watch",
"jade:watch": "jade -w ./src/*.jade -o ./dist",
"less:watch": "pleeease-watch",
"js:watch": "babel ./src/index.js --watch --out-file ./dist/main.js",
@nielk
nielk / concat.js
Created June 9, 2015 14:57
tweet sized concat string function
function t(s, d) {
for (var p in d)
s = s.replace(new RegExp('{' + p + '}', 'g'), d[p]);
return s;
}