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

generate-json-patch-maya

v0.0.6

Published

A simple function to diff any two objects and generate a JSON Patch

Downloads

7

Readme

generate-json-patch

Create RFC 6902 compliant JSON Patch objects based on two given JSON objects with a configurable interface.

Version Downloads/week Size Tests License TypeScript

TL;DR

  • Can diff any two JSON compliant objects - returns differences as JSON Patch.
  • Elegant array diffing by providing an objectHash to match array elements
  • Ignore specific keys by providing a propertyFilter
  • move operations are ALWAYS appended at the end, therefore, they can be ignored (if wanted) when the patch gets applied.
  • :paw_prints: Is it small? Zero dependencies - it's ~7 KB (uncompressed).
  • :crystal_ball: Is it fast? I haven't done any performance comparison yet.
  • :hatched_chick: Is it stable? Test coverage is high, but it's still in its early days - bugs are expected.
  • The interface is inspired by jsondiffpatch
  • 100% Typescript

Installation

Works on node and browser environments.

npm install generate-json-patch

Usage

import { generateJSONPatch } from 'generate-json-patch';

const before = { manufacturer: "Ford", type: "Granada", year: 1972 };
const after = { manufacturer: "Ford", type: "Granada", year: 1974 };

const patch = generateJSONPatch(before, after);

console.log(patch) // => [{op: 'replace', path: '/year', value: 1974}]

Configuration

import { generateJSONPatch, JsonPatchConfig, JsonValue } from 'generate-json-patch';

generateJSONPatch({/*...*/}, {/*...*/}, {
    // called when comparing array elements
    objectHash: function(value: JsonValue, context: GeneratePatchContext) {
        // for arrays of primitive values like string and numbers, a stringification is sufficent:
        // return JSON.stringify(value)
        // If we know the shape of the value, we can match be specific properties
        return value.name
    },
    // called for every property on objects. Can be used to ignore sensitive or irrelevant 
    // properties when comparing data.
    propertyFilter: function (propertyName: string, context: GeneratePatchContext) {
        return !['sensitiveProperty'].includes(propertyName);
    },
    array: {
        // When true, no move operations will be created. 
        // The rersulting patch will not lead to identical objects, 
        // as postions of array elements can be different!
        ignoreMove: true
    }
});

Patch Context

Both config function (objectHash, propertyFilter), receive a patch context as second parameter. This allows for granular decision-making on the provided data.

Example

import {generateJSONPatch, JsonPatchConfig, JsonValue, pathInfo} from 'generate-json-patch';

const before = {
    manufacturer: "Ford",
    type: "Granada",
    colors: ['red', 'silver', 'yellow'],
    engine: [
        { name: 'Cologne V6 2.6', hp: 125 },
        { name: 'Cologne V6 2.0', hp: 90 },
        { name: 'Cologne V6 2.3', hp: 108 },
        { name: 'Essex V6 3.0', hp: 138 },
    ]
}

const after = {
    manufacturer: "Ford",
    type: "Granada",
    colors: ['red', 'silver', 'yellow'],
    engine: [
        {name: 'Essex V6 3.0', hp: 138},
        {name: 'Cologne V6 2.6', hp: 125},
        {name: 'Cologne V6 2.0', hp: 90},
        {name: 'Cologne V6 2.3', hp: 108},
    ]
}

const patch = generateJSONPatch(before, after, {
    objectHash: function (value: JsonValue, context: GeneratePatchContext) {
        const {length, last} = pathInfo(context.path)
        if (length === 2 && last === 'engine') {
            return value.name
        }
        return JSON.stringify(value)
    }
});

console.log(patch) // => [
// { op: 'replace', path: '/engine/3/hp', value: 138 },
// { op: 'move', from: '/engine/2',  path: '/engine/3' },
// { op: 'move', from: '/engine/1',  path: '/engine/2' },
// { op: 'move', from: '/engine/0',  path: '/engine/1' }]

For more examples, check out the tests