New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-patcher

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-patcher

Either generates diffs or applies diffs between objects in js (json)

latest
npmnpm
Version
1.7.0
Version published
Maintainers
0
Created
Source

JSON-patcher

Diff

Generate a series of instructions to get from an object to another object.


import { diff } from 'json-patcher';

let a = {
    'foo': 'bar'
}

let b = {
    'bar': 'baz'
}

console.log(diff(a, b));

/*
[
  [
    "DEL",
    [],
    "foo"
  ],
  [
    "SET",
    [
      "bar"
    ],
    "baz"
  ]
]
*/

stringDiff(a, b)

Provides a more human readable format of the diff between a and b

import { stringDiff } from 'json-patcher';

let a = {
    'foo': 'bar'
}

let b = {
    'bar': 'baz'
}

console.log(stringDiff(a, b));

/*
DEL: $ - foo
SET: $.bar - baz
*/

apply(a, instructions)

Takes a set of instructions and performs them on an object. Returns the new object.

import { apply } from 'json-patcher';

let a = {
    'foo': 'bar'
}

let instructions = [
    ['DEL', [], 'foo'],
    ['SET', ['bar'], 'baz']
];

let b = apply(a, instructions)

console.log(b);
/*
{
  "bar": "baz"
}
*/

Nice things :)

These are just the things I remember, it's all just nice of course :P

Noop

The output of diff(a, a) is always an empty set of instructions, which is a noop.

Primitives

The diff and apply functions both work on primitives. Meaning it can generate instructions for converting an object to a primitive and apply those instructions to get a primitive.

All pairs of the following are tested together:

  • empty object
  • number
  • string
  • boolean
  • simple non empty object (e.g. {'foo': 'bar'}
  • deep object (e.g. {'foo': {'bar': 'baz': 'bat'}}})
  • array
  • complex object (e.g. an object with multiple types as keys)
  • null
  • undefined

FAQs

Package last updated on 11 Aug 2024

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