Skip to content

Instantly share code, notes, and snippets.

View Sangrene's full-sized avatar

Hugo Laplace-Builhe Sangrene

View GitHub Profile
const handleCommand = async ({
channel,
from,
func,
to,
}: {
from: string;
to: string;
func: (args: any) => any;
channel: amqp.Channel;
@Sangrene
Sangrene / getKeys.ts
Created March 29, 2021 22:40
Typed Object.keys(...)
export const getKeys = <T extends {}>(o: T): Array<keyof T> =>
o ? (Object.keys(o) as Array<keyof T>) : [];
@Sangrene
Sangrene / Timed_memoize.ts
Created June 23, 2020 13:41
To cache a function result based on time and arguments
type AnyFunction = (...args: any[]) => any;
type GetReturnType<T extends AnyFunction> = T extends (...args: any[]) => infer R ? R : any;
export const timeCacheResult = <T extends AnyFunction>(
cache: Map<any, { until: number; value: any }>,
cacheTime: number,
func: T,
): T => {
return function(...args: any[]) {
const curTime = new Date().getTime();
@Sangrene
Sangrene / findItemAndIndexById.ts
Created December 10, 2019 16:32
Typescript function to extract item & index from an array with items containing an "id" property
const findItemAndIndexById = <T extends { id: string }>(
id: string,
tabWithId: T[]
):
| {
item: T;
index: number;
}
| undefined => {
let itemIndex = -1;
import React from 'react';
interface Props {
if: boolean;
}
const Conditionnal: React.FC<Props> = props => {
if (props.if) {
return <React.Fragment>{props.children}</React.Fragment>;
} else {
return null;
const bills = createStore([{id: 0, state: 0}, {id: 1, state: 2}]);
const selected = createStore(0);
const cheatersStore = createStoreObject({
bills,
selected
})
const validate = createEvent('validate')
bills.on(validate, (bills, idBill) => {
interface ShopModel {
date: string;
}
class Shop extends ShopModel {
constructor(shop: ShopModel){
this.date = shop.date;
}
get formattedDate() {
@Sangrene
Sangrene / logAction.ts
Created March 21, 2019 15:25
Decorator to log async method
export const logAction = (name: string) => {
return (target, key, descriptor) => {
if(descriptor === undefined){
descriptor = Object.getOwnPropertyDescriptor(target, key);
}
const originalMethod: Function = descriptor.value;
descriptor.value = async function() {
const args: Array<any> = [];
import React from "react";
import { View, TouchableOpacity } from "react-native";
class MyChildComponent extends React.Component {
render(){
}
}
@Sangrene
Sangrene / .gitignore
Created December 17, 2018 14:04
Gitignore for NodeJS AWS ELB using /dist as build folder
node_modules
.env
.ebextensions
error.log
dist