ast-get-object1.9.18

Getter/setter for nested parsed HTML AST's, querying objects by key/value pairs

§ Quick Take

import { strict as assert } from "assert";
import getObj from "ast-get-object";

// get - two input arguments
assert.deepEqual(
  getObj(
    [
      // <- search in this, the first argument, in this case, a nested array
      {
        tag: "meta",
        content: "UTF-8",
        something: "else",
      },
      {
        tag: "title",
        attrs: "Text of the title",
      },
    ],
    {
      // <- search for this object, the second argument
      tag: "meta",
    }
  ),
  [
    {
      tag: "meta",
      content: "UTF-8",
      something: "else",
    },
  ]
);

// set - three input arguments
assert.deepEqual(
  getObj(
    [
      {
        tag: ["two", "values"],
        content: "UTF-8",
        something: "else",
      },
      {
        tag: "title",
        attrs: "Text of the title",
      },
    ],
    {
      tag: ["two", "values"],
    },
    [
      {
        tag: ["three", "values", "here"],
        content: "UTF-8",
        something: "else",
      },
    ]
  ),
  [
    {
      tag: ["three", "values", "here"], // <--- got updated
      content: "UTF-8",
      something: "else",
    },
    {
      tag: "title",
      attrs: "Text of the title",
    },
  ]
);

§ Purpose

It is a helper function to extract plain objects by certain key-value pairs (if two input arguments given) OR to replace those findings (if three input arguments given).

§ API

getObj(input, keyValPairObj, [replacementContentsArr])

In other words, it's a function which takes two or three input arguments.

§ API - Input

Input argumentTypeObligatory?Description
inputWhateveryesAST tree, or object or array or whatever. Can be deeply-nested.
keyValPairObjPlain objectyesKey/value pairs to look for.
replacementContentsArrArrraynoThe array of new values to set the findings objects. Those values can even be massive nested trees of plain objects and arrays. It doesn't matter.

§ API - Output

Output depends on is it GET mode — 2 arguments, or SET mode — 3 arguments.

  • If it's GET mode, result will be an array of parent objects that hold key/value pairs you asked.

  • If it's SET mode, result will be of the same type as your input, but with all plain objects that had your key/value pairs replaced with contents of third, replacement array. Mind you, if you will supply too few elements in the replacements array, this library won't do anything to those findings.

§ For example, reading or querying parsed trees (GET)

Let's GET all plain objects that contain key tag and value meta. In a true parsed-HTML fashion, everything is in an array, and there are other plain objects around:

const result = getObj(
[
// <- search in this, the first argument, in this case, a nested array
{
tag: "meta",
content: "UTF-8",
something: "else",
},
{
tag: "title",
attrs: "Text of the title",
},
],
{
// <- search for this object, the second argument
tag: "meta",
}
);

result — each parent object that holds your requested key/value pair(s) is put into an array:

[
{
tag: "meta",
content: "UTF-8",
something: "else",
},
];

All findings are always wrapped in an array, even if there's just one finding as above.

§ Writing-over example (SET)

Task: take this nested array of plain objects:

[
{
tag: ["two", "values"],
content: "UTF-8",
something: "else",
},
{
tag: "title",
attrs: "Text of the title",
},
];

Find all plain objects that contain key tag and value ['two', 'values'] (so value is an array!).

Replace all those plain objects with:

{
tag: ['three', 'values', 'here'],
content: 'UTF-8',
something: 'else'
}

Solution:

getObj(
[
{
tag: ["two", "values"],
content: "UTF-8",
something: "else",
},
{
tag: "title",
attrs: "Text of the title",
},
],
{
tag: ["two", "values"],
},
[
{
tag: ["three", "values", "here"],
content: "UTF-8",
something: "else",
},
]
);

PS. Notice that replacement is put into an array. Also, keep in mind that array is like a cartridge — it will expect a separate value for each finding, so we're OK in this case — there was one finding, and one replacement in our array "cartridge".

Result of the above will be:

[
{
tag: ["three", "values", "here"],
content: "UTF-8",
something: "else",
},
{
tag: "title",
attrs: "Text of the title",
},
];

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 ast-deep-contains 1.1.21
Like t.same assert on array of objects, where element order doesn't matter.
📦 ast-contains-only-empty-space 1.9.16
Returns Boolean depending if passed AST contain only empty space
📦 ast-monkey 7.11.22
Traverse and edit AST
📦 ast-monkey-traverse-with-lookahead 1.1.12
Utility library to traverse AST, reports upcoming values
📦 ast-loose-compare 1.8.16
Compare anything: AST, objects, arrays and strings
📦 ast-compare 1.13.19
Compare anything: AST, objects, arrays, strings and nested thereof
📦 ast-monkey-traverse 1.12.20
Utility library to traverse AST