An open API service indexing awesome lists of open source software.

https://github.com/ozum/resettable

Reset object to its original state using JSON Patch with less strict rules.
https://github.com/ozum/resettable

Last synced: about 2 months ago
JSON representation

Reset object to its original state using JSON Patch with less strict rules.

Awesome Lists containing this project

README

        

# resettable-object

Reset object to its original state using JSON Patch with less strict rules. Maybe used to undo auto generated configuration data.

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

# Description

Provide functions to get diff of two objects and use that diff to reset object into its original state. Uses less strict rules than JSON Patch. Maybe used to undo auto generated configuration data.

# Synopsis

## Modify `package.json` file

```js
import { mayChange, diff, reset, clone } from "resettable";
import fs from "fs";
import isEqual from "lodash.isEqual";

const originalPkg = JSON.parse(fs.readFileSync(`${__dirname}/../package.json`, "utf8")); // Read package.json
const pkg: any = clone(originalPkg); // Clone it for changes.
pkg.scripts.myScript = "echo 1"; // Add some script.

// Look if scripts.test can be changed safely. (Not same with "test in scripts", See API.)
if (mayChange(pkg, originalPkg, "scripts.test")) {
pkg.scripts.test = "test-different";
}

const patch = diff(pkg, originalPkg);

fs.writeFileSync(`${__dirname}/../package.json`, JSON.stringify(pkg, undefined, 2)); // Write package.json
fs.writeFileSync(`${__dirname}/../patch.json`, JSON.stringify(patch, undefined, 2)); // Write patch.json
```

## Reset `package.json` to its original state

```js
const pkgFromDisk = JSON.parse(fs.readFileSync(`${__dirname}/../package.json`, "utf8")); // Read package.json
const patchFromDisk = JSON.parse(fs.readFileSync(`${__dirname}/../patch.json`, "utf8")); // Read package.json
reset(pkgFromDisk, patchFromDisk);

fs.writeFileSync(`${__dirname}/../package.json`, JSON.stringify(pkgFromDisk, undefined, 2)); // Write package.json
fs.writeFileSync(`${__dirname}/../patch.json`, JSON.stringify({}, undefined, 2)); // Clear patch.json
```

# API
{{>main~}}