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

oncha

v0.0.22

Published

ऊंचा Oncha - A modular exalted javascript monadic library & functional fun.

Downloads

1,423

Readme

ऊंचा Oncha

A modular exalted javascript monadic library & functional fun. fantasy-land compliant.

Install

yarn add oncha

Types

| Name | Apply | Applicative | Setoid | Semigroup | Foldable| Functor | Monad | Chain | | ----------------- | :----------: | :--------------: | :----------: | :------------: | :----------: | :----------: | :--------: | :-----------: | | Either | ✔︎ | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | ✔︎ | ✔︎ | | Future | ✔︎ | | ✔︎ | | ✔︎ | ✔︎ | | ✔︎ | | Identity | ✔︎ | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | ✔︎ | ✔︎ | | Maybe | ✔︎ | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | ✔︎ | ✔︎ | | List | | | ✔︎ | ✔︎ | ✔︎ | ✔︎ | | ✔︎ |

  • There is a divergence form fantasy land where reduce is named fold for some types.

All

These functions are available on all types.

ap

Apply

chain :: (a -> b) -> b
Id(5).chain(a => Id(a))
//=> Id(5)

// You can use chain to join the monads.
Id(Id(5)).chain(a => a)
//=> Id(5)

equals

Setoid

equals :: Id -> Boolean
Id(1).equals(Id(1))
//=> true

Id(2).equals(Id(1))
//=> false

Id(2).equals(Id(1)) === Id(1).equals(Id(1))
//=> false

chain

Chain

chain :: (a -> b) -> b
Id(5).chain(a => Id(a))
//=> Id(5)

// You can use chain to join the monads.
Id(Id(5)).chain(a => a)
//=> Id(5)

map

Functor

map :: (a -> b) -> Id of b
Id(7).map(a => a * 2)
//=> Id(14)

of

Applicative

of :: a -> Id of a
Id(5).of(6)
//=> Id(6)

Id(5).of(Id(6))
//=> Id(Id(6))

inspect

inspect :: () -> String
Id(5).inspect()
//=> Id(5)

Id

Identity monad.

import Id from 'oncha/id'
import log from 'nyaya/console/log'

Id(5)
  .map(num => num * 7)
  .map(num => num - 1)
  .fold(log)
//=> 34

fold

Foldable

fold :: (a -> b) -> b
Id(5).fold()
//=> 5

Id(5).fold(a => a + 1)
//=> 6

Maybe

Maybe monad.

import Maybe from 'oncha/maybe'
import log from 'nyaya/console/log'

// Maybe of a string
Maybe('Hello exalted one')
  .map(sentence => sentence.toUpperString())
  .map(sentence => `${sentence}!`)
  .fold(log)
//=> 'HELLO EXALTED ONE!'

// Maybe of nothing
Maybe(null)
  .map(sentence => sentence.toUpperString())
  .else(() => 'Maybe received a null')
  .fold(log)
//=> 'Maybe received a null'

else

Sets the value to fold on.

else :: Any -> Nothing of Any
Maybe(1).else(5).fold()
//=> 1

Maybe(null).else(5).fold()
//=> 5

fold

Foldable

fold :: (a -> b) -> b
Maybe(5).fold()
//=> 5

Maybe(5).fold(a => a + 1)
//=> 6

Either

An Either monad includes cond, fromCond, fromNullable, Left, Right.

import Either from 'oncha/either'
const { Left, Right, fromNullable } = Either

Either.fromNullable('Hello') // this will return a Right('Hello')
  .fold(
    () => 'Oops',
    val => `${val} world!`)
//=> 'Hello world!'

Either.fromNullable(null) // this will return a Left(null)
  .fold(
    () => 'Oops',
    val => `${val} world!`)
//=> 'Oops'

