package.es.util.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apidom-core Show documentation
Show all versions of apidom-core Show documentation
Tools for manipulating ApiDOM structures.
The newest version!
import { defaultTo, has, mapObjIndexed, path, propSatisfies } from 'ramda';
import { isPlainObject, isString, trimCharsStart } from 'ramda-adjunct';
/**
* This dereference algorithm is used exclusively for dereferencing specification objects.
* It doesn't handle circular references of external references and works on objects only (not arrays).
*/
// eslint-disable-next-line import/prefer-default-export
export const dereference = (object, root) => {
const rootObject = defaultTo(object, root);
return mapObjIndexed(val => {
if (isPlainObject(val) && has('$ref', val) && propSatisfies(isString, '$ref', val)) {
const $ref = path(['$ref'], val);
// @ts-ignore
const pointer = trimCharsStart('#/', $ref);
return path(pointer.split('/'), rootObject);
}
if (isPlainObject(val)) {
return dereference(val, rootObject);
}
return val;
}, object);
};