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.
- Host: GitHub
- URL: https://github.com/ozum/resettable
- Owner: ozum
- License: mit
- Created: 2018-04-09T15:05:03.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-11T11:48:46.000Z (about 7 years ago)
- Last Synced: 2025-03-14T22:17:28.976Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 2.2 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.hbs
- Changelog: CHANGELOG.md
- License: LICENSE
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.
[](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~}}