Skip to content

janovekj/track-object-history

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

track-object-history

track-object-history provides a simple function with which you can wrap your object in a proxy that automatically tracks mutations to said object.

It has a couple of use cases:

  • debugging mutation-heavy code
  • simple time travel capabilities

However, if you find yourself reaching for this package, you might want to consider rewriting your code in a immutable fashion instead.

Installation

npm install track-object-history

Usage

Supports the most common ways of mutating an object, i.e. reassigning, deleting and defining properties.

Basic example

import { trackObjectHistory } from "track-object-history";

const [form, history] = trackObjectHistory({
  firstName: "",
  lastName: ""
});

form.firstName = "John";
console.log(form); // { firstName: "John", lastName: "" }
console.log(history); // [{ firstName: "", lastName: "" }]

form.lastName = "Doe";

console.log(form); // { firstName: "John", lastName: "Doe" }
console.log(history); // [{ firstName: "John", lastName: "" }]