resettable
TypeScript icon, indicating that this package has built-in type declarations

0.1.19 • Public • Published

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

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

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

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

Functions

reset([data], [history], [options])Array.<Operation> | undefined

Resets given object to its original satate using given array of operations. Different from JSON Patch standard, uses more relaxed rules. For example rejected operations does stop further execution, values to be replaced in arrays are searched in different index positions etc. Please note: This function mutates data object.

diff([currentObject], [originalObject])Array.<Operation>

Compares two objects and returns operations needed to reset current object into original object.

mayChange(currentObject, originalObject, path)boolean

Checks whether value at given path is safe to change by comparing cuurent object to original object. May be used to test whether values in given path are created/modified by user and safe to change. Like "in" operator, returns true if there is no entry for given path. However, differently, it returns true if value exist in modified object but it is different from original, assuming it is modified using this library and may be changed further.

Typedefs

Path : string | number | Array.<(number|string)>

Type for storing path of object or array element as a chained string or array.

Operation : Object

Type for storing operations.

reset([data], [history], [options]) ⇒ Array.<Operation> | undefined

Resets given object to its original satate using given array of operations. Different from JSON Patch standard, uses more relaxed rules. For example rejected operations does stop further execution, values to be replaced in arrays are searched in different index positions etc. Please note: This function mutates data object.

Kind: global function
Returns: Array.<Operation> | undefined -

  • Returns array of operations which are skipped, undefined if all operations are applied.
  • Param Type Default Description
    [data] Object

    Data to be reset.

    [history] Array.<Operation> []

    Array of operations to execute.

    [options] OperationOptions

    Options

    [options.force] boolean false

    Forces operation even it is not safe.

    [options.exact] boolean false

    Modifies find algorithm for arrays. If true, function searches given value in given exact position. Otherwise searches all array.

    [options.checkDuplicate] boolean true

    Checks duplicate values in array. If true, when duplicate value is present, add/replace operation is skipped.

    [options.addNotFound] boolean true

    If true, during replace, adds given value even replaced key is not present in array or object.

    [options.clean] boolean true

    If true, removes empty objects, empty arrays, empty strings, null and undefined values from objects and arrays.

    diff([currentObject], [originalObject]) ⇒ Array.<Operation>

    Compares two objects and returns operations needed to reset current object into original object.

    Kind: global function
    Returns: Array.<Operation> -

    • Array of operations to apply to current object to get original object.
    • Param Type Default Description
      [currentObject] object {}

      Object to be used in reset function.

      [originalObject] object {}

      Original object to get after reset operation.

      mayChange(currentObject, originalObject, path) ⇒ boolean

      Checks whether value at given path is safe to change by comparing cuurent object to original object. May be used to test whether values in given path are created/modified by user and safe to change. Like "in" operator, returns true if there is no entry for given path. However, differently, it returns true if value exist in modified object but it is different from original, assuming it is modified using this library and may be changed further.

      Kind: global function
      Returns: boolean -

      • Whether given path has same value as original path.
      • Param Type Description
        currentObject object

        Current object.

        originalObject object

        Original object.

        path Array.<(string|number)> | string | number

        Path to get result for.

        Path : string | number | Array.<(number|string)>

        Type for storing path of object or array element as a chained string or array.

        Kind: global typedef
        Example

        const path = "member.name"; // { member: { name: ... } }
        const other = ["member", "first.name"]; // { member: { "first.name": ... } }

        Operation : Object

        Type for storing operations.

        Kind: global typedef
        Properties

        Name Type Description
        op string

        Operation to execte: test, remove, add , replace. (test checks the existence of value at given path).

        path string

        Path to make changes at.

        value *

        Value to be used in operation.

        Package Sidebar

        Install

        npm i resettable

        Weekly Downloads

        2

        Version

        0.1.19

        License

        MIT

        Unpacked Size

        52.3 kB

        Total Files

        13

        Last publish

        Collaborators

        • ozum