object-fill-missing-keys examples

Table of Contents

Quick Take

import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";

// deleting key 'c', with value 'd'
assert.deepEqual(
  fillMissing(
    {
      // input object that could have came from JSON
      b: "b",
    },
    {
      // schema reference object
      a: false,
      b: false,
      c: false,
    }
  ),
  {
    // patched result
    a: false,
    b: "b",
    c: false,
  }
);

An option to not fill the paths if they contain placeholders

import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";

assert.deepEqual(
  fillMissing(
    {
      // input object
      a: {
        b: false, // <---- we don't want to automatically normalise this key
        x: "x",
      },
      z: "z",
    },
    {
      // reference schema object
      a: {
        b: {
          c: false,
          d: false,
        },
        x: false,
      },
      z: false,
    },
    {
      doNotFillThesePathsIfTheyContainPlaceholders: [
        "a.b",
      ],
    }
  ),
  {
    a: {
      b: false, // <---
      x: "x",
    },
    z: "z",
  }
);

Truncation upon request, to minimize the object footprint

import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";

assert.deepEqual(
  fillMissing(
    {
      // input object
      a: {
        b: {
          // this object in "b"'s value will be removed and set to placeholder "false"
          c: false,
          d: false,
        },
        x: {
          // this too
          y: false,
        },
      },
      z: "z",
    },
    {
      // schema object
      a: {
        b: {
          c: false,
          d: false,
        },
        x: false,
      },
      z: false,
    },
    {
      // settings
      doNotFillThesePathsIfTheyContainPlaceholders: [
        "lalala",
        "a.b",
        "a.x",
      ],
    }
  ),
  {
    // result
    a: {
      b: false,
      x: false,
    },
    z: "z",
  }
);

opts.useNullAsExplicitFalse

import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";

// on
assert.deepEqual(
  fillMissing(
    {
      // object we're working on
      a: null,
    },
    {
      // reference schema
      a: ["z"],
    },
    {
      // options
      useNullAsExplicitFalse: true, // <--- !
    }
  ),
  {
    // result
    a: null,
  }
);

// off
assert.deepEqual(
  fillMissing(
    {
      // object we're working on
      a: null,
    },
    {
      // reference schema
      a: ["z"],
    },
    {
      // options
      useNullAsExplicitFalse: false, // <--- !
    }
  ),
  {
    // result
    a: ["z"],
  }
);

Using placeholder to cause the value population

import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";

assert.deepEqual(
  fillMissing(
    {
      // object we work upon
      a: {
        b: true, // <-- not placeholder but lower in data hierarchy (boolean)
        x: "x",
      },
      z: "z",
    },
    {
      // reference (schema) object
      a: {
        b: {
          c: false,
          d: false,
        },
        x: false,
      },
      z: false,
    },
    {
      doNotFillThesePathsIfTheyContainPlaceholders: [
        "a.b",
      ],
    }
  ),
  {
    a: {
      b: {
        c: false, // <---- values added!
        d: false, // <---- values added!
      },
      x: "x",
    },
    z: "z",
  }
);