Table of Contents
Quick Take
import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";
assert.deepEqual(
fillMissing(
{
b: "b",
},
{
a: false,
b: false,
c: false,
}
),
{
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(
{
a: {
b: false,
x: "x",
},
z: "z",
},
{
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(
{
a: {
b: {
c: false,
d: false,
},
x: {
y: false,
},
},
z: "z",
},
{
a: {
b: {
c: false,
d: false,
},
x: false,
},
z: false,
},
{
doNotFillThesePathsIfTheyContainPlaceholders: ["lalala", "a.b", "a.x"],
}
),
{
a: {
b: false,
x: false,
},
z: "z",
}
);
opts.useNullAsExplicitFalse
import { strict as assert } from "assert";
import { fillMissing } from "object-fill-missing-keys";
assert.deepEqual(
fillMissing(
{
a: null,
},
{
a: ["z"],
},
{
useNullAsExplicitFalse: true,
}
),
{
a: null,
}
);
assert.deepEqual(
fillMissing(
{
a: null,
},
{
a: ["z"],
},
{
useNullAsExplicitFalse: false,
}
),
{
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(
{
a: {
b: true,
x: "x",
},
z: "z",
},
{
a: {
b: {
c: false,
d: false,
},
x: false,
},
z: false,
},
{
doNotFillThesePathsIfTheyContainPlaceholders: ["a.b"],
}
),
{
a: {
b: {
c: false,
d: false,
},
x: "x",
},
z: "z",
}
);