npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-merge-patch

v0.1.0

Published

Typescript-ready version of RFC7396 Merge Patch

Downloads

9

Readme

ts-merge-patch

Typescript ready attempt of RFC 7396 JSON Merge Patch method

Based upon RFC7396: RFC 7396 This specification defines the JSON merge patch document format, processing rules, and associated MIME media type identifier. The merge patch format is primarily intended for use with the HTTP PATCH method [RFC5789] as a means of describing a set of modifications to a target resource's content.

Install

Install the module via NPM

npm

$ npm install ts-merge-patch --save

To use simply import apply from the module import {apply} from 'ts-merge-patch';

Applying a patch to a target origin:

apply<obj1Type,obj2Type>(target: obj1Type, patchItem: obj2Type) : Object | obj2Type | {}

Example

const patch = {
        title: "Hello!",
        phoneNumber: "+01-123-456-7890",
        author: {
            familyName: null,
        },
        tags: ["example"],
    };

const target = {
        title: "Goodbye!",
        author: {
            givenName: "John",
            familyName: "Doe",
        },
        tags: ["example", "sample"],
        content: "This will be unchanged",
    };

const updated = apply(target,patch);

console.log(updated);
{
    title: "Hello!",
    author: {
    givenName: "John",
    },
    tags: ["example"],
    content: "This will be unchanged",
    phoneNumber: "+01-123-456-7890",
}
import { apply } from "ts-merge-patch";

const target = { a: 56, b: 65, c: { e: 4, hi: 7 } };
const patch = { c: { e: 444 }, newProp: "test" };
console.log(apply(target, patch));
//{ a: 56, b: 65, c: { e: 444, hi: 7 }, newProp: 'test' }

interface ob1 {
  thisItem: string;
  thatItem: string;
  anotherItem: string;
}
interface ob2 {
  bonusItem: string;
  thisItem: string;
  thatItem: string;
}

interface combined extends ob1, ob2 {}

const target2: ob1 = { anotherItem: "bah", thisItem: "this", thatItem: "that" };
const patch2: ob2 = { thisItem: "this", thatItem: "nope", bonusItem: "bonus" };

const r = apply<ob1, ob2>(target2, patch2) as combined;
/*
{
  anotherItem: 'bah',
  thisItem: 'this',
  thatItem: 'nope',
  bonusItem: 'bonus'
}
*/

Running tests

Jest tests are available, many thanks goes to pierreinglebert as his tests provided the basis for which this test code was written. Just rejigged them to work in Jest.

npm test

License

MIT