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

@sweet-monads/identity

v3.3.1

Published

[Identity Monad](https://blog.ploeh.dk/2022/05/16/the-identity-monad/), The Identity monad is a monad that does not embody any computational strategy. It simply applies the bound function to its input without any modification.

Downloads

7

Readme

@sweet-monads/identity

Identity Monad, The Identity monad is a monad that does not embody any computational strategy. It simply applies the bound function to its input without any modification.

This library belongs to sweet-monads project

sweet-monads — easy-to-use monads implementation with static types definition and separated packages.

Usage

npm install @sweet-monads/identity

// Before
app.use(
  express.static(
    path.resolve(getDirname(import.meta.url), "../public")
  )
)


// After
Identity.from(import.meta.url)
    .map(getDirname)
    .map(dir => path.resolve(dir, "../public"))
    .map(express.static)
    .map(app.use);

API

chain

function chain<A, B>(fn: (v: A) => Promise<Identity<B>>): (m: Identity<A>) => Promise<Identity<B>>;
  • fn: (v: A) => Promise<Identity<B>> - function which should be applied asynchronously to Identity<A> value
  • Returns function with Identity<A> argument and mapped by fn value (could be used inside Promise#then function).

Example:

const getValue = async () => from(1);

// Identity<number>
const result = await getValue()
  .then(Identity.chain(async v => from(v * 2)))
  .then(Identity.chain(async () => from(null)));

from

function from<T>(value: T): Identity<T>;
  • Returns Identity which contains value with T type. Example:
const v1 = from(2); // Identity<number>
const v2 = from<2>(2); // Identity<2>

isIdentity

function isIdentity<T>(value: unknown | Identity<T>): value is Identity<T>;
  • Returns boolean if given value is instance of Identity constructor. Example:
const value: unknown = 2;
if (isIdentity(value)) {
  // ... value is Identity<unknown> at this block
}

Identity#join

function join<V>(this: Identity<Identity<V>>): Identity<V>;
  • this: Identity<Identity<V>> - Identity instance which contains another Identity instance.
  • Returns unwrapped (inner) Identity. Example:
const v = from(from(2));

v1.join(); // Identity with value 2

Identity#map

function map<Val, NewVal>(fn: (val: Val) => NewVal): Identity<NewVal>;
  • Returns mapped by fn function value wrapped with Identity. Example:
const v = just(2);

const newVal = v.map(a => a.toString()); // Identity<string> with value "2"
Identity#asyncMap
function asyncMap<Val, NewVal>(fn: (val: Val) => Promise<NewVal>): Promise<Identity<NewVal>>;
  • Returns Promise with mapped by fn function value wrapped with Identity Example:
const v = from(2);

// Promise<Identity<string>> with value "2"
const newVal = v.asyncMap(a => Promise.resolve(a.toString()));
Identity#apply
function apply<A, B>(this: Identity<(a: A) => B>, arg: Identity<A>): Identity<B>;
function apply<A, B>(this: Identity<A>, fn: Identity<(a: A) => B>): Identity<B>;
  • this | fn - function wrapped by Identity, which should be applied to value arg
  • arg | this - value which should be applied to fn
  • Returns mapped by fn function value wrapped by Identity. Example:
const v = from(2);
const fn = from((a: number) => a * 2);

const newVal1 = fn.apply(v); // Identity<number> with value 4
const newVal2 = v.apply(fn); // Identity<number> with value 4
Identity#asyncApply

Async variant of Identity#apply

function asyncApply<A, B>(
  this: Identity<(a: Promise<A> | A) => Promise<B>>,
  arg: Identity<Promise<A> | A>
): Promise<Identity<B>>;
function asyncApply<A, B>(this: Identity<Promise<A> | A>, fn: Identity<(a: Promise<A> | A) => Promise<B>>): Promise<Identity<B>>;
  • this | fn - function wrapped by Identity, which should be applied to value arg
  • arg | this - value which should be applied to fn
  • Returns Promise with mapped by fn function value wrapped by Identity. Example:
const v = from(2);
const fn = from((a: number) => Promise, resolve(a * 2));

const newVal1 = fn.apply(v); // Promise<Identity<number>> with value 4
const newVal2 = v.apply(fn); // Promise<Identity<number>> with value 4

Identity#chain

function chain<Val, NewVal>(fn: (val: Val) => Identity<NewVal>): Identity<NewVal>;
  • Returns mapped by fn function value wrapped by Identity Example:
const v = from(2);

const newVal = v1.chain(a => from(a.toString())); // Identity<string> with value "2"
Identity#asyncChain
function asyncChain<Val, NewVal>(fn: (val: Val) => Promise<Identity<NewVal>>): Promise<Identity<NewVal>>;
  • Returns Promise with mapped by fn function value wrapped with Identity. Example:
const v = from(2);

// Promise<Identity<string>> with value "2"
const newVal = v.asyncChain(a => Promise.resolve(from(a.toString())));

Helpers

// Value from Identity instance
const { value } = from(2); // number
const value = from(2).unwrap(); // returns 2

License

MIT (c) Artem Kobzar see LICENSE file.