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

typesafe-deep-paths

v1.1.2

Published

A TypeScript utility package to safely access and modify deeply nested properties within objects using typed string paths.

Downloads

10

Readme

typesafe-deep-paths

A TypeScript utility package to safely access and modify deeply nested properties within objects using typed string paths. Provides type-safe path autocompletion for known paths while allowing fallback values for paths that may not exist.

Features

  • Type-safe path autocompletion: Access nested properties with autocompletion for valid paths.
  • Fallback support: Define fallback values for paths that may not exist.
  • Flexible access: Handles both known and unknown paths in objects.
  • Deeply nested property access: Easily get and set deeply nested properties using string paths.

Installation

npm install typesafe-deep-paths

Usage

getPath

The getPath function allows you to retrieve the value of a deeply nested property in an object based on a string path. If the path doesn't exist, you can provide a fallback value. It also supports retrieving values from arrays, returning multiple values if the path leads to an array of items.

Syntax:

getPath<T, P extends Path<T> | string, F>(obj: T, path: P, defaultValue?: F): P extends Path<T> ? any : F;
  • T: The object type from which the property is being accessed.
  • P: A string representing the path to the property (type-safe if the path exists in the object).
  • F: The fallback value type to return if the path does not exist.

Example:

const data = {
    user: {
        profile: {
            name: 'Alice',
            address: {
                street: 'Main St',
                city: 'Somewhere',
            },
        },
        subProfiles: [
            {
                name: 'John',
                address: {
                    street: 'Elm St',
                    city: 'Somewhere else',
                },
            },
            {
                name: 'Robb',
                address: {
                    street: 'Second St',
                    city: 'Another Place',
                },
            },
        ],
    },
};

// Accessing a known path - returns the correct value
const street = getPath(data, 'user.profile.address.street');  // 'Main St'

// Accessing an unknown path - returns the default value
const zipCode = getPath(data, 'user.profile.address.zipCode', '00000');  // '00000' (default value)

// Using fallback with unknown path
const age = getPath(data, 'user.profile.age', 30);  // 30 (default since 'age' is missing)

// Accessing multiple entries in an array - returns an array of values
const cities = getPath(data, 'user.subProfiles.address.city');  // ['Somewhere else', 'Another Place']

// Accessing a property that exists only once in an array - returns a single value
const postcode = getPath(data, 'user.subProfiles.address.postcode');  // '00000' (or your default value if not present)

setPath

The setPath function allows you to set the value of a deeply nested property in an object based on a string path. If intermediate objects in the path do not exist, they will be created automatically.

Syntax:

setPath<T, P extends Path<T>>(obj: T, path: P, value: any): void;
  • T: The object type in which the property is being set.
  • P: A string representing the path to the property (type-safe).
  • value: The value to set at the specified path.

Example:

const data = {
  user: {
    profile: {
      name: 'Alice',
    },
  },
};

// Setting a nested property
setPath(data, 'user.profile.address.street', 'Elm St');
console.log(data.user.profile.address.street);  // 'Elm St'

// Creating intermediate objects while setting a value
setPath(data, 'user.profile.contact.email', '[email protected]');
console.log(data.user.profile.contact.email);  // '[email protected]'

TypeScript Support

The package uses TypeScript's advanced types to provide autocompletion and type checking for known object paths. When accessing a known path, TypeScript will suggest the valid nested keys and check for correct types. For unknown paths, you can use fallback values to ensure safe access.