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

json-patch-history

v0.0.1

Published

**JSON Patch History** is a set of interfaces and functions that provide undo/redo JSON Patch tracking, allowing you to manage changes to JSON documents. This package helps track the history of applied patches and easily allows for undo/redo operations on

Downloads

5

Readme

JSON Patch History

JSON Patch History is a set of interfaces and functions that provide undo/redo JSON Patch tracking, allowing you to manage changes to JSON documents. This package helps track the history of applied patches and easily allows for undo/redo operations on JSON objects. Use standard JSON Patch operations (RFC 6902). This package is currently dependent on the immutable-json-patch

Installation

To install the package, use npm:

npm install json-patch-history

Basic Usage

The following example demonstrates how to set up patch tracking, apply patches to a subject, and use undo/redo functionality:

// Setup
import { 
  initializeHistory, 
  getApplyPatches, 
  getUndoPatches, 
  getRedoPatches 
} from 'json-patch-history';
import { immutableJSONPatch } from 'immutable-json-patch';

// Initial subject and history setup
let subject = { foo: 'bar' }; // The object we want to track changes for
let history = initializeHistory(); // Initialize patch history

// Create a patch to modify the subject
const patch = { op: 'replace', path: '/foo', value: 'baz' };

// Apply patch to the subject and track it in the history
const patchesToApply = getApplyPatches(subject, history, [patch]);
subject = immutableJSONPatch(subject, patchesToApply.subjectPatches);
history = immutableJSONPatch(history, patchesToApply.historyPatches);

// Subject and history after applying the patch:
// subject: { foo: 'baz' }
// history.patchStack: [{ op: 'replace', path: '/foo', value: 'baz' }]
// history.undoStack: [{ op: 'replace', path: '/foo', value: 'bar' }]
// history.redoStack: []

// Undo the change
const undoPatches = getUndoPatches(history);
subject = immutableJSONPatch(subject, undoPatches.subjectPatches);
history = immutableJSONPatch(history, undoPatches.historyPatches);

// Subject and history after undo:
// subject: { foo: 'bar' }
// history.patchStack: []
// history.undoStack: []
// history.redoStack: [{ op: 'replace', path: '/foo', value: 'baz' }]

// Redo the change
const redoPatches = getRedoPatches(subject, history);
subject = immutableJSONPatch(subject, redoPatches.subjectPatches);
history = immutableJSONPatch(history, redoPatches.historyPatches);

// Subject and history after redo:
// subject: { foo: 'baz' }
// history.patchStack: [{ op: 'replace', path: '/foo', value: 'baz' }]
// history.undoStack: [{ op: 'replace', path: '/foo', value: 'bar' }]
// history.redoStack: []

API

initializeHistory(): IPatchHistory

Initializes an empty patch history.

interface IPatchHistory {
  patchStack: JSONPatchDocument[];
  undoStack: JSONPatchDocument[];
  redoStack: JSONPatchDocument[];
}

getApplyPatches(subject: object, history: IPatchHistory, patches: JSONPatchDocument): PatchHistoryResult

Applies a set of patches to the subject and updates the history.

Parameters:

  • subject: The object being modified.
  • history: The current patch history.
  • patches: A JSON Patch array to apply to the subject.

Returns:

  • subjectPatches: The patches to apply to the subject.
  • historyPatches: The patches to apply to the history.

getUndoPatches(history: IPatchHistory): PatchHistoryResult

Returns the patches necessary to undo the most recent change to the subject.

getRedoPatches(subject: object, history: IPatchHistory): PatchHistoryResult

Returns the patches necessary to redo the most recently undone change to the subject.

PatchHistoryResult

type PatchHistoryResult = {
  subjectPatches: JSONPatchDocument;
  historyPatches: JSONPatchDocument;
};

Contributing

If you'd like to contribute to this project, feel free to open a pull request or report issues. Contributions are welcome!

License

This package is licensed under the MIT License.