resettable-object
Reset object to its original state using JSON Patch with less strict rules. Maybe used to undo auto generated configuration data.
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
package.json
file
Modify ;;; const originalPkg = JSON; // Read package.jsonconst pkg: any = ; // Clone it for changes.pkgscriptsmyScript = "echo 1"; // Add some script. // Look if scripts.test can be changed safely. (Not same with "test in scripts", See API.)if pkgscriptstest = "test-different"; const patch = ; fs; // Write package.jsonfs; // Write patch.json
package.json
to its original state
Reset const pkgFromDisk = JSON; // Read package.jsonconst patchFromDisk = JSON; // Read package.json; fs; // Write package.jsonfs; // 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.
Array.<Operation>
| undefined
reset([data], [history], [options]) ⇒ 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
-
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. |
Array.<Operation>
diff([currentObject], [originalObject]) ⇒ Compares two objects and returns operations needed to reset current object into original object.
Kind: global function
Returns: Array.<Operation>
-
Param | Type | Default | Description |
---|---|---|---|
[currentObject] | object |
{} |
Object to be used in |
[originalObject] | object |
{} |
Original object to get after reset operation. |
boolean
mayChange(currentObject, originalObject, path) ⇒ 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
-
Param | Type | Description |
---|---|---|
currentObject | object |
Current object. |
originalObject | object |
Original object. |
path | Array.<(string|number)> | string | number |
Path to get result for. |
string
| number
| Array.<(number|string)>
Path : 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": ... } }
Object
Operation : Type for storing operations.
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
op | string |
Operation to execte: |
path | string |
Path to make changes at. |
value | * |
Value to be used in operation. |