Skip to content

Commit

Permalink
Merge pull request #101 from dgreene1/optionalBooleansShouldNotBeAnEr…
Browse files Browse the repository at this point in the history
…rorCase

Optional booleans and Dates should not be an error case
  • Loading branch information
dgreene1 authored Sep 18, 2020
2 parents d91316f + 990c46d commit 3ee5a27
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "2.0.0",
"license": "MIT",
"description": "A convenience method for overwriting only the values you want",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/conditionalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type OnlyOptionalValues<T> = SetDifference<OptionalValues<T>, truthyNonCurlies>;
export type NestedPartialWarningStr = 'mergePartially.deep does not allow a seed object to have values on it that are optional objects. Please use mergePartially.shallow instead. See https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md for more information.';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type truthyNonCurlies = number | Date | string | symbol | Set<any> | any[] | (() => any) | null | undefined;
type truthyNonCurlies = number | boolean | Date | string | symbol | Set<any> | any[] | (() => any) | null | undefined;

export type NoNestedOptionalObjectsDeep<T> = OnlyOptionalValues<T> extends never
? NestedPartialProblemPreventer<T>
Expand Down
35 changes: 35 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,39 @@ describe('mergePartially', () => {
expect(seed.optionalProp).toEqual(undefined);
});

it('should override an optional date if provided, and leave un-overriden dates as-is', () => {
interface IObjWithDate {
aRequiredString: string;
aRequiredDate: Date;
aDate?: Date;
}
const seed: IObjWithDate = {
aRequiredString: 'hello',
aRequiredDate: new Date('2000-01-01'),
};

// Act
const result = mergePartially.deep(seed, {
aRequiredString: 'hello',
aDate: new Date('2020-09-17'),
});

// Assert
// First check that the dates haven't been stringified
expect(result.aDate).toBeInstanceOf(Date);
expect(result.aRequiredDate).toBeInstanceOf(Date);
expect(result).toEqual({
aDate: new Date('2020-09-17'),
aRequiredDate: new Date('2000-01-01'),
aRequiredString: 'hello',
});
// Prove that mergePartially is a pure function
expect(seed).toEqual({
aRequiredString: 'hello',
aRequiredDate: new Date('2000-01-01'),
});
});

it('supports nested objects (automatically with mergePartially.deep)', () => {
interface ITestCase {
a: string;
Expand All @@ -169,6 +202,7 @@ describe('mergePartially', () => {
b3a: string;
b3b: string;
b3c?: string;
b3d?: boolean;
};
};
c: string;
Expand All @@ -194,6 +228,7 @@ describe('mergePartially', () => {
b3: {
b3a: undefined,
b3b: 'new value for b3b',
b3d: false,
},
},
c: 'new c',
Expand Down

0 comments on commit 3ee5a27

Please sign in to comment.