loading...

Pratica - Monadic Library, now fully in Typescript!

rametta profile image Jason ・2 min read

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!

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

or if you prefer npm

npm i pratica

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 require…

Posted on by:

rametta profile

Jason

@rametta

Software Developer in Montreal, Canada.

Discussion

markdown guide
 
 
 

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!

 

Awesome, any contributions to this library will be welcome!