js-path-resolver
v1.0.14
Published
Resolves paths in Javascript objects, and provides tools to get, set, replace, or remove the resolved item
Downloads
95
Maintainers
Readme
Overview
Resolves paths in Javascript objects, providing tools to get, set, replace, or remove the resolved item.
Install
Web Browser
<script src="https://unpkg.com/js-path-resolver/dist/index.js"></script>
<script>
const resolver = jsPathResolver.default;
</script>
Node
import resolver from 'js-path-resolver';
Usage
import resolver from 'js-path-resolver'
const state = {
lists:{
todo:[
{caption:'wake up', completed:true},
{caption:'eat', completed:false}
]
}
}
const info = resolver(state, 'lists.todo.1.completed')
const value = info.get()
info.set(true)
// Remove a field from its parent object or array.
resolver(state, 'lists.todo.1').delete()
By default, if a path cannot be resolved up to the parent, an exception will be thrown.
resolver(state, 'foo') // will not throw
resolver(state, 'foo.bar') // will throw
To never throw, use the onError
option. The property exists
will be set to false instead. However, all mutation operations (delete, get, set) will throw.
const info = resolver(state, 'foo.bar', {onError:'continue'}) // will not throw
// info.exists === false
info.get() // will throw
API
See js-path-resolver official documentation for API and tutorials.