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

undy

v0.1.1

Published

Better error handling for undefined values in nested objects.

Downloads

6

Readme

undy Build Status

undy is a small Javascript library to enable better handling for undefined values in deeply nested objects, similar to the null-conditional operator in other languages such as C#. This lets you write what you mean, and deal with undefined values only at the end instead of constantly checking for undefined values at every level of your object.

undy two functions: one that wraps your variable with undy(variable) to change their nested undefined behaviour, and the other to configure the property to get your value out, with undy.configure(newValue). All values retrieved from the new object are similarly wrapped. Call .value, or whatever you set in undy.configure(newValue) to retrieve your raw object back at any point in the chain.

Get Started

To install run npm install undy.

Include it in your project and use it as follows:

import undy, {configure} from 'undy'

var a = {b: {c: {d: 'e'}}}

// Returns either the value or undefined if somewhere in the chain an object doesn't exist
let val = undy(a).b.c.d.value // 'e'
val = undy(a).value // {b: {c: {d: 'e'}}}
val = undy(a).b.x.y.z.value // undefined
val = undy(a).b.value // {c: {d: 'e'}}

// Mutating an object wrapped by undy updates the underlying object
var wrapped = undy(a)
wrapped.b.c.d = 'hello'
console.log(a.b.c.d) // 'hello'

// Primitives return undefined for all property access except directly calling .value
var primitive = undy('hello')
val = primitive.a.value // undefined
val = primitive.a.b.c.value // undefined
val = primitive.value // 'hello'

// Change .value to anything you want by calling configure
const unWrap = Symbol.for('undy')
configure(unWrap)
val = undy(a).b.c.d[unWrap] // 'e'
val = undy(a).z[unWrap] // undefined

Compatability

undy works in all evergreen browsers, and in the latest version of Node.js. However since undy relies on ES2015 proxies, it does not work in IE 11 and below.