Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
just-diff-apply
Advanced tools
Apply a diff to an object. Optionally supports jsonPatch protocol
The npm package 'just-diff-apply' is a utility that allows users to apply differences between objects or arrays. It is typically used in scenarios where changes need to be tracked and applied, such as in state management or data synchronization.
Applying differences to objects
This feature allows you to apply a set of differences to an object. In the provided code, a difference is applied to change the value of 'a' from 3 to 4.
const diffApply = require('just-diff-apply');
const obj = {a: 3, b: 5};
const diff = [{op: 'replace', path: ['a'], value: 4}];
diffApply(obj, diff);
console.log(obj); // {a: 4, b: 5}
Applying differences to arrays
This feature enables the application of differences to arrays. In the example, an element with value 5 is added at index 1.
const diffApply = require('just-diff-apply');
const arr = [1, 2, 3];
const diff = [{op: 'add', path: [1], value: 5}];
diffApply(arr, diff);
console.log(arr); // [1, 5, 2, 3]
jsondiffpatch is a library for diffing and patching JSON-structured data. It offers more complex diffing strategies and can handle larger and more complex data structures compared to just-diff-apply.
deep-diff is another npm package that provides a way to observe differences between objects and arrays and apply changes. It is similar to just-diff-apply but also includes functions for reverting changes and detailed tracking of changes.
Part of a library of zero-dependency npm modules that do just do one thing. Guilt-free utilities for every occasion.
npm install just-diff-apply
yarn add just-diff-apply
Apply a diff object to an object. Pass converter to apply a http://jsonpatch.com standard patch
import {diffApply} from 'just-diff-apply';
const obj1 = {a: 3, b: 5};
diffApply(obj1,
[
{ "op": "remove", "path": ['b'] },
{ "op": "replace", "path": ['a'], "value": 4 },
{ "op": "add", "path": ['c'], "value": 5 }
]
);
obj1; // {a: 4, c: 5}
const obj2 = {a: 3, b: 5};
diffApply(obj2,
[
{ "op": "move", "from": ['a'], "path": ['c']},
]
);
obj2; // {b: 5, c: 3}
// using converter to apply jsPatch standard paths
// see http://jsonpatch.com
import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'
const obj3 = {a: 3, b: 5};
diffApply(obj3, [
{ "op": "remove", "path": '/b' },
{ "op": "replace", "path": '/a', "value": 4 }
{ "op": "add", "path": '/c', "value": 5 }
], jsonPatchPathConverter);
obj3; // {a: 4, c: 5}
// arrays (array key can be string or numeric)
const obj4 = {a: 4, b: [1, 2, 3]};
diffApply(obj4, [
{ "op": "replace", "path": ['a'], "value": 3 }
{ "op": "replace", "path": ['b', 2], "value": 4 }
{ "op": "add", "path": ['b', 3], "value": 9 }
]);
obj4; // {a: 3, b: [1, 2, 4, 9]}
// nested paths
const obj5 = {a: 4, b: {c: 3}};
diffApply(obj5, [
{ "op": "replace", "path": ['a'], "value": 5 }
{ "op": "remove", "path": ['b', 'c']}
{ "op": "add", "path": ['b', 'd'], "value": 4 }
]);
obj5; // {a: 5, b: {d: 4}}
FAQs
Apply a diff to an object. Optionally supports jsonPatch protocol
The npm package just-diff-apply receives a total of 1,885,432 weekly downloads. As such, just-diff-apply popularity was classified as popular.
We found that just-diff-apply 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.