RefTools

1.1.9 · active · verified Sun Apr 19

RefTools is a JavaScript utility library providing a comprehensive set of functions for working with JavaScript objects, particularly focusing on JSON References, JSON Pointers, and various cloning strategies. It offers deep, shallow, and circular-aware cloning, object flattening, and powerful recursion and visitation mechanisms for complex object structures. Currently at version 1.1.9, the library is actively maintained, as evidenced by its integration into the `oas-kit` monorepo, which includes projects like `swagger2openapi`. While `reftools` itself does not have a rapid, independent release cadence, its core features are crucial for OpenAPI/Swagger tooling, ensuring ongoing relevance and updates. Its key differentiators lie in its robust handling of JSON Pointers/References and advanced object traversal features with configurable callbacks, making it suitable for intricate data manipulation and schema processing tasks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic cloning, JSON Reference dereferencing, JSON Pointer (jptr) usage for getting/setting values, and cloning an object with circular references.

import { clone, dereference, jptr, circularClone } from 'reftools';

const originalObject = {
  a: 1,
  b: { c: 2 },
  d: { $ref: '#/b' }, // JSON Reference
  e: [],
};
originalObject.e.push(originalObject.b); // Introduce a circular reference

console.log('Original Object:', JSON.stringify(originalObject, null, 2));

// 1. Basic cloning
const clonedObject = clone(originalObject);
console.log('\nCloned Object (JSON.parse/stringify):', JSON.stringify(clonedObject, null, 2));
// Note: JSON.parse/stringify cloning loses circular refs and some types

// 2. Dereferencing JSON Pointers
const dereferencedObject = dereference(originalObject);
console.log('\nDereferenced Object (JSON Pointers resolved):', JSON.stringify(dereferencedObject, null, 2));

// 3. Using JSON Pointer (jptr) to get and set values
const valueA = jptr(originalObject, '/a');
console.log(`\nValue at '/a': ${valueA}`);

jptr(originalObject, '/b/c', 99);
console.log('Object after jptr set /b/c to 99:', JSON.stringify(originalObject, null, 2));

// 4. Cloning with circular reference handling
const circularHandledClone = circularClone(originalObject);
console.log('\nCircular-aware cloned object (inspect for circularity - will be simplified):', circularHandledClone);

view raw JSON →