Skip to content

Instantly share code, notes, and snippets.

View moatorres's full-sized avatar
👋

Moa Torres moatorres

👋
  • 18:45 (UTC -03:00)
View GitHub Profile
@moatorres
moatorres / .bash_profile.sh
Last active June 27, 2021 13:58
Bash Profile
# @hoxlux/bash
# 🗂 💡 🔑 🔐 🚨 🐙 🌴 🔥 🌈 🥥 🥚 ⏱ 🧰 🧪 🛍 🇬🇧 🇧🇷
alias ga="git add ."
alias gc1="git commit -m 'commit inicial'"
alias gmaster="git switch master"
alias gnext="git switch next"
alias gprod="git switch production"
function parse_git_branch {
@moatorres
moatorres / Maybe.d.ts
Last active June 27, 2021 13:49
Maybe monad in JS/Node (without classes)
type NotNullOrUndefined = string | number | boolean | symbol | object
interface IMaybe<T> {
type: symbol
has(): boolean
isJust(): boolean
isNothing(): boolean
unwrap(): T | never
map<U>(fn: (value: T) => U): IMaybe<U>
flatMap<U>(fn: (value: T) => U): T
@moatorres
moatorres / Maybe.d.ts
Last active June 27, 2021 13:51
Maybe monad in JS/Node (with ES6 classes)
type CallbackFunction<T = unknown> = (...params: unknown[]) => T
export class Maybe<T> {
private constructor(private value: T | null) {}
static just<T>(a: any): Maybe<T>
static nothing<T>(): Maybe<T>
static fromValue<T>(value: T | undefined | null): Maybe<T>
isJust(): boolean
isNothing(): boolean
toString(): string
@moatorres
moatorres / List.js
Last active June 27, 2021 13:51
Lazy-list in JS/Node (with ES6 classes)
// a Haskell-like lazy-list
// adapted from this awesome gist: https://gist.github.com/gvergnaud/6e9de8e06ef65e65f18dbd05523c7ca9
export class List {
constructor(generator, length) {
this[Symbol.iterator] = generator
this.length = length
}
// should be private
@moatorres
moatorres / Identity.js
Last active June 27, 2021 13:50
Identity type in JS/Node (without classes)
export const Identity = (x) => ({
chain: (f) => f(x),
unwrap: () => x,
map: (f) => Identity(f(x)),
inspect: () => `Identity(${x})`,
})
@moatorres
moatorres / Tuple.d.ts
Last active June 27, 2021 13:50
Tuple type in JS/Node (without classes)
interface ITuple<F, S> {
fst(): F
snd(): S
toArray(): [F, S]
unwrap(): [F, S]
toJSON(): [F, S]
inspect(): string
toString(): string
equals(other: ITuple<F, S>): boolean
bimap<F2, S2>(f: (fst: F) => F2, g: (snd: S) => S2): ITuple<F2, S2>
@moatorres
moatorres / List.js
Last active June 27, 2021 13:50
List type in JS/Node (without classes)
export const List = (x) => ({
emit: () => x,
head: () => x[0],
tail: () => List.of(x.splice(1)),
last: () => x[x.length - 1],
chain: (f) => x.map(f),
map: (f) => List.of(x.map(f)),
inspect: () => `List(${x})`,
concat: (a) => List.of(x.concat(a))
})
@moatorres
moatorres / Result.js
Last active June 27, 2021 13:50
Result monad in JS/Node (without classes)
const makeReadOnly = (value) => Object.freeze(value)
const ResultType = {
Ok: Symbol(':ok'),
Err: Symbol(':err'),
}
const Ok = (val) => makeReadOnly({
type: ResultType.Ok,
isOk: () => true,
@moatorres
moatorres / Option.js
Created June 27, 2021 14:22
Option monad in JS/Node (without classes)
import { isValid } from './utils'
const OptionType = {
Some: Symbol(':some'),
None: Symbol(':none'),
}
const makeSome = (val) => ({
type: OptionType.Some,
isSome: () => true,
@moatorres
moatorres / styled-components.spec.tsx
Created November 5, 2021 20:23
Get styles applied by `styled-components` with `jest` and `react-test-renderer`
import { getStyles } from './test-helpers'
describe('styled-components', () => {
test('extended components keep their styles', () => {
const Box = styled.div`
margin: 16px;
`
const Card = styled(Box)`
color: tomato;
`