A simple TypeScript utility function for getting a value from an object by following a path.
- Supports TypeScript!
- Full JSDoc documentation
- Very lightweight (~1.6kB of actual code!)
npm install resolve-object-path --save
<script src="https://joker876.github.io/resolve-object-path/dist/index.min.js"></script>
const resolvePath = require('resolve-object-path');
// or
import resolvePath from 'resolve-object-path';
All declarations are automatically available in all other files.
This package exports only one function, as the default export.
resolvePath(object: object, path: string);
Where:
object
is the object to get the value frompath
is the path to the value
It returns the value the path was pointing to, or undefined
if the object does not contain that path.
- Throws if type of the argument
object
is not"object"
. - Throws if type of the argument
path
is not"string"
. - Throws if the path is invalid, such as
"."
or"v|as.t"
.
import resolvePath from 'reolve-object-path';
const testObj = {
foo: 'abc',
'tes"t': 'pizza',
'bra[ck"][et': 'cheese',
bar: {
baz: -5,
'str-ing': { prop: 86 },
'qux': [{}, { def: 'ghi' }],
'fred': [1, 3, 5],
}
};
resolvePath(testObj, ''); // returns the whole object
resolvePath(testObj, 'foo'); // -> "abc"
resolvePath(testObj, 'bar.baz'); // -> -5
resolvePath(testObj, 'bar.fred[2]'); // -> 5
resolvePath(testObj, '["foo"]'); // -> "abc"
resolvePath(testObj, '["foo"].bar'); // -> -5
resolvePath(testObj, 'bar["str-ing"]'); // -> { prop: 86 }
resolvePath(testObj, 'bar.qux[1].def'); // -> "ghi"
// even works in edge cases such as
resolvePath(testObj, '["tes"t"]'); // -> "pizza"
resolvePath(testObj, '["bra[ck"][et"]'); // -> "cheese"