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

object-path-resolver

v1.1.0

Published

A simple object path resolver

Downloads

181

Readme

Object Path Resolver

npm

Object Path Resolver is a TypeScript library that provides a flexible and efficient way to resolve nested property paths within JavaScript objects. It allows you to access deeply nested properties, arrays, and even iterate over them asynchronously. This README will guide you through using and understanding the library.

Installation

You can install the object-path-resolver library using npm or yarn:

npm install object-path-resolver

or

yarn add object-path-resolver

Usage

To use object-path-resolver, you need to import the necessary functions and types from the library. Here's a basic example of how to use it:

import { pathResolver, PathResolverOptions } from 'object-path-resolver';

const data = {
    user: {
        profile: {
            name: 'John Doe',
            age: 30,
        },
        hobbies: ['reading', 'swimming'],
    }
};

const path = 'user.profile.name';
const result = pathResolver(data, path);

console.log(result); // Output: 'John Doe'

In this example, we imported the pathResolver function and used it to access the deeply nested property user.profile.name within the data object.

Notes

  • Empty Strings as Keys: object-path-resolver does not support empty strings as keys.
  • Special Characters: Special characters such as backslash (\), dot (.), and asterisk (*) need to be escaped with a backslash (\) if you want to use them as plain actual key characters.
  • Indexes in iterators: Indexes in iterators are not supported (a PR for this feature is welcome!)
  • prototype: Accessing prototype or __proto__ is not allowed by default due to security concerns (you can enable it by passing allowPrototypeAccess: true).

API

  • sync: Optional boolean value that specifies whether to resolve properties synchronously or asynchronously. Defaults to true.
  • missing: Optional value to return when the specified property is not found. Defaults to undefined.
  • allowPrototypeAccess: Optional boolean value that specifies whether to allow access to prototype or __proto__. Defaults to false.

Features

Synchronous and Asynchronous Resolution

object-path-resolver supports both synchronous and asynchronous property resolution. You can specify whether you want to resolve properties synchronously or asynchronously using the sync option:

const syncResult = pathResolver(data, path, { sync: true }); // Synchronous
const asyncResult = pathResolver(data, path, { sync: false }); // Asynchronous

Custom Missing Value

You can specify a custom value to return when the specified property is not found using the missing option:

const missingValue = pathResolver(data, 'user.profile.location', { missing: 'Not found' });
console.log(missingValue); // Output: 'Not found'

Array and Iteration Support

object-path-resolver supports accessing and iterating over array elements and iterable objects:

const data = {
  items: [
    { name: 'Item 1', price: 10 },
    { name: 'Item 2', price: 20 },
  ],
};

const arrayResult = pathResolver(data, 'items.0.name');
console.log(arrayResult); // Output: 'Item 1'

const names = pathResolver(data, 'items.*.name');
console.log(names); // Output: ['Item 1', 'Item 2]

const iterableData = {
  items: (function* () {
    yield 'Item A';
    yield 'Item B';
  })(),
};

const iterableResult = pathResolver(iterableData, 'items.*');
console.log(iterableResult); // Output: ['Item A', 'Item B']

Testing

The library includes test cases to ensure its functionality. You can run the provided tests. Here's an example of how to run the tests:

npm test

Contributions

Contributions to the object-path-resolver library are welcome. If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub repository.

License

This library is open-source and released under the MIT License. You can find the detailed license information in the LICENSE file.


Thank you for using object-path-resolver! We hope it simplifies your JavaScript object property resolution tasks. If you have any questions or need further assistance, please don't hesitate to reach out.