You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

merge-partially

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

merge-partially

A convenience method for overwriting only the values you want

0.0.1
Source
npmnpm
Version published
Weekly downloads
3.1K
-21.56%
Maintainers
1
Weekly downloads
 
Created
Source

Coverage Status

merge-partially

mergePartially is a convenience method for overwriting only the values you want

I see lots of TypeScript stuff. Can I use this in JavaScript too?

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.

Why would you want to use this:

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.

First, let's try to write the flexible factory function without mergePartially

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;
}

Now let's refactor using mergePartially

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);
}

Examples

See the unit tests for various examples

FAQs

Package last updated on 24 Jun 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.