Skip to content

Instantly share code, notes, and snippets.

View Magellol's full-sized avatar

Thomas Lefebvre Magellol

View GitHub Profile
@Magellol
Magellol / sum-io-ts-codecs.ts
Created February 7, 2024 22:38
Derive sum types from codecs
import * as Sum from 'shared/facades/Sum';
import * as t from 'shared/facades/io-ts';
const TypeA = t.strict({
a: t.string,
});
interface TypeA extends t.TypeOf<typeof TypeA> {}
const TypeB = t.strict({
@Magellol
Magellol / lenses.ts
Created February 26, 2020 21:39
Quick and dirty approach to lenses
type Lens<O, V> = {
get: (o: O) => V
set: (value: V) => (o: O) => O,
update: (fn: (current: V) => V) => (o: O) => O
}
type Homework = {done: number};
type Person = {
age: number;
@Magellol
Magellol / ts-custom-intrinsic-elements.tsx
Created December 9, 2018 18:28
Override react types to exclusively allow a set of custom intrinsic elements without inheriting from HTML.
// By overriding a subset of the interfaces from the `@types/react` package,
// we're able to strictly type check what our JSX must look like and avoid inheriting from HTML elements (since they're not valid in our environment for instance).
import * as React from 'react';
declare module 'react' {
namespace JSX {
interface ElementChildrenAttribute {
children: {};
}
@Magellol
Magellol / conditionallyRenderingRamda.tsx
Last active August 14, 2018 18:27
Conditionally rendering with ramda
const ifNotReady = ifElse(
either(
propEq('status', 'init'),
propEq('status', 'pending'),
),
);
render() {
<>
{ifNotReady(
@Magellol
Magellol / react-optional-function-as-children.jsx
Last active September 7, 2017 18:52
Give the developer the ability to have more flexibility on the layout if a function is passed as children.
const propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
};
const defaultProps = {
children: null,
};
const SaveButton = () => ...
const ActionsContainer = ({ children }) => ...
@Magellol
Magellol / babel-loader-worker-loader.js
Last active July 15, 2017 20:41
Example of a webpack configuration to get workers code transpiled. Hint: "Rule.enforce".
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
// Babel loader options
},
},
],
@Magellol
Magellol / renderComponentAfterTimeout.js
Created April 11, 2017 21:26
Higher-Order component rendering a component after a timeout.
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import Loading from 'loading';
export default function TimedOut(WrappedComponent) {
const propTypes = {
delay: PropTypes.number.isRequired,
};
class Wrapper extends Component {
@Magellol
Magellol / removeIndexFromArray.js
Created March 27, 2017 16:56
Removing an index from an array.
/**
* Remove an index from an array.
* This creates a new array without the index we're removing.
* Because of that, indexes are re-created so there isn't any empty spot.
*
*/
export function removeIndexFrom(array, indexToRemove) {
return [
...array.slice(0, indexToRemove),
...array.slice(indexToRemove + 1),
@Magellol
Magellol / promisify.js
Created February 27, 2017 18:35
Promisify error-first callback based functions.
function promisify(fn) {
return (...args) => {
return new Promise((resolve, reject) => {
fn(...args, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
@Magellol
Magellol / createShallowCopy.js
Last active December 14, 2016 16:58
Array.concat() or the spread syntax creates shallow copies.
const original = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
const clone = [...original];
console.log(original === clone); // false.
console.log(original[0] === clone[0]); // true.