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

prevOpen Source→ast-get-values-by-keynext

ast-get-values-by-key5.2.1

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

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — GETBYKEY()
  • GETTER
  • SETTER
  • API — VERSION
  • API — TYPES
  • Changelog

Installation

Quick Take

Examples

  • Match keys case-sensitively
  • Fetch the values of several keys at once
  • Replace all null's in keys amount with zero, but only under orders
  • Replace each match with the next value in the array
  • A single replacement value is used only once
  • Set the values of keys matched by a wildcard
  • Using wildcard patterns

Purpose

There are many ways to work with AST’s — huge nested trees of objects and arrays. This program is one of the possible ways.

Two arguments trigger the GET mode — it returns an array of objects with value-path findings.

If you map that into desired values array and rerun it, this time putting desired values as a third input argument, you get the SET mode.

API — getByKey()

The main function getByKey() is imported like this:

It’s a function which takes either two or three input arguments:

  • If two arguments are given, it’s getter. You’ll receive an array of zero or more plain objects with keys: val and path, where path follows object-path notation.

  • If three arguments are given, it’s setter. You’ll receive a copy of original input, changed accordingly.

Input argumentTypeObligatoryDescription
input
Type: Any
Obligatory: yes
inputAnyyesSomething to work upon
whatToFind
Type: String or array of strings
Obligatory: yes
whatToFindString or array of stringsyesKey names to look for. You can use matcheropens in a new tab wildcards in them.
replacement
Type: Array of any
Obligatory: no
replacementArray of anynoPass an array of values to replace each finding with

None of the input arguments will be mutated by this program, we have unit tests to prove that.

Getter

To search (“get”) put two input arguments, for example:

const findings = getByKey(source, ["*cles"]);

See the examples section. Paths are using object-path notation. Where vanilla JS path would use brackets to address array elements: nested[0].cutticles, object-path notation uses dots still: nested.0.cutticles.

Setter

To set values, put three input arguments, for example:

const result = getByKey(source, ["*cles"], ["a", "b", "c", "d"]);

The third argument, ["a", "b", "c", "d"] above, is the pot of values to put for each finding. The idea is that you’d use a getter first, prepare the amended values array, then feed it again into this program and change the input (AST or whatever plain object or array or whatever).

API — version

You can import version:

API — types

This package is written in TypeScript and exports the type Findings — one result: the value found (val) and the object-path notation path it was found at.

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

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