Skip to content

Commit

Permalink
fix(core): add support for "null" type
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Dec 18, 2024
1 parent a582fa5 commit 5daa4c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/@o3r/core/src/utils/deep-fill/deep-fill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ describe('Deep fill function', () => {
expect(deepFill(base, source)).toEqual(base);
});

it('should support "null" value', () => {
const base = { selection: { field: 'test-field' }, a: 'string' };
const source = { selection: null } as any;

expect(deepFill(base, source)).toEqual(base);
});

it('should keep properties from base not present in the source', () => {
const base = Object.freeze({a: 1, b: '2', c: true});
const source = Object.freeze({c: false, a: 3});
Expand Down
4 changes: 2 additions & 2 deletions packages/@o3r/core/src/utils/deep-fill/deep-fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function immutablePrimitive(obj: any, additionalMappers?: PrimitiveRevive
return new Date(obj);
} else if (obj instanceof Object) {
// eslint-disable-next-line no-use-before-define
return deepFill(obj, obj, additionalMappers);
return obj === null ? null : deepFill(obj, obj, additionalMappers);
} else {
return obj;
}
Expand All @@ -68,7 +68,7 @@ export function deepFill<T extends { [x: string]: any }>(base: T, source?: { [x:
for (const key in base) {
if (key in source && typeof base[key] === typeof source[key]) {
const keyOfSource = source[key];
newObj[key] = typeof keyOfSource === 'undefined' ? immutablePrimitive(base[key], additionalMappers) : deepFill(base[key], source[key], additionalMappers);
newObj[key] = typeof keyOfSource === 'undefined' || keyOfSource === null ? immutablePrimitive(base[key], additionalMappers) : deepFill(base[key], keyOfSource, additionalMappers);
} else {
newObj[key] = immutablePrimitive(base[key], additionalMappers);
}
Expand Down

0 comments on commit 5daa4c1

Please sign in to comment.