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

safe-ts

v1.0.0

Published

Type safe deep object selector

Downloads

34

Readme

Safe nested object selector

safe-ts helps you select values from nested objects and arrays, without throwing error Cannot read property 'x' of undefined.

It's compact, supported by all browsers, preserves Typescript typings and code-completion during development by your IDE (like WebStorm or Visual Studio Code).

It also takes an optional default value.

Install

npm i --save safe-ts

Requirements

  • TypeScript >= 2.9
  • NodeJS >= 6

Example Usage

import { safe } from 'safe-ts';

const abc = {
    a: {
        b: [
            {c: 'C-0'}, 
        ],
    }
};

let c1 = safe(_=> abc.a.b[0].c); // 'C-0'

let c9 = safe(_=> abc.a.b[9].c, 'C-9'); // 'C-9'

Alternatives

Logical expressions are very verbose for long paths.

let c2 = (abc.a && abc.a.b && abc.a.b[9] && abc.a.b[9].c) || 'C-9'

Lodash get(...) is more compact, but removes typescript support.

import { get } from 'lodash';

let c9 = get(abc, 'a.b[9].c', 'C-9');

ts-optchain preserves typescript typings and has an elegant syntax. Unfortunately it requires Proxy. Please consider this option if the browser support suits your project.

import { oc } from 'ts-optchain';

let c9 = oc(abc).b.c('C-9');

Reading the ts-optchain source and this article tought me a lot and inspired me to publish my simple alternative.

Type Preservation

safe-ts preserves TypeScript typings and code-completion by IDEs like Visual Studio Code or WebStorm.

const abc = {a: {b: {c: 'C'}}};

let b = safe(_=> abc.a.b, {c:'C default'});

console.log(b.c) // When typing 'b' your code editor suggests '.c'

Optional properties

To traverse optional properties, wrap your object in the all function, included in ts-safe.

import { safe, all } from 'safe-ts';

// Everything is optional.
type ABCDE = {a?: {b?: {c?: {d?: {e?: string}}}}}
const abc:ABCDE = {
    a:{
        b: {
            c: {} // incomplete.
        }
    }
};

let e1 = safe(_=> abc.a.b.c.d.e); // typescript error: Object is possibly 'undefined'
let e2 = safe(_=> all(abc).a.b.c.d.e); // no typescript error. e2 becomes undefined, as expected.

Note: the all function tells typescript all (nested) properties exits. This affects the return value. For instance, using abc from before:

// 'all' tells typescript not to worry whether anything exists.
let c = safe(_=> all(abc).a.b.c);

if (c) {
    // When c exists, typescript falsely assumes d and e exist.
    console.log(c.d.e); // RUNTIME error: Cannot read property
    
    // This works. Typscript normally enforces this. Now it's up to you. 
    if (c.d) {
        console.log(c.d.e);
    }
}

Please keep this in mind when using optional properties.

License

safe-ts is MIT Licensed.