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.
If this does help you, please consider making a tiny donation here, even small amounts help! 🤝
npm i object-rover
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
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.
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[]
getPropertiesAsync(obj: object, divider?: string): Promise<string[]>
getPropertiesWithTypes(obj: object,divider?: string): { path: string; type: PrimitiveType }[]
getPropertiesWithTypesAsync(obj: object,divider?: string): Promise<{ path: string; type: PrimitiveType }[]>
setProperty(obj: object,selector: string,value: any,divider?: string): object
getProperty(obj: object,selector: string,divider?: string): PrimitiveType | object
Distributed under the MIT License. See LICENSE
for more information.
A huge portion of this package is based on code written by coderaiser
Antonio Ramirez: sepehralizade@live.com
Project Link: Github
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