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 🙏

© 2025 – Pkg Stats / Ryan Hefner

swiftly-optional

v1.1.2

Published

Optionals for JavaScript, inspired by Swift

Downloads

35

Readme

Swiftly Optional

Optionals for JavaScript, inspired by Swift.

Installation

Install via npm:

npm i swiftly-optional

Import the module in Node:

const Optional = require('swiftly-optional')

Or in Babel:

import Optional from 'swiftly-optional';

Usage

First, wrap an object in a new Optional construction:

const myOptional = new Optional(objData)

Now, when accessing an object, you can add an underscore to object properties that may or may not exist, and JS won't throw errors:

myOptional.a_.b_.c_.d

In the example above, if any of the properties a, b, or c do not exist, then undefined will be returned.

Why do I need this?

Handling nested properties of an object in JavaScript is clumsy and verbose. Let's say we want to display a user's hometown as a string with a "city, country" format. A user object might look like this:

user = {
  name: 'Joe',
  profile: {
    location: {
      city: 'Boston',
      country: 'USA'
    } 
  }
}

Or it might be less complete and look like this:

user = {
  name: 'Joe',
  profile: null
}

Or it might have not been found at all, in which case user might simply be undefined. How do we handle these unknowns? Well, of course, in a perfect world, the structure of the object would be entirely consistent and we could always get away with this:

`${user.profile.location.city}, ${user.profile.location.country}`

But if we can't guarantee that, for whatever reason, then we need to check our values before we attempt to access them. What many devs intuitively want to do is:

const location = user.profile.location
if (location) { return `${location.city}, ${location.country}`}

But if user or user.profile are undefined or null, then we'll get an error. So then we can either check for truthiness at every level of nesting:

if (user && user.profile && user.profile.location) {
  const location = user.profile.location
  return `${location.city}, ${location.country}`
}

Or we can use a try/catch block:

let location = ''
try {
  location = user.profile.location
} catch (e) {}
return `${location.city}, ${location.country}`

There are many variations of those two basic approaches, and they do work. But they're not elegant. Swift's optionals provide a simple, clean way of accessing nested values without hitting errors. A Swift optional might look like this:

let location = user.profile?.location
if (location != nil) { return location!.city + ", " + location!.country }

That happens to be remarkably similar to the intuitive approach that most developers want to take in JS, but can't.

Enter Optionals.

Unfortunately, question marks aren't valid variable names, so we can't imitate Swift perfectly. But underscores are valid, and we can use them as a substitute. Here's the example above, with Swiftly Optionals, for JavaScript™️:

let user = new Optional(userData)
let location = user.profile_.location
if (location) { return `${location.city}, ${location.country}` }

Not too shabby. Here's a complete example:

const Optional = require('swiftly-optional')

const users = [
  {
    name: 'Joe',
    profile: {
      location: {
        city: 'Boston',
        country: 'USA'
      } 
    }
  }, {
    name: 'Joe',
    profile: null
  }, 
  undefined
]

users.forEach( user => {
  let location = Optional(user).profile_.location
  if (location) {
    console.log( `${location.city}, ${location.country}` )
  }
})