Installation
Quick Take
Examples
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
andpath
, wherepath
followsobject-path
notation. -
If three arguments are given, it’s setter. You’ll receive a copy of original input, changed accordingly.
Input argument | Type | Obligatory | Description |
---|---|---|---|
input Type: Any Obligatory: yes | |||
input | Any | yes | Something to work upon |
whatToFind Type: String or array of strings Obligatory: yes | |||
whatToFind | String or array of strings | yes | Key names to look for. You can use matcher wildcards in them. |
replacement Type: Array of any Obligatory: no | |||
replacement | Array of any | no | Pass 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
: