Jason Rametta Blog

Software Developer from Montreal

Pratica, now fully in Typescript!

Pratica is now written completely in Typescript!

What is Pratica?

Pratica is a super tiny 720B monadic library, comparable to Crocks or Monet JS.

Why would I use Pratica?

If you want to start writing more functional code in Javascript or Typescript, this is a great library for learning some FP fundamentals, while also making your code safer and more resilient to runtime bugs. It's super tiny size and easy to read dot-chaining syntax makes it easy to get started in any project.

How do I start?

You can install it with: yarn add pratica or npm i pratica. Then you can import the main functions like:

import { nullable } from 'pratica'

Create small, safe and easy to read programs by composing functions together, like:

// Typescript

import { Maybe, nullable, get, parseDate } from 'pratica'

const getPersonAge = (person?: Person): Maybe<number> =>
  nullable(person)
    .chain(get<string>(['birthday']))
    .chain(parseDate)
    .map(birthday => Date.now() - birthday.getTime())
    .chain(parseDate)
    .map(date => Math.abs(date.getUTCFullYear() - 1970))


getPersonAge({ birthday: '1994-06-08' }) // -> Just(25)
getPersonAge({ birthday: 771033600000  }) // -> Just(25)
getPersonAge({ birthday: null }) // -> Nothing
getPersonAge(null) // -> Nothing

Pratica works great with React too! Use it in your JSX for handling cases with missing data.

const viewPersonAge = ({ person }) =>
  getPersonAge(person).cata({
    Just: age => <div>{age}</div>
    Nothing: () => <span>No age available</span>
  })

Try it out

Try it out in an online browser sandbox here!