Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const traverseObjUntilRef = (obj: unknown, path: JsonPath): string | null => {
let piece: unknown = obj;
for (const segment of path.slice()) {
if (!isObject(piece)) {
throw new TypeError('Segment is not a part of the object');
}
if (segment in piece) {
piece = piece[segment];
} else if (hasRef(piece)) {
return piece.$ref;
} else {
throw new Error('Segment is not a part of the object');
}
path.shift();
}
if (isObject(piece) && hasRef(piece) && Object.keys(piece).length === 1) {
return piece.$ref;
}
return null;
};
if (!isObject(piece)) {
throw new TypeError('Segment is not a part of the object');
}
if (segment in piece) {
piece = piece[segment];
} else if (hasRef(piece)) {
return piece.$ref;
} else {
throw new Error('Segment is not a part of the object');
}
path.shift();
}
if (isObject(piece) && hasRef(piece) && Object.keys(piece).length === 1) {
return piece.$ref;
}
return null;
};
function* siblingIterator(obj: object, path: JsonPath): IterableIterator {
for (const key in obj) {
if (!Object.hasOwnProperty.call(obj, key)) continue;
const scopedPath = [...path, key];
if (hasRef(obj) && key !== '$ref') {
yield scopedPath;
}
if (key !== '$ref' && typeof obj[key] === 'object' && obj[key] !== null) {
yield* siblingIterator(obj[key], scopedPath);
}
}
}