Nacho Pace Maker

    objdepth

    0.1.0 • Public • Published

    objdepth

    Analyze any value for how deep an object structure it contains and where there might be recursion. Formerly known as object-depth-finder.

    Usage

    from test/usage.js:

    var objectDepthFinder = require('objdepth'), report,
      compactJson = require('ersatz-compactjson'),
      aquarium = {},
      animals = {
        goldfish: { home: aquarium, swim: true },
        cat: { feet: 4, color: ['orange', 'white'] },
      },
      scanOpts = {
        hooks: {
          finishedBranch: objectDepthFinder.hook.toCleanArrays,
        },
        metaPath: true,
      };
    aquarium.fish = [ animals.goldfish ];
    report = objectDepthFinder(animals, scanOpts);
    console.log(compactJson(report));

    Result:

    [ { "level": 0, "type": "object", "depth": 4,
        "path": [] },
      [ { "level": 1, "idx": 0, "key": "goldfish", "type": "object", "depth": 3,
          "path": [ "goldfish" ] },
        [ { "level": 2, "idx": 0, "key": "home", "type": "object", "depth": 2,
            "path": [ "goldfish", "home" ] },
          [ { "level": 3, "idx": 0, "key": "fish", "type": "object", "depth": 1,
              "path": [ "goldfish", "home", "fish" ] },
            [ { "level": 4, "idx": 0, "type": "object", "depth": -3,
                "path": [ "goldfish", "home", "fish", 0 ] },
              "<(circular)>" ] ] ],
        [ { "level": 2, "idx": 1, "key": "swim", "type": "boolean", "depth": 0,
            "path": [ "goldfish", "swim" ] },
          true ] ],
      [ { "level": 1, "idx": 1, "key": "cat", "type": "object", "depth": 2,
          "path": [ "cat" ] },
        [ { "level": 2, "idx": 0, "key": "feet", "type": "number", "depth": 0,
            "path": [ "cat", "feet" ] },
          4 ],
        [ { "level": 2, "idx": 1, "key": "color", "type": "object", "depth": 1,
            "path": [ "cat", "color" ] },
          [ { "level": 3, "idx": 0, "type": "string", "depth": 0,
              "path": [ "cat", "color", 0 ] },
            "orange" ],
          [ { "level": 3, "idx": 1, "type": "string", "depth": 0,
              "path": [ "cat", "color", 1 ] },
            "white" ] ] ] ]
     

    Options

    • circular: (any value) What to put in the value slot for a circular reference. Choose something that, when you see it in your debug dump, reminds you to double-check the depth of that object. If the depth is zero or positive, your tree really contains this value. If the depth is negative, your tree references the object Math.abs(depth) levels up. Default: <(circular)>
    • hooks: (false or object) Meddle with scanning, see below.
    • metaPath: (bool) Whether to include the path property in meta data.
    • sortKeys: (bool or func) Secret ninja option.

    Hooks

    Still beta, but the one shown in the usage example is there to stay.

    Search the source for .hook( to find event names, and for .hook. to find some included handlers.

    License

    ISC

    Install

    npm i objdepth

    DownloadsWeekly Downloads

    0

    Version

    0.1.0

    License

    ISC

    Last publish

    Collaborators

    • mk-pmb