Monster.Data. Pathfinder

Pathfinder is a class to find a path to an object.

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}]}}

Constructor

new Pathfinder(value)

Parameters:
NameTypeDescription
valuearray | object | Map | Set
Since
  • 1.4.0
License
  • AGPLv3
Throws:

the parameter must not be a simple type

Type
Error
Examples
import {Pathfinder} from '@schukai/monster/source/data/pathfinder.mjs';

let value = new Pathfinder({
    a: {
        b: {
            f: [
                {
                    g: false,
                }
            ],
        }
    }
}).getVia("a.b.f.0.g");

console.log(value);
// ↦ false

try {
    new Pathfinder({}).getVia("a.b.f.0.g");
} catch (e) {
    console.log(e.toString());
    // ↦ Error: the journey is not at its end (b.f.0.g)
}
import {Pathfinder} from '@schukai/monster/source/data/pathfinder.mjs';

let p = new Pathfinder({
    a: {
        x: [
            {c: 1}, {c: 2}
        ],
        y: true
    },
    b: {
        x: [
            {c: 1, d: false}, {c: 2}
        ],
        y: true
    },
});

let r = p.getVia("*.x.*.c");
console.log(r);

Methods

deleteVia(path) → {Pathfinder}

Delete Via Path

.

Delete Via Path

Parameters:
NameTypeDescription
pathstring
Since
  • 1.6.0
Throws:
  • unsupported type

    Type
    TypeError
  • value is not a string

    Type
    TypeError
  • value is not an integer

    Type
    TypeError
  • unsupported action for this data type

    Type
    Error
Returns:
Type: 
Pathfinder

exists(path) → {bool}

Parameters:
NameTypeDescription
pathstring
Since
  • 1.4.0
Throws:
  • unsupported type

    Type
    TypeError
  • value is not a string

    Type
    TypeError
  • value is not an integer

    Type
    TypeError
Returns:
Type: 
bool

getVia(path) → {*}

Parameters:
NameTypeDescription
pathstring
Since
  • 1.4.0
Throws:
  • unsupported type

    Type
    TypeError
  • the journey is not at its end

    Type
    Error
  • value is not a string

    Type
    TypeError
  • value is not an integer

    Type
    TypeError
  • unsupported action for this data type

    Type
    Error
Returns:
Type: 
*

setVia(path, value) → {Pathfinder}

Parameters:
NameTypeDescription
pathstring
value*
Since
  • 1.4.0
Throws:
  • unsupported type

    Type
    TypeError
  • value is not a string

    Type
    TypeError
  • value is not an integer

    Type
    TypeError
  • unsupported action for this data type

    Type
    Error
Returns:
Type: 
Pathfinder

setWildCard(wildcard) → {Pathfinder}

set wildcard

.

set wildcard

Parameters:
NameTypeDescription
wildcardstring
Since
  • 1.7.0
Returns:
Type: 
Pathfinder