const extractEmail = obj => obj.email ? Right(obj.email) : Left()
extractEmail({ email: '[email protected]' }
  .map(extractDomain)
  .fold(
    () => 'No email found!',
    x => x)
//=> 'example.com'

extractEmail({ name: 'user' }
  .map(extractDomain) // this will not get executed
  .fold(
    () => 'No email found!',
    x => x)
//=> 'No email found!'

cond

A -> B -> C -> Any Evaluates A when truly calls C if it is a function or return C, when falsely calls B if it is a function or returns B.

cond :: (() -> Boolean) -> (() -> c) -> (() -> d) -> c | d
cond :: (() -> Boolean) -> c -> d -> c | d
cond :: Boolean -> b -> c -> b | c
Either.cond(1 === 1)(0)(1)
//=> 1

Either.cond(() => true)(0)(1)
//=> 1

Either.cond(() => true)(() => 0)(() => 1)
//=> 1

Either.cond(true)(() => 0)(1)
//=> 1

fromCond

A -> B -> C -> Either Evaluates A when truly return Right of C or Left or B.

fromCond :: (() -> Boolean) -> a -> b -> Either
fromCond :: Boolean -> a -> b -> Either
Either.fromCond(1 === 1)(0)(1)
//=> Right(1)

Either.fromCond(() => true)(0)(1)
//=> Right(1)

Either.fromCond(() => true)(() => 0)(() => 1)
//=> Right(1)

Either.fromCond(true)(() => 0)(1)
//=> Right(1)

fold

Foldable - Folds on the first function for Left and the second for Right.

fold :: (a -> a, b -> b) -> a | b
Right(5).fold(a => a + 1, a => a + 2)
//=> 7

Left(5).fold(a => a + 1)
//=> 6

List

List Monad.

import List from 'oncha/list'
import log from 'nyaya/console/log'

List([2, 4, 6])
  .map(num => num * 2)
  .filter(num => num > 5)
  .fold(log)
//=> [8, 12]

head

tail

nth

concat

lenght

every

filter

includes

indexOf

inspect

join

lastIndexOf

map

reduce

reduceRight

reverse

slice

some

Future

A Future monad for async computation.

import Future from 'oncha/future'
import log from 'nyaya/console/log'

// Basic usage
Future((reject, resolve) => resolve('Yay'))
  .map(res => res.toUpperString())
  .fork(
    err => log(`Err: ${err}`),
    res => log(`Res: ${res}`))
//=> 'YAY'

// Handle promises
Future.fromPromise(fetch('https://api.awesome.com/catOfTheDay'))
  .fork(
    err => log('There was an error fetching the cat of the day :('),
    cat => log('Cat of the day: ' + cat))
//=> 'Cat of the day: Garfield'

// Chain http calls
Future.fromPromise(fetch('https://api.awesome.com/catOfTheDay'))
  .chain(cat => Future.fromPromise(fetch(`https://api.catfacts.com/${cat}`)))
  .fork(
    err => log('There was an error fetching the cat of the day :('),
    facts => log('Facts for cat of the day: ' + facts))
//=> 'Facts for cat of the day: Garfield is awesome.'

all

Forks all the futures.

all :: ([Futures]) -> b
Future.all(
  Future.of('apple'),
  Future((left, right) => setTimeout(() => right('orange'), 1000)),
  Future.of('lemon')
).fork(
  () => (),
  ([ apple, orange, lemon ]) =>
    //=> apple, orange, lemon
)

fold

Foldable

fold :: (a -> b) -> b
Future.of(5).fold()
//=> 5

Future.of(5).fold(a => a + 1)
//=> 6

fork

Executes the Future returning a Future of the resuts.

fork :: (a -> a, b -> b) -> Future of a | b
Future((left, right) => right(5)).fork(a => a, a => a)
//=> Future of 5

Future((left, right) => left(Error('this is an error'))).fork(a => a)
//=> Future of Error

Higher Order

Compose

Compose takes n functions as arguments and return a function.

import compose from 'oncha/compose'
import log from 'nyaya/console/log'

const transform = compose(sentence => sentence.toUpperString(), sentence => `${sentence}!`)
const logTransform = compose(log, transform)

logTransform('Hello exalted one')
//=> 'HELLO EXALTED ONE!'

// supports miltiple arguments
compose(path.normalize, path.join)('./exalted', '/one')
//=> './exalted/one'

Curry

Creates a partially applicable function.

import curry from 'oncha/curry'

const curried = curry((a, b) => a * b)
curried(3)(6)
//=> 18

curry((a, b, c) => a + b + c)(1, 2, 3)
//=> 6

Fork

Fork as partial application and first class.

import fork from 'oncha/fork'
import future from 'oncha/future'

const fut = Future.of('EXALTED!')

fork(a => a)(b => b)(fut)
//=> 'EXALTED!'

Map

Map as partial application and first class with arity support.

import fork from 'oncha/map'

map(a => a + 1, a => a * 3)([1, 2, 3])
//=> [4, 7, 10]