redoc/src/utils/object.ts
Bill Collins 127ef260b9
fix: remove GenericObject shim (#2177)
This was declared in the local stubs file but not shipped, meaning that
consumers with typescript libchecking enabled would have to also add the
GenericObject definition to their local stubs file.

This commit inlines the definition where necessary, or replaces it with
object where appropriate.
2022-09-29 14:42:23 +03:00

29 lines
763 B
TypeScript

export function objectHas(object: object, path: string | Array<string>): boolean {
let _path = <Array<string>>path;
if (typeof path === 'string') {
_path = path.split('.');
}
return _path.every((key: string) => {
if (typeof object != 'object' || object === null || !(key in object)) return false;
object = object[key];
return true;
});
}
export function objectSet(object: object, path: string | Array<string>, value: any): void {
let _path = <Array<string>>path;
if (typeof path === 'string') {
_path = path.split('.');
}
const limit = _path.length - 1;
for (let i = 0; i < limit; ++i) {
const key = _path[i];
object = object[key] ?? (object[key] = {});
}
const key = _path[limit];
object[key] = value;
}