-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid clobbering default options with undefined values. (#6715)
* Avoid clobbering default options with undefined values. Should fix #5978, using an approach inspired by trojanowski/react-apollo-hooks#77. * Mention PR #6715 in CHANGELOG.md.
- Loading branch information
Showing
9 changed files
with
112 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { compact } from "../compact"; | ||
|
||
const hasOwn = Object.prototype.hasOwnProperty; | ||
|
||
describe("compact", () => { | ||
it("should produce an empty object when called without args", () => { | ||
expect(compact()).toEqual({}); | ||
}); | ||
|
||
it("should merge objects without modifying them", () => { | ||
const a = { a: 1, ay: "a" }; | ||
const b = { b: 2, bee: "b" }; | ||
const c = compact(a, b); | ||
|
||
expect(c).toEqual({ | ||
...a, | ||
...b, | ||
}); | ||
|
||
expect(Object.keys(a)).toEqual(["a", "ay"]); | ||
expect(Object.keys(b)).toEqual(["b", "bee"]); | ||
}); | ||
|
||
it("should clean undefined values from single objects", () => { | ||
const source = { | ||
zero: 0, | ||
undef: void 0, | ||
three: 3, | ||
}; | ||
|
||
const result = compact(source); | ||
|
||
expect(result).toEqual({ | ||
zero: 0, | ||
three: 3, | ||
}); | ||
|
||
expect(Object.keys(result)).toEqual(["zero", "three"]); | ||
}); | ||
|
||
it("should skip over undefined values in later objects", () => { | ||
expect(compact( | ||
{ a: 1, b: 2 }, | ||
{ b: void 0, c: 3 }, | ||
{ a: 4, c: void 0 }, | ||
)).toEqual({ | ||
a: 4, | ||
b: 2, | ||
c: 3, | ||
}); | ||
}); | ||
|
||
it("should not leave undefined properties in result object", () => { | ||
const result = compact({ a: 1, b: void 0 }, { a: 2, c: void 0 }); | ||
expect(hasOwn.call(result, "a")).toBe(true); | ||
expect(hasOwn.call(result, "b")).toBe(false); | ||
expect(hasOwn.call(result, "c")).toBe(false); | ||
expect(result).toEqual({ a: 2 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TupleToIntersection } from './mergeDeep'; | ||
|
||
export function compact<TArgs extends object[]>( | ||
...objects: TArgs | ||
): TupleToIntersection<TArgs> { | ||
const result = Object.create(null); | ||
|
||
objects.forEach(obj => { | ||
if (!obj) return; | ||
Object.keys(obj).forEach(key => { | ||
const value = (obj as any)[key]; | ||
if (value !== void 0) { | ||
result[key] = value; | ||
} | ||
}); | ||
}); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters