DEV Community

Jason
Jason

Posted on • Updated on • Originally published at rametta.org

Pratica - Monadic Library, 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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
  })
Enter fullscreen mode Exit fullscreen mode

Try it out

Try it out in an online browser sandbox here!

or check it out on Github below!

GitHub logo rametta / pratica

πŸ₯ƒ Functional Algebraic Data Types

npm Pratica License PRs Welcome

πŸ₯ƒ Pratica

Functional Programming for Pragmatists

Why is this for pragmatists you say?

Pratica sacrifices some common FP guidelines in order to provide a simpler and more approachable API that can be used to accomplish your goals quickly - while maintaining data integrity and safety, through algrebraic data types.

For V1 docs - check out v1 docs readme

Install

With yarn

yarn add pratica
Enter fullscreen mode Exit fullscreen mode

or if you prefer npm

npm i pratica
Enter fullscreen mode Exit fullscreen mode

Documentation

Table of Contents

Changes from V1 to V2

If you are migrating from Pratica V1 to V2. Here is a small list of changes made:

  • Maybe() utility was renamed to nullable()
  • .default(() => 'value') was renamed to .alt('value') and does not…

Top comments (5)

Collapse
 
hongduc profile image
Hong duc

ohh interesting :)

Collapse
 
buinauskas profile image
Evaldas Buinauskas

This is gold.

Collapse
 
macsikora profile image
Pragmatic Maciej

Jason very nice. I am working currently on something similar. If I gave up, maybe I will do some contribution to your lib. But I feel the same need of something simple with fp powers. Thanks for that!

Collapse
 
rametta profile image
Jason

Awesome, any contributions to this library will be welcome!

Collapse
 
sdyalor profile image
sdyalor

😱️😱️