You can call the method via the monster namespace new Monster.Data.Pathfinder()
.
<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/monster.js';
console.log(new Monster.Data.Pathfinder())
</script>
Alternatively, you can also integrate this function individually.
<script type="module">
import {Pathfinder} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/data/pathfinder.js';
console.log(new Pathfinder())
</script>
With the help of the pathfinder, values can be read and written from an object construct.
new Pathfinder({
a: {
b: {
f: [
{
g: false,
}
],
}
}
}).getVia("a.b.f.0.g"); // ↦ false
if a value is not present or has the wrong type, a corresponding exception is thrown.
new Pathfinder({}).getVia("a.b.f.0.g"); // ↦ Error
The Pathfinder.exists()
method can be used to check whether access to the path is possible.
new Pathfinder({}).exists("a.b.f.0.g"); // ↦ false
pathfinder can also be used to build object structures. to do this, the Pathfinder.setVia()
method must be used.
obj = {};
new Pathfinder(obj).setVia('a.b.0.c', true); // ↦ {a:{b:[{c:true}]}}