ast-get-values-by-key examples

Table of Contents

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
    },
  }
);

Replace all null's in keys amount with zero, but only under orders

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

const source = {
  orders: [
    {
      date: "2020-09-26",
      amount: null, // replace
      product: "Bramble",
    },
    {
      date: "2020-09-26",
      amount: 2,
      product: "Croodle",
    },
    {
      date: "2020-09-26",
      amount: null, // replace
      product: "Zapple",
    },
  ],
};

// first GET
const findings = getByKey(
  source, // what to process
  "amount" // what to look for
);
assert.deepEqual(findings, [
  { val: null, path: "orders.0.amount" },
  { val: 2, path: "orders.1.amount" },
  { val: null, path: "orders.2.amount" },
]);

// prepare replacement array for SET third input argument
const replacement = findings.map(({ val }) => +val);
assert.deepEqual(replacement, [0, 2, 0]);

// then SET
const result = getByKey(
  source, // what to process
  "amount", // what to look for
  replacement // pot of replacement values to consume (single-use, FIFO stack)
);
assert.deepEqual(result, {
  orders: [
    {
      date: "2020-09-26",
      amount: 0, // replace
      product: "Bramble",
    },
    {
      date: "2020-09-26",
      amount: 2,
      product: "Croodle",
    },
    {
      date: "2020-09-26",
      amount: 0, // replace
      product: "Zapple",
    },
  ],
});

Using wildcards (matcher api)

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

const source = {
  popsicles: 1,
  tentacles: 0,
  nested: [
    {
      cutticles: "yes",
    },
  ],
};

// Two input arguments - getter:
const findings = getByKey(source, ["*cles"]);

assert.deepEqual(findings, [
  {
    val: 1,
    path: "popsicles",
  },
  {
    val: 0,
    path: "tentacles",
  },
  {
    val: "yes",
    path: "nested.0.cutticles",
  },
]);

// notice the path "nested.0.cutticles" is as per object-path notation -
// it's not "nested[0].cutticles" as in "normal" JS paths

// Three input arguments - setter:
assert.deepEqual(
  getByKey(source, ["*cles"], ["a", "b", "c"]),
  {
    popsicles: "a",
    tentacles: "b",
    nested: [
      {
        cutticles: "c",
      },
    ],
  }
);