Skip to content

Instantly share code, notes, and snippets.

View YannickLeRoux's full-sized avatar
🏠
Working from home

Yannick YannickLeRoux

🏠
Working from home
View GitHub Profile
@YannickLeRoux
YannickLeRoux / wwr-conf.json
Last active May 3, 2022 02:33
WWR config file
[
{
"name": "Favorites",
"list": ["7fo5v_QH-MI", "7fo5v_QH-MI", "7fo5v_QH-MI", "gTRnWBZdOfY"]
},
{
"name": "Vinyasa",
"list": [
"7fo5v_QH-MI",
"7fo5v_QH-MI",
@YannickLeRoux
YannickLeRoux / typescriptreact.json
Last active May 17, 2021 20:41
VS Code User snippets for Typescript React
{
"React Functional Component": {
"prefix": "crfc",
"body": [
"import React from 'react';",
"",
"interface ${1:${TM_FILENAME_BASE}}Props {}",
"",
"const ${1:${TM_FILENAME_BASE}}: React.FC<${1:${TM_FILENAME_BASE}}Props> = (props) => {",
"",
@YannickLeRoux
YannickLeRoux / compose_map_reduce.js
Created February 15, 2021 23:26
Example Ramda - compose map and reduce
import * as R from 'ramda'
const foods = [{item: 'bread', calories: 500},{item: 'cheese', calories: 300},{item: 'ham', calories: 200}]
const getTotalCalories = R.compose(R.reduce(R.add,0),R.map(R.prop('calories')))
console.log(getTotalCalories(foods))
@YannickLeRoux
YannickLeRoux / ts_type_guards.ts
Created January 4, 2021 16:49
Typescript Runtime Typeguards
// Asserts data is an array of values that pass a certain check
// Once the assertion passes (not throwing), TS will infer the proper type
function assertIsTypedArray<T>(arg: any, check: (value: any) => value is T): asserts arg is T[] {
if (!Array.isArray(arg)) throw new Error(`This is not an array: ${JSON.stringify(arg)}`);
if (arg.some((el) => !check(el)))
throw new Error(`Some elements of the array are not the expected type: ${JSON.stringify(arg)}`);
}
// exemple of check function
@YannickLeRoux
YannickLeRoux / funcUtils.js
Created March 13, 2020 16:57
Useful utilities to manipulate functions
/**
* Composes a function that returns the result of invoking the given functions
* with the `this` binding of the created function, where each successive
* invocation is supplied the return value of the previous.
**/
export function pipe(...funcs) {
const length = funcs.length;
let index = length;
while (index--) {
@YannickLeRoux
YannickLeRoux / module.d.ts
Created January 29, 2020 22:37
simplest types declaration file for a 3rd party module
declare module 'module' {
const noTypesYet: any;
export default noTypesYet;
}
@YannickLeRoux
YannickLeRoux / deepCompareMemoize
Last active October 22, 2019 22:09
can be use with useEffect to make a deep comparison instead of a reference comparison
function useDeepCompareMemoize(value) {
const ref = React.useRef()
if (!deepEqual(value, ref.current)) {
ref.current = value
}
return ref.current
}
@YannickLeRoux
YannickLeRoux / usePrevious
Created May 6, 2019 18:16
usePrevious Hook
const usePrevious = value => {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
@YannickLeRoux
YannickLeRoux / remove_duplicate.js
Created April 11, 2019 18:33
Array - Remove objects with a similar 'label' property
function uniq(array) {
return array.filter(
(obj, index, self) => index === self.findIndex(el => el.label === obj.label)
);
}
@YannickLeRoux
YannickLeRoux / arrayEqual.js
Created April 8, 2019 19:01
Check the equality of 2 arrays or that both contain same elements
// compares 2 arrays
const arraysEqual = (a, b) => {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
// compare that both contains same elements no matter the order (optional)
const aSort = a.sort();
const bSort = b.sort();