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

nulless

v0.2.3

Published

Help you handle horrible null and undfiend.

Downloads

8

Readme

nulless

nulless is a javascript/typescript library that helps you handle null/undefined safely and elegance. nulless provide a class called Maybe to wrap your value, the concept comes from Monad. Maybe has serval functions to handle null check and support chain call, it can also support async with rxjs.

Usage

basics usage:

import { Maybe } from 'nulless';

let maybe = Maybe.nothing();
console.log(maybe.getDangerValue() === null); // => true
console.log(maybe.transform((value) => value + 'inside').isNothing()); // => true, because maybe has no value
maybe.do(value => console.log(value)) // => do nothing, because maybe has no value

maybe = Maybe.just(43);
console.log(maybe.getDangerValue()); // => 43
console.log(maybe.transform(value => value + 'inside').getValue()); // => 43inside
maybe.do(value => console.log(value)); // => 43

code without nulless:

let items = [{id: 1, content: {subContent: 2}}, {id: 2, content: null}, {id: 4, content: {subContent: 6}}];
function getItemSubContent(id) {
    if (!id) return null;

    let item = items.find(e => e.id === id);
    if (!item) return null;
    if (!item.content) return null;

    return item.content.subContent;
}

let v1 = getItemSubContent(null);
if (v1) {
    console.log(v1);
}
let v2 = getItemSubContent(1);
if (v2) {
    console.log(v2);
}
let v3 = getItemSubContent(2);
if (v3) {
    console.log(v3);
}
let v4 = getItemSubContent(3);
if (v4) {
    console.log(v4);
}

code with nulless:

import { Maybe } from 'nulless';

let items = [{
    id: 1,
    content: {
        subContent: 'dsa'
    }
}, {
    id: 2,
    content: null
}, {
    id: 4,
    content: {
        subContent: 'fd'
    }
}];

function getItemSubContent(id) {
    // function transform will return another maybe
    return id.transform(value => items.find(item => item.id === value))
        .transform(value => value.content)
        .transform(value => value.subContent);
}

// function do will call pass-in function if maybe has value
getItemSubContent(Maybe.nothing()).do(value => console.log(value)); // do nothing since get nothing
getItemSubContent(Maybe.just(1)).do(value => console.log(value)); // => dsa, since get actual value
getItemSubContent(Maybe.just(2)).do(value => console.log(value)); // do nothing since get nothing
getItemSubContent(Maybe.just(3)).do(value => console.log(value)); // do nothing since get nothing

async nulless with rxjs:


import { Maybe } from 'nulless';
import { Observable } from 'rxjs';

function getItemSubContent(id) {
    // function transform will return another maybe
    return id.asyncTransform(value => new Observable(subscriber => {
        let items = [{
            id: 1,
            content: {
                subContent: 'dsa'
            }
        }, {
            id: 2,
            content: null
        }, {
            id: 4,
            content: {
                subContent: 'fd'
            }
        }];
        let result = Maybe.just(items.find(item => item.id === value))
            .transform(value => value.content)
            .transform(value => value.subContent);
        subscriber.next(result);
    }));
}

// function do will call pass-in function if maybe has value
getItemSubContent(Maybe.nothing()).subscribe(value => console.log(value)); // do nothing since get nothing id
getItemSubContent(Maybe.just(1)).subscribe(value => console.log(value)); // => dsa, since get actual value
getItemSubContent(Maybe.just(2)).subscribe(value => console.log(value)); // => nothing maybe, since get nothing item
getItemSubContent(Maybe.just(3)).subscribe(value => console.log(value)); // => nothing maybe, since get nothing item

Install

npm install nulless