Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ast-get-objectnext

ast-get-object4.2.1

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

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — GETOBJ()
  • API — VERSION
  • API — TYPES
  • READING OR QU…
  • WRITING-OVER…
  • Changelog

Installation

Quick Take

Examples

  • Find every matching object in a nested AST
  • Match an object by a nested value
  • Replace a match with an object that omits an unwanted property
  • Replace matching objects in traversal order

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()

The main function getObj() is imported like this:

It’s a function which takes three input arguments:

Input argumentTypeObligatoryDescription
input
Type: Whatever
Obligatory: yes
inputWhateveryesAST tree, or object or array or whatever. Can be deeply-nested.
keyValPairObj
Type: Plain object
Obligatory: yes
keyValPairObjPlain objectyesKey/value pairs to look for.
replacementContentsArr
Type: Array
Obligatory: no
replacementContentsArrArraynoThe 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.

The program has two modes:

  • if two arguments were passed, it is GET mode
  • if three arguments were passed, it is SET mode

Now, the function’s output depends which mode is it on:

  • 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.

API — version

You can import version:

API — types

This package is written in TypeScript and exports the type UnknownValueObj — a plain object with string keys and values of any type — the shape of the keyValPair argument and of each element of replacementContentsArr.

import type { UnknownValueObj } from "ast-get-object";

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 you’re OK in this case — there was one finding, and one replacement in the array “cartridge”.

Result of the above will be:

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

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab