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-rover

v1.0.0

Published

A package to get, read and modify nested javascript object properties.

Downloads

40

Readme

object-rover

npm version

A package to get, read and modify nested javascript object properties. List all nested leaf properties of object, optionally get their primitive types, then iterate over them and change what you need. Very useful for json validation. This package will be used for a complete rewrite of express-autosanitizer, a popular tool that cleans xss injections from express requests.

Support Me

If this does help you, please consider making a tiny donation here, even small amounts help! 🤝

Why Object Rover?

  • Straightforward, easy functions.
  • Provides typescript types for function parameters and returned values.
  • Supports Async listing of properties, for large objects.
  • You can optionally list primitive types of leaf notes (e.g. string, boolean)
  • Pretty small.
  • Is tested and passing all tests, Almost 100% coverage.
  • Convert recursive traversing of an object into iterating over an array, with support for async!

Getting Started

npm i object-rover

Examples:

const rover = require('object-rover');

const testObj = {
		foo:  null,
		bar: {
		 a: {
		  b:  'hello'
		 },
		 c:  false
		}
	  };
	  
rover.getProperties(testObj)
// ['foo', 'bar.c', 'bar.a.b']

await rover.getPropertiesAsync(testObj)
// ['foo', 'bar.c', 'bar.a.b']

// for a list of types, read types section
rover.getPropertiesWithTypes(testObj)
// [{ path:  'foo', type:  'null' }, 
//  { path:  'bar.c', type:  'boolean' },
//  { path:  'bar.a.b', type:  'string' }]

await rover.getPropertiesWithTypesAsync(testObj)
// [{ path:  'foo', type:  'null' }, 
//  { path:  'bar.c', type:  'boolean' },
//  { path:  'bar.a.b', type:  'string' }]

rover.getProperty(testObj, 'foo') // null
rover.getProperty(testObj, 'bar.a.b') // 'hello'
rover.getProperty(testObj, 'bar.a') 
// testObj.bar.a object will be returned

// set is mutating, so we clone testObj if we need to get a new object
rover.setProperty({ ...testObj }, 'bar.a.b', 44)
// returns an object like testObj except testObj.bar.a.b will be 44

Types

Returned types are JavaScript Primitive types, with null and array. JavaScript recognizes null and arrays as objects, but you might need to filter them out or do something with them, so object-rover indicates that.

  • 'string', 'boolean', 'array', 'undefined', 'number', 'bigint', 'symbol', 'null', 'function'

API

divider is a nonempty string that is used to parse/generate paths, by default it is a dot. if you pass '-' for example, you will get paths like bar-a-b

selector is the path string, e.g. 'bar.a.c'

getProperties(obj: object, divider?: string): string[]

  • Returns array of paths for nested leaf properties of obj

getPropertiesAsync(obj: object, divider?: string): Promise<string[]>

  • Returns a promise that resolves to an array of paths for nested leaf properties of obj

getPropertiesWithTypes(obj: object,divider?: string): { path: string; type: PrimitiveType }[]

  • Returns an array of paths and types for nested leaf properties of obj

getPropertiesWithTypesAsync(obj: object,divider?: string): Promise<{ path: string; type: PrimitiveType }[]>

  • Returns a promise that resolves to an array of paths and types for nested leaf properties of obj

setProperty(obj: object,selector: string,value: any,divider?: string): object

  • Sets the selector path to value and returns the object
  • Mutates the obj, clone if you need immutability like examples above

getProperty(obj: object,selector: string,divider?: string): PrimitiveType | object

  • Gets the value at selector path

License

Distributed under the MIT License. See LICENSE for more information.

Credits

A huge portion of this package is based on code written by coderaiser

Contact

Antonio Ramirez: [email protected]

Project Link: Github

Key Phrases

how to get all nested properties of object how to list all leaf properties of object list nested properties of object list nested primitive properties of object set nested property of object get nested property of object