Pathfinder

Monster.Data. Pathfinder

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

Constructor

# new Pathfinder(value)

Parameters:
Name Type Description
value array | object | Map | Set
Since:
  • 1.4.0
Throws:

the parameter must not be a simple type

Type
Error
Examples
import {Pathfinder} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/data/pathfinder.js';

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 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/data/pathfinder.js';

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

Parameters:
Name Type Description
path string
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:
Name Type Description
path string
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:
Name Type Description
path string
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:
Name Type Description
path string
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

Parameters:
Name Type Description
wildcard string
Since:
  • 1.7.0
Returns:
Type
Pathfinder