Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ast-get-values-by-key

codsen3.4kMIT5.0.21TypeScript support: included

Extract values and paths from AST by keys OR set them by keys

ast, by, edit, get, html, key, object, parse, parsed, parser, posthtml, posthtml-tree, posthtmltree, read, set, tree, value, write, xhtml, xml

readme

ast-get-values-by-key

Extract values and paths from AST by keys OR set them by keys

page on codsen.com page on npm page on github Downloads per month changelog MIT Licence

Install

This package is pure ESM. If you're not ready yet, install an older version of this program, 3.1.0 (npm i ast-get-values-by-key@3.1.0).

npm i ast-get-values-by-key

Quick Take

import { strict as assert } from "assert";

import { getByKey } from "ast-get-values-by-key";

// GETTER
// ======

// returns "object-path" notation paths where arrays use dots:
assert.deepEqual(
  getByKey(
    {
      parsed: [
        {
          tag: "html",
        },
      ],
    },
    "tag", // value to search for
  ),
  [{ val: "html", path: "parsed.0.tag" }],
);

// SETTER
// ======

assert.deepEqual(
  getByKey(
    {
      parsed: [
        {
          tag: "html",
        },
      ],
      foo: {
        tag: null,
      },
      bar: {
        tag: null,
      },
    },
    "tag", // value to search for
    [123, 456], // pot of values to pick from (one result not enough)
  ),
  {
    parsed: [
      {
        tag: 123,
      },
    ],
    foo: {
      tag: 456,
    },
    bar: {
      tag: null, // value pot was depleted and there was nothing left to put here
    },
  },
);

Documentation

Please visit codsen.com for a full description of the API.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub.

Licence

MIT License.

Copyright © 2010-2025 Roy Revelt and other contributors.

ok codsen star

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

5.0.0 (2022-12-01)

BREAKING CHANGES

  • Minimum supported Node version is v14.18; we're dropping v12 support

4.1.0 (2022-08-12)

Features

4.0.0 (2021-09-09)

Features

BREAKING CHANGES

  • programs now are in ES Modules and won't work with Common JS require()

3.1.0 (2021-05-24)

Features

  • config file based major bump blacklisting (e15f9bb)

3.0.15 (2021-04-11)

Reverts

  • Revert "chore: setup refresh" (23cf206)

3.0.1 (2021-01-28)

Fixed

  • add testStats to npmignore (f3c84e9)

3.0.0 (2021-01-23)

Features

  • rewrite in TS and start using named exports (0380ba1)

BREAKING CHANGES

  • previously: import getAllValuesByKey from ... - now import { getByKey } from ...

2.8.0 (2020-11-28)

Accidental version bump during migration to SourceHut. Sorry about that.

2.7.0 (2020-09-27)

Features

  • some rebasing and examples (7fc5cf5)

2.2.0 (2018-12-26)

Features

  • Supplement the getter's result with path (1594a36)

2.1.0 (2018-10-13)

  • Updated all dependencies and restored coverage tracking both via terminal when testing and through coveralls.io

2.0.0 (2018-09-27)

🔨 Major API changes: now the result is not array of strings, but array of plain objects, where previous string is under key val. This lets us get another key - path of that value.

Setter method behaves the same.

1.3.0 (2018-06-13)

GitHub sold us out. In the meantime, we:

  • Migrated to BitBucket (to host repo + perform CI) and Codacy (for code quality audit)
  • Dropped BitHound (RIP) and Travis
  • Removed package-lock

1.2.0 (2018-05-14)

Improvements

  • Now pointing unit tests at ES Modules build, not CommonJS-one. This means, unit test coverage will be correct (higher) because there won't be any missing rows that Babel added which are impossible to cover.
  • Tweaks to ava config in package.json, properly enabling the dev Rollup builds.

1.1.0 (2018-05-02)

Features

  • Set up Prettier
  • Removed package.lock and .editorconfig
  • Wired Rollup to remove comments from non-dev builds. This means we can now leave the console.logs in the source code — Rollup will remove from production code.

1.0.0 (2018-03-11)

  • First public release