Skip to content

Commit

Permalink
fix: set the merging to be correct as per what is promised
Browse files Browse the repository at this point in the history
during deep merging, if value of the incoming schema is matching what's already there, now already
there prevails; previously, the schema would prevail

BREAKING CHANGE: during deep merging, if value of the incoming schema is matching what's already
there, now already there prevails; previously, the schema would prevail

fix #51
  • Loading branch information
revelt committed Oct 16, 2022
1 parent 36f5d9c commit 1830aff
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/object-fill-missing-keys/.npmignore
@@ -1,6 +1,7 @@
# folders:
.turbo
coverage
examples
perf
src
test
Expand Down
23 changes: 23 additions & 0 deletions packages/object-fill-missing-keys/src/main.ts
Expand Up @@ -8,6 +8,8 @@ import { version as v } from "../package.json";

const version: string = v;

declare let DEV: boolean;

export interface Obj {
[key: string]: any;
}
Expand Down Expand Up @@ -50,6 +52,7 @@ function fillMissingKeys(
resolvedOpts: Opts,
path = ""
): Obj {
DEV && console.log(`055 fillMissingKeys() starts`);
let incomplete = clone(incompleteOriginal);
if (
existy(incomplete) ||
Expand All @@ -61,7 +64,9 @@ function fillMissingKeys(
allEq(incomplete, resolvedOpts.placeholder)
)
) {
DEV && console.log(`067`);
if (isObj(schema) && isObj(incomplete)) {
DEV && console.log(`069 - it's a plain object`);
// traverse the keys on schema and add them onto incomplete
Object.keys(schema).forEach((key) => {
// calculate the path for current key
Expand Down Expand Up @@ -99,6 +104,7 @@ function fillMissingKeys(
}
});
} else if (Array.isArray(schema) && Array.isArray(incomplete)) {
DEV && console.log(`107 - it's an array`);
if (incomplete.length === 0) {
return schema;
}
Expand All @@ -116,8 +122,17 @@ function fillMissingKeys(
}
}
} else {
DEV && console.log(`125 - mergeAdvanced()`);
return mergeAdvanced(schema, incomplete, {
useNullAsExplicitFalse: resolvedOpts.useNullAsExplicitFalse,
cb: (inputArg1, inputArg2, resultAboutToBeReturned) => {
// if two primitive values are being merged, don't write
// the schema value onto the original
if (typ(inputArg1) === typ(inputArg2)) {
return inputArg2;
}
return resultAboutToBeReturned;
},
});
}
}
Expand Down Expand Up @@ -164,6 +179,14 @@ function fillMissing(incomplete: Obj, schema: Obj, opts?: Partial<Opts>): Obj {

// fill any settings with defaults if missing:
let resolvedOpts: Opts = { ...defaults, ...opts };
DEV &&
console.log(
`184 ${`\u001b[${33}m${`resolvedOpts`}\u001b[${39}m`} = ${JSON.stringify(
resolvedOpts,
null,
4
)}`
);

resolvedOpts.doNotFillThesePathsIfTheyContainPlaceholders = arrayiffy(
resolvedOpts.doNotFillThesePathsIfTheyContainPlaceholders
Expand Down
64 changes: 64 additions & 0 deletions packages/object-fill-missing-keys/test/test.js
Expand Up @@ -1179,4 +1179,68 @@ test("30 - opts.useNullAsExplicitFalse - case #3", () => {
);
});

// https://github.com/codsen/codsen/issues/51
test("31 - case #51", () => {
equal(
fillMissing(
{
a: false,
},
{
a: true,
},
{}
),
{
a: false,
},
"30.01"
);
equal(
fillMissing(
{
a: false,
},
{
a: false,
},
{}
),
{
a: false,
},
"30.02"
);
equal(
fillMissing(
{
a: true,
},
{
a: false,
},
{}
),
{
a: true,
},
"30.03"
);
equal(
fillMissing(
{
a: true,
},
{
a: true,
},
{}
),
{
a: true,
},
"30.04"
);
});

test.run();

0 comments on commit 1830aff

Please sign in to comment.