npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ortense/functors

v0.2.0

Published

A collection of dependency free functors written in typescript

Downloads

49

Readme

Functors banner - the functors mascot generated by dall-e 2

@ortense/functors

install size Coverage Status

A collection of dependency-free functors written in TypeScript, created to be type-safe, immutable, and lightweight.

What is a Functor?

In functional programming, a functor is a concept that represents a type with the ability to be mapped over. This means that you can apply a function to the values inside the type, without altering the structure of the type itself. Functors provide a powerful abstraction for building composable and reusable code, promoting a declarative and functional style of programming.

functor chart - made in excalidraw.com

Overview

This project is a TypeScript library that embraces the functor concept, offering implementations of common functors. These functors provide useful abstractions for various scenarios in your application, allowing you to write more expressive and maintainable code.

Install

Pick your favorite package manager.

npm install @ortense/functors  # npm
yarn add  @ortense/functors    # yarn
pnpm add @ortense/functors     # pnpm
bun add @ortense/functors      # bun

Implemented Functors

For detailed documentation, please refer to the official documentation.

Lazy

Represents a lazy-evaluated value, allowing computations to be deferred until needed.

import { lazy } from '@ortense/functors'

const double = (value: number) => {
  console.log('Doubling...')
  return value * 2
}
const lazyDouble = lazy(() => 7)
  .map(double)
  .map(double)

const result = lazyDouble.evaluate()
console.log(result)
// Output:
// Doubling...
// Doubling...
// 42

History

Represents a history of values, enabling operations like mapping, resetting, and rolling back to previous states.

import { history } from '@ortense/functors'

const userHistory = history({ name: 'Jane Doe' })
  .map(user => ({ ...user, id: 1 }))
  .map(({ id }) => ({ id, name: 'Jonh Due' }))

console.log(userHistory.current()) // { id: 1, name: 'Jonh Due' }
console.log(userHistory.rollback().current()) // { id: 1, name: 'Jane Doe' }
console.log(userHistory.reset().current()) // {  name: 'Jane Doe' }

Either

Represents a type that can be either of type L or R, providing a mechanism for branching based on success or failure.

import { Either, left, right } from '@ortense/functors'

const divide = (
  numerator: number,
  denominator: number,
): Either<Error, number> => {
  if (Number.isNaN(numerator)) {
    return left(new Error('Numerator is not a number.'))
  }
  if (Number.isNaN(denominator)) {
    return left(new Error('Denominator is not a number.'))
  }

  if (denominator === 0) {
    return left(new Error('Division by zero is not posible.'))
  }

  return right(numerator / denominator)
}

const numerator = Number(document.querySelector('input#numerator').value)
const denominator = Number(document.querySelector('input#denominator').value)
const display = document.querySelector('div#display-result')

divide(numerator, denominator)
  .right(result => {
    display.textContent = `${numerator} / ${denominator} = ${result}`
  })
  .left(error => {
    display.textContent = error.message
  })

Maybe

Represents a type that may contain a value or be empty, helping to handle null or undefined values.

import { Maybe, maybe } from '@ortense/functors'

type User = { id: string; name: string }

const findUserById = async (id: string): Maybe<User> => {
  const user = await db.users.findByID(id) // suppose that return user or null
  return maybe<User>(user)
};

const userName = findUserById('123')
  .map(user => user.name) // Maybe<string>
  .mapEmpty(() => 'Default Name')
  .unwrap()

console.log(userName) // Output: Default Name (if user not found)

Contribution

Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback to help improve this library.

License

This library is licensed under the MIT License - see the LICENSE file for details.