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

adev-monads

v2.2.0

Published

A lightweight library for functional programming monads including Option, Maybe, and more.

Downloads

298

Readme

ADev Monads 🚀

ADev Monads is a lightweight functional utility library based on monads. It provides types and functions to work with structures such as Option, Maybe, Either, and others, making data flows safer and composable.


✨ Features

  • Lightweight and Modular: Designed for easy extension with new types.
  • 🛠️ TypeScript Support: Fully typed for greater development safety.
  • 🌟 Efficient: Optimized implementations with no external dependencies.
  • 🔦 Focus on Simplicity: Easy-to-use utilities.

📦 Installation

Use npm or yarn to install the package:

npm install adev-monads

Or with yarn:

yarn add adev-monads

🚀 Quick Start

🧹 Working with Option

The Option type allows you to handle values that may be present (some) or absent (none).

import { Option } from 'adev-monads';

const value = Option.some(42);

// Check if a value is present
if (value.isSome()) {
  console.log('Value present:', value);
} else {
  console.log('No value');
}

// Transform the value if present
const transformed = value.map((x) => x * 2);
console.log('Transformed:', transformed.getOrElse(0));

📚 Full API

Option

🛠️ Constructor

  • Option.some<T>(value: T): Option<T>: Creates an Option instance with a present value.
  • Option.none<T>(): Option<T>: Creates an Option instance without a value (absent).

🚨 Main Methods

  • isNone(): boolean: Returns true if the value is none.
  • isSome(): boolean: Returns true if the value is some.
  • map<U>(fn: (value: T) => U): Option<U>: Applies a function to the value if present.
  • flatMap<U>(fn: (value: T) => Option<U>): Option<U>: Similar to map, but avoids nested values.
  • getOrElse(defaultValue: T): T: Gets the value or returns a default value.
  • filter(predicate: (value: T) => boolean): Option<T>: Filters the value based on a predicate.
  • fold<U, V>(ifNone: () => U, fn: (value: T) => V): U | V: Handles both cases (some and none).

Full Example

const option = Option.some(10);
const result = option
  .filter((x) => x > 5)
  .map((x) => x * 2)
  .getOrElse(0);

console.log(result); // 20

📝 Writer Monad

The Writer monad represents a computation that produces a value along with a log. It's useful in scenarios where you need to track a sequence of messages or actions (e.g., logging, debugging) alongside the result of the computation.

Constructor

  • Writer.of<T, W>(value: T, log: W[] = []): Writer<T, W>: Creates an instance of Writer with a value and an optional log.
  • Writer.tell<W>(message: W): Writer<null, W>: Creates a Writer instance with a log message and no value.

Main Methods

  • map<U>(fn: (value: T) => U): Writer<U, W>: Transforms the value inside the Writer using the provided function.
  • flatMap<U>(fn: (value: T) => Writer<U, W>): Writer<U, W>: Similar to map, but the function returns a new Writer, combining logs.
  • fold<U>(onValue: (value: T) => U, onLog: (log: W[]) => void): U: Processes both the value and the log with the provided functions.
  • getValue(): T: Retrieves the value inside the Writer.
  • getLog(): W[]: Retrieves the log associated with the Writer.

Example

import { Writer } from 'adev-monads';

const writer = Writer.of(42, ['initial log']);
const newWriter = writer.map(x => x * 2);
const finalWriter = newWriter.flatMap(x => Writer.of(x + 5, ['calculation complete']));

console.log(finalWriter.getValue()); // 89
console.log(finalWriter.getLog()); // ['initial log', 'calculation complete']

🤝 Contributing

If you want to contribute to this project:

  1. 🍷 Fork the repository.
  2. 🌱 Create a branch for your feature: git checkout -b feature/new-feature.
  3. ✨ Make the necessary changes and ensure tests pass.
  4. 🔄 Submit a pull request.

🗒 License

This project is licensed under the MIT license.


👨‍💻 Author

Developed by Armando Dev.