
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
merge-partially
Advanced tools
mergePartially
is a convenience method for overwriting only the values you want
interface
as the seed objecttl;dr: with mergePartially
helps you fold objects together without overwriting the originals. You can have less brittle tests but with all the flexibility you need.
There are many use cases, but I find this function to be most useful in testing scenarios.
Context: Often times when creating a factory function for tests, you want to be able to create a function that
interface IUser {
id: number;
firstName: string;
lastName: string;
age: number;
}
function makeFakeUser(): IUser {
return {
id: 1,
age: 42,
firstName: 'John',
lastName: 'Smith',
};
}
But what happens when unit test #2 needs the firstName value to be different? If you change the hard-coded value inside of makeFakeUser, then you break unit test #1. So if you don't proceed carefully, then makeFakeUser is at risk of creating brittle tests!
A more flexible approach is provide default values and allow the user to provide their own values.
Ugh this is gonna be long...
function makeFakeUser(overrides?: Partial<IUser>): IUser {
const defaults = {
id: 1,
age: 42,
firstName: 'John',
lastName: 'Smith',
};
const result = {
id: overrides && overrides.id !== undefined ? overrides.id : defaults.id,
age: overrides && overrides.age !== undefined ? overrides.age : defaults.age,
firstName: overrides && overrides.firstName !== undefined ? overrides.firstName : defaults.firstName,
lastName: overrides && overrides.lastName !== undefined ? overrides.lastName : defaults.lastName,
};
return result;
}
Wow look how much fewer lines and characters we have to write to accomplish the same thing:
import { mergePartially, NestedPartial } from 'merge-partially';
function makeFakeUser(overrides?: NestedPartial<IUser>): IUser {
return mergePartially.deep(
{
id: 1,
age: 42,
firstName: 'John',
lastName: 'Smith',
},
overrides
);
}
See the unit tests for various examples.
These two functions have different goals. Object.assign
can merge two different types into a combination type. mergePartially
always returns the same type as the seed object. That's one of many reasons why mergePartially
is safer than Object.assign
.
Yes. Even though the examples are in TypeScript (since it helps to illustrate the problem that mergePartially
solves), you can just remove the type annotations when using mergePartially
.
.deep
allows you to pass multiple levels of partially supplied objects but .shallow
only allows partial objects at the first level.
.deep
allows you to pass in NestedPartial<T>
as where .shallow
only accepts Partial<T>
For example:
interface ISeed {
a: {
b: {
c: string;
d: string;
};
};
}
const seed: ISeed = {
a: {
b: {
c: 'c',
d: 'd',
},
},
};
const deepResult = mergePartially.deep(seed, { a: { b: { d: 'new d' } } });
const shallowResult = mergePartially.shallow(seed, {
a: {
b: {
c: 'I had to supply a value for c here but I did not have to supply it in .deep',
d: 'new d',
},
},
});
.shallow
even necessary?There are some data types that are "less-compatible" with the library and therefore require a workaround (click here for the description). It should be rare that you need to use .shallow
, but you might prefer .shallow
over .deep
anyway for explicitness.
In order to meet the design goals (see above), mergePartially proactively prevents certain data combinations. See this link for more information on the soluton: https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md
PRs are welcome. To contribute, please either make a Github issue or find one you'd like to work on, then fork the repo to make the change.
FAQs
A convenience method for overwriting only the values you want
The npm package merge-partially receives a total of 2,013 weekly downloads. As such, merge-partially popularity was classified as popular.
We found that merge-partially demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.