@mintlify/common
Version:
Commonly shared code within Mintlify
31 lines (30 loc) • 1.01 kB
JavaScript
/**
* Reads `obj[key]` when `key` is only known at runtime.
*
* Navigation config types such as `DecoratedNavigationConfig` are large discriminated unions
* (`{ languages: … } | { versions: … } | …`). TypeScript cannot type `obj[key]` for `key: string`
* without collapsing the union. Call sites pass validated config from Zod; we narrow with
* `Array.isArray` and `typeof item === 'object'`, then assert each element to `T` once.
*/
export function readObjectArrayProperty(obj, key) {
if (!(key in obj))
return undefined;
const raw = obj[key];
if (!Array.isArray(raw))
return undefined;
const out = [];
for (const item of raw) {
if (typeof item === 'object' && item !== null) {
out.push(item);
}
}
return out;
}
export function iterateObjectArrayProperty(obj, key, visit) {
const items = readObjectArrayProperty(obj, key);
if (!items)
return;
for (const item of items) {
visit(item);
}
}