What is reftools?
The reftools npm package is a utility library designed for managing and manipulating JSON references within JavaScript objects. It provides tools for resolving, merging, and cloning JSON references, making it useful for handling complex JSON structures that include internal references.
What are reftools's main functionalities?
resolve
This feature resolves JSON references within the document. It replaces references with the actual data they refer to, simplifying the structure for easier manipulation and access.
const reftools = require('reftools');
let doc = { a: { b: { $ref: '#/c' } }, c: { d: 'hello' } };
let resolved = reftools.resolve(doc);
console.log(resolved);
merge
This feature merges two JavaScript objects, incorporating properties from the source object into the destination object. It's particularly useful for combining configurations or settings.
const reftools = require('reftools');
let src = { a: 1 };
let dst = { b: 2 };
reftools.merge(src, dst);
console.log(dst);
clone
This feature creates a deep clone of an object, including resolving any internal JSON references. This is useful for creating independent copies of complex objects that include references.
const reftools = require('reftools');
let original = { a: { b: { $ref: '#/c' } }, c: { d: 'hello' } };
let cloned = reftools.clone(original);
console.log(cloned);
Other packages similar to reftools
json-refs
json-refs is a package that provides tools for resolving references in JSON objects. It is similar to reftools but focuses more on the resolution aspect and includes additional utilities for finding and listing unresolved references.
swagger-tools
swagger-tools is a package primarily used for building and managing Swagger documents but includes functionality for resolving JSON references similar to reftools. It differs in that it is more specialized towards Swagger, whereas reftools is more general-purpose.
RefTools
Constants
- util
LICENSE: MIT
Source: https://simplapi.wordpress.com/2015/08/19/detect-graph-cycle-in-javascript/
removed dependency on underscore, MER
Functions
- clone()
a collection of cloning functions
- flatten()
flattens an object into an array of properties, takes a callback
which can mutate or filter the entries (by returning null)
- jptr()
from obj, return the property with a JSON Pointer prop, optionally setting it
to newValue
- recurse()
recurses through the properties of an object, given an optional starting state
anything you pass in state.payload is passed to the callback each time
- reref()
Simply creates an object without self-references by replacing them
with $ref pointers
- toposort(nodes) ⇒
Array
| Null
Try to get a topological sorting out of directed graph.
- visit()
Given an expanded object and an optional object to compare to (e.g. its $ref'd form), will call
the following functions:
callbacks.before - lets you modify the initial starting state, must return it
callbacks.where - lets you select a subset of properties, return a truthy value
callbacks.filter - called for all selected properties, can mutate/remove (by setting to undefined)
callbacks.compare - allowing the objects to be compared by path (i.e. for $ref reinstating)
callbacks.identity - called on any object identity (previously seen) properties
callbacks.selected - called for all selected/unfiltered properties, does not mutate directly
callbacks.count - called at the end with the number of selected properties
callbacks.finally - called at the end of the traversal
util
LICENSE: MIT
Source: https://simplapi.wordpress.com/2015/08/19/detect-graph-cycle-in-javascript/
removed dependency on underscore, MER
Kind: global constant
clone()
a collection of cloning functions
Kind: global function
flatten()
flattens an object into an array of properties, takes a callback
which can mutate or filter the entries (by returning null)
Kind: global function
jptr()
from obj, return the property with a JSON Pointer prop, optionally setting it
to newValue
Kind: global function
recurse()
recurses through the properties of an object, given an optional starting state
anything you pass in state.payload is passed to the callback each time
Kind: global function
reref()
Simply creates an object without self-references by replacing them
with $ref pointers
Kind: global function
toposort(nodes) ⇒ Array
| Null
Try to get a topological sorting out of directed graph.
Kind: global function
Returns: Array
| Null
- An array if the topological sort could succeed, null if there is any cycle somewhere.
Param | Type | Description |
---|
nodes | Object | A list of nodes, including edges (see below). |
visit()
Given an expanded object and an optional object to compare to (e.g. its $ref'd form), will call
the following functions:
callbacks.before - lets you modify the initial starting state, must return it
callbacks.where - lets you select a subset of properties, return a truthy value
callbacks.filter - called for all selected properties, can mutate/remove (by setting to undefined)
callbacks.compare - allowing the objects to be compared by path (i.e. for $ref reinstating)
callbacks.identity - called on any object identity (previously seen) properties
callbacks.selected - called for all selected/unfiltered properties, does not mutate directly
callbacks.count - called at the end with the number of selected properties
callbacks.finally - called at the end of the traversal
Kind: global function