Package Exports
- attrpath
- attrpath/dist/esm/index.js
- attrpath/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (attrpath) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AttrPath
Object Attribute Path Traverser. Safely traverse the javascript attribute tree using a text path representation. You can also check the existence of the path.
Motivation
For example, I didn't like to write the following code every time... humm...
const value = {
    children: {
        john: {hobby: [{name: "Cycling"}, {name: "Dance"}], pet: [{type: "dog", name: "Max"}]},
        tom: {hobby: [{name: "Squash"}], pet: [{type: "cat", name: "Chloe"}]}
    }
};
var answer = undefined;
if (value.children) {
    if (value.children.john) {
        if (value.children.john.hobby) {
            if (Array.isArray(value.children.john.hobby)) {
                if (value.children.john.hobby.length >= 2) {
                    if (value.children.john.hobby[1].name) {
                        answer = value.children.john.hobby[1].name;
                    }
                }
            }
        }
    }
}
or
answer = value?.children?.john?.hobby[1]?.name;
The ugly thing above is nice if you can write it like this, right?
const {AttrPath}: any = require("attrpath");
var answer = AttrPath.traverse(value, ".children.john.hobby[1].name");Features
Safely traverse the object path using the given path string. Also, an array may be included in the middle of the path.
Installation
npm install atttrpathNo modules depend on it.
Usage
API
const {AttrPath}: any = require("attrpath");
// or
import {AttrPath} from 'attrpath';traverse value.
AttrPath.traverse(object, path [default_value]);params
| params | meaning | 
|---|---|
| object: any | Target Object. | 
| path: string | Traverse path. The beginning of the path is "." or "[". | 
| e.g. | ".cat.eye.left", ".dog['leg'][1].pad" , etc... | 
| default_value: any | The value to return if there is no corresponding value in the object path. default is "undefined". | 
| default_value: function | If you give a function, give the traverse result to the first argument of the function. | 
result
| result | meaning | 
|---|---|
| result: any | Objects obtained as a result of traverse. | 
path is grammatically valid?
AttrPath.is_valid(path);params
| params | meaning | 
|---|---|
| path: string | Traverse path. | 
result
| result | meaning | 
|---|---|
| result: boolean | path is grammatically correct? | 
Default Value
If the result is Undefined, the default value is returned.
const {AttrPath} = require('attrpath');
    AttrPath.traverse({}, '.path', 1);Example
const {AttrPath}: any = require("attrpath");
const value = {
    children: {
        john: {
            hobby: [{name: "Cycling"}, {name: "Dance"}],
            pet: [{type: "dog", name: "Max"}]
        },
        tom: {
            hobby: [{name: "Squash"}],
            pet: [{type: "cat", name: "Chloe"}]
        }
    }
};
console.log(AttrPath.traverse(value, '.children.john.hobby[0].name'))
> "Max"
console.log(AttrPath.traverse(value, '.children.john.hobby[1].name'))
> undefined
const Default = (n:any) => {
    console.log(n);
}
AttrPath.traverse(value, '.children.john.hobby[0].name', Default)
> "Max"
console.log(AttrPath.is_valid('.children.john.hobby[0].name'))
> true
console.log(AttrPath.is_valid('.children.john.hobby[0]..name'))
> falsemore Example
class Klass {
    member = "name";
    Member() {
        return AttrPath.traverse(this, '.member');
    }
}
const klass = new Klass();
console.log(klass.Member())
> "name"class ParentKlass {
    member = "name";
}
class SubKlass extends ParentKlass {
    Member() {
        return AttrPath.traverse(this, '.member');
    }
}
const sub_klass = new SubKlass();
console.log(sub_klass.Member())
> "name"Example Data
    const value = {
        children: {
            john: {
                hobby: [{name: "Cycling"}, {name: "Dance"}],
                pet: [{type: "dog", name: "Max"}]
            },
            tom: {
                hobby: [{name: "Squash"}],
                pet: [{type: "cat", name: "Chloe"}]
            }
        }
    };ESModule
import {AttrPath} from 'attrpath';
    AttrPath.traverse(value, '.children')
    AttrPath.is_valid('.children["john"].hobby[1].name')CommonJS
const {AttrPath} = require('attrpath');
    AttrPath.traverse(value, '.children');
    AttrPath.is_valid('.children["john"].hobby[1].name')Array
The original value can be an array.
const {AttrPath} = require('attrpath');
    AttrPath.traverse([1], '[0]');Undefined
Returns Undefined if the original value is not an object.
const {AttrPath} = require('attrpath');
    AttrPath.traverse(null, '.path');
    AttrPath.traverse(undefined, '.path');
    AttrPath.traverse(false, '.path');
    AttrPath.traverse(true, '.path');
    AttrPath.traverse(NaN, '.path');
    AttrPath.traverse(Infinity, '.path');
    AttrPath.traverse(0, '.path');
    AttrPath.traverse(-1, '.path');
    AttrPath.traverse("", '.path');
    AttrPath.traverse("1", '.path');
    AttrPath.traverse([1], '.path');
    AttrPath.traverse({}, '.path');Known Bug
If the key contains "." as shown below, it will not work properly.
    const value = {
        "children.john": {
                hobby: [{name: "Cycling"}, {name: "Dance"}],
                pet: [{type: "dog", name: "Max"}]
            }
    };
    NG:
    AttrPath.traverse(value, ".'children.john'");Note
See demo.md for unclear cases.
Author
License
"AttrPath" is under MIT license.