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

koalaesce

v0.6.5

Published

Null coalescing library for JS

Downloads

19

Readme

koalaesce

Build Status Code Climate Test Coverage Dependency Status devDependency Status

About

A simple null-coalescing library for JS, providing behavior much like C#'s ?? operator or Groovy's ?. operator.

koalaesce.get allows you to pass a base object and chain of property names and retrieve the last link in the chain, without worrying about the intermediate null checks. It also features invocation of functions found along the way, and the ability to specify a default value if the chain could not be resolved.

Usage

koalaesce is set up as a utility class, so you should start by requiring it:

let koalaesce = require("koalaesce");

The gulpfile included with koalaesce builds UMD modules, so koalaesce should be with most module systems and is usable within the browser as well as node/io tools.

Two primary methods are exposed from this class: get and getOrDefault. For users preferring exceptions over null, the getOrThrow method is provided. All use the same underlying implementation.

get

koalaesce.get works down through a chain, returning the last link (if it can be found). If a link is missing or a null link is encountered before the end of the chain, it will return null. For example:

let obj = {foo: {bar: 3}};
koalaesce.get(obj, "foo", "bar") === 3;

let obj = {foo: null};
koalaesce.get(obj, "foo", "bar") === null; // null since foo is null, but we still need bar

let obj = {foo: {baz: 3}};
koalaesce.get(obj, "foo", "bar") === null; // null since foo does not have a bar, only baz

To invoke a function encountered along the chain, the function name (and any arguments) should be provided as an array:

let obj = {foo: (a) => { return a+1; }};
koalaesce.get(obj, ["foo", 2]) === 3;

Normal links must not be provided as an array, koalaesce uses arrays to detect when it should attempt to invoke a link.

getOrDefault

koalaesce.getOrDefault behaves almost identically to koalaesce.get, but allows you to specify a default value to be returned rather than null:

let obj = {foo: null};
koalaesce.getOrDefault(obj, 3, "foo", "bar") === 3;

let obj = {foo: {baz: 3}};
koalaesce.getOrDefault(obj, 4, "foo", "bar") === 4;

getOrThrow

koalaesce.getOrThrow will throw an exception rather than returning null if a missing or null link is encountered:

let obj = {foo: null};
try {
    koalaesce.get(obj, "foo", "bar");
} catch (e) {
    e.constructor === NullLinkError;
}

let obj = {foo: {baz: 3}};
try {
    koalaesce.get(obj, "foo", "bar");
} catch (e) {
    e.constructor === MissingLinkError;
}

This can be useful in environments where an exception may trigger other behavior, or if a stacktrace is desired. The link where the error was encountered will be included in the error message.

getNamed

koalaesce.getNamed behaves much like koalaesce.get, but only takes two arguments: the base object and a string of all links in the chain, separated by .s. This is stylistically similar to using the obj.foo.bar syntax, with the additional checks koalaesce provides.

let obj = {foo: {bar: 3}};
koalaesce.getNamed(obj, "foo.bar") === 3;

Note: This does not support property names containing the '.' character, yet. Support for those is planned.

getNamedOrDefault

koalaesce.getNamedOrDefault behaves much like koalaesce.getOrDefault, but only takes three arguments: the base object, a default return value, and a string of links in the chain. It uses the same '.'-separated behavior as getNamed:

let obj = {foo: {baz: 3}};
koalaesce.getNamedOrDefault(obj, 4, "foo.bar") === 4;

Note: The same caveat about property names containing the '.' character applies to getNamedOrDefault as to getNamed.