
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
merge-partially
Advanced tools
mergePartially
is a convenience method for overwriting only the values you want
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
.
tl;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:
function makeFakeUser(overrides?: Partial<IUser>): IUser {
return mergePartially({
id: 1,
age: 42,
firstName: "John",
lastName: "Smith"
}, overrides);
}
See the unit tests for various examples
FAQs
A convenience method for overwriting only the values you want
The npm package merge-partially receives a total of 2,418 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.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
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.