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

optional-neo

v1.0.2

Published

This tiny library allows you to work with object in functional programming style and forget about null checking. More or less it is very similar to Java's implementation of Optional functionality with several useful unique utilities.

Downloads

8

Readme

optional-neo

This tiny library allows you to work with object in functional programming style and forget about null checking. More or less it is very similar to Java's implementation of Optional functionality with several useful unique utilities.

Since it's written in Typescript, you will always able to have correct type of particular object.

Why do you probably need this lib

  • You like functional programming style and do not want to write if in order to check object's nullability
  • You have come to JS/TS world from other ones, where you used to write the code with similar constructions (for example, like in Java)
  • You want to have additional set of useful utilities, what other similar libraries does not provide you
  • You want to use typed library for optional objects

Installation

npm install optional-neo

Interface's methods

  • map(mapping_function) - map value of this Optional via mapping_function and return new one. New value would be result of mapping
  • chain(mapping_function) - similar to map, but here mapping_function returns Optional value
  • filter(filter_function) - return this Optional if filter_function for current value returns true, otherwise empty Optional
  • getOrElse/getOrNull/getOrUndefined - return this value in case of non-empty Optional, otherwise defaulValue/null/undefined
  • isPresent()/isEmpty() - return true in case of non-empty/empty Optional, otherwise false
  • ifPresent(function)/ifAbsent(function) - invoke function in case of non-empty/empty Optional
  • isEqual(another_optional, comparison_function?) - compare this and another_optional Optional. If comparison_function is defined - use it, otherwise use ===
  • backUp(another_optional) - return this Optional if it is non-empty, otherwise another_optional Optional

Utility functions and constants

  • empty - constant for Empty Optional
  • some(value) - create a new Optional with non-null value
  • fromNullable(possibly_nullable_value) - create a new Optional with non-null value or return Empty Optional
  • cleanOptional(possibly_undefined_value) - sometime already created Optional could have undefined value (it's JS world, dude :-) ). In this case cleanOptional function will 'clean' it - returns itself if value is not null or undefined, otherwise returns Empty Optional
  • emptyFunction - function, which return Empty Optional
  • group - group several Optional objects into single one
  • instanceOfOptional(object) - checks, does provided object Optional or not

Examples

  • For React components
import * as React from 'react'
import {fromNullable} from 'optional-neo'

type LabelProps = {
    value?: string;
};

export const Label: React.FC<LabelProps> => ({value}) =>
  fromNullable(value)
      .map(val => (<span>{val}</span>))
      .getOrNull();
  • Grouping several Optionals together into single one and map it somehow:
import {group, some} from 'optional-neo';

const firstDateOpt = some(new Date(2023, 1, 1));
const secondDateOpt = some(new Date(2022, 12, 31));

const isDayBefore = group({
    firstDate: firstDateOpt,
    secondDate: secondDateOpt,
})
    .map((firstDate: Date, secondDate: Date) => firstDate.getTime() < secondDate.getTime())
    .getOrElse(false);
  • Use back up value in case of empty Optional
import {fromNullable, Optional, some} from 'optional-neo';

// Since it is incorrect Date, it will be parsed to NaN. We just filter it out and convert to empty Optional
const resolvedMillis: Optional<number> = fromNullable(Date.parse('date')).filter(millis => !isNaN(millis));
const nowMillis: Optional<number> = some(Date.UTC());

console.log(resolvedMillis.backUp(nowMillis).getOrNull()); // for example, 1679157633434
  • Use chain for inner Optional values
import {} from 'optional-neo';

type TestType = {
    name: Optional<string>;
}

const test: Optional<TestType> = some({name: some('Dima')});
console.log(
    test
        .chain(t => t.name) // return name, Optional<string>
        .getOrNull()        // return value of 'name' Optional
); // Dima

License

MIT licensed