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

guava-optional

v3.0.1

Published

Support for Guava like optionals in Node.js.

Downloads

41

Readme

Optionals Library

Support for Guava like Optionals in Node.js

view on npm npm module downloads Build Status Gitter

Install

Description

Examples

There are plenty of examples using the Guava's Java API and it should be straight forward to follow these examples. Also you can look at the tests found in the ./spec folder. They include an example of every call possible in the library. That being said I have included some basic use cases below.

Using Optional.of()

Using Optional.or()

Using Optional.absent()

Using Optional.fromNullable()

Missing API or Bugs

Please reach out to me (Cory Parrish) if you would like a new Optional type added or if you think you have found a bug.

NPM Scripts

  1. npm run test - Run linter and unit tests.
  2. npm run ut - Use Maddox to Run Unit Tests.
  3. npm run perf - Use Maddox to Performance metrics.
  4. npm run uap - Use Maddox to Unit Tests and Performance metrics.
  5. npm run lint - Run linter.
  6. npm run docs - Rebuild public API Docs.

Releases

  • 3.0.0
    • Now uses errr interface for preconditions.
    • Redesign of code.
    • Now uses maddox for unit testing.
    • Moves to Node 5 paradigms.

API

Optional

Optional entry point interface.

Kind: global class

Optional.of(item) ⇒ Present

Get a Present instance with the given item that may or may not be defined.

Kind: static method of Optional
Returns: Present - - Instance of the Present class.

| Param | Type | Description | | --- | --- | --- | | item | Object | A value that may or may not be defined. |

Optional.absent() ⇒ Absent

Get the Absent static instance.

Kind: static method of Optional
Returns: Absent - - Absent static instance.

Optional.fromUndefinedable(item) ⇒ Absent | Present

Synonym for fromNullable.

Kind: static method of Optional

| Param | Type | Description | | --- | --- | --- | | item | Object | A value that may or may not be defined. |

Optional.fromNullable(item) ⇒ Absent | Present

Returns the Absent static instance if the given value is not defined otherwise returns a Present instance.

Kind: static method of Optional

| Param | Type | Description | | --- | --- | --- | | item | Object | A value that may or may not be defined. |

Present

Present Class represents an Optional that wraps a value that may or may not be defined.

Kind: global class

new Present(item)

Constructor for the Present class;

| Param | Type | | --- | --- | | item | Object |

present.get() ⇒ Object

Get the wrapped item if it exists.

Kind: instance method of Present
Returns: Object - - If the wrapped item is present, it will be returned.
Throws:

  • Errr - If the wrapped item is not present, the function will throw an Errr.

present.or() ⇒ Object

Get the wrapped item or the second choice.

Kind: instance method of Present
Returns: Object - - If the wrapped item is present, it will be returned. If the wrapped item is not present and the second choice is present, then the second choice will be returned.
Throws:

  • Errr - If the wrapped item and second choice is not present, the function will throw an Errr.

present.orUndefined() ⇒ Object | undefined

Returns the wrapped item or undefined.

Kind: instance method of Present
Returns: Object | undefined - - If the wrapped item exists, it will be returned, else this function will return undefined.

present.orNull() ⇒ Object | undefined

Returns the wrapped item or null.

Kind: instance method of Present
Returns: Object | undefined - - If the wrapped item exists, it will be returned, else this function will return null.

present.isPresent() ⇒ Boolean

Describes if the wrapped item is present.

Kind: instance method of Present
Returns: Boolean - - If the wrapped item exists, this function will return true, else false.

present.transform(func) ⇒ Object | Absent

Transform the wrapped item using the given function.

Kind: instance method of Present
Returns: Object | Absent - - Returns transformed wrapped item it is present. Returns the Absent static instance if the wrapped item is not present.

| Param | Type | Description | | --- | --- | --- | | func | function | The function that will be used to transform the wrapped item. |

Absent

Absent Class represents an Optional that wraps an undefined or null value.

Kind: global class

Absent.get()

Always throws an Errr because the the value is Absent.

Kind: static method of Absent
Throws:

  • Errr

Absent.or(secondChoice) ⇒ Object

If secondChoice is defined, then it will be returned. If secondChoice is undefined or null, then the function will throw.

Kind: static method of Absent
Returns: Object - - The secondChoice passed into the 'or' function.
Throws:

  • Errr - Throw when secondChoice is undefined or null.

| Param | | --- | | secondChoice |

Absent.orUndefined() ⇒ undefined

Always returns undefined because the Absent object has no value.

Kind: static method of Absent

Absent.orNull() ⇒ null

Always returns null because the Absent object has no value.

Kind: static method of Absent

Absent.isPresent() ⇒ undefined

Always returns false because the Absent object represents a non present Optional.

Kind: static method of Absent

Absent.transform() ⇒ undefined

Always returns the Absent static instance because the Absent object has no value to transform.

Kind: static method of Absent