mcp-server-airbnb
by openbnb-org
export function cleanObject(obj: any) {
Object.keys(obj).forEach(key => {
if (!obj[key] || key === "__typename") {
delete obj[key];
} else if (typeof obj[key] === "object") {
cleanObject(obj[key]);
}
});
}
export function pickBySchema(obj: any, schema: any): any {
if (typeof obj !== 'object' || obj === null) return obj;
// If the object is an array, process each item
if (Array.isArray(obj)) {
return obj.map(item => pickBySchema(item, schema));
}
const result: Record<string, any> = {};
for (const key in schema) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const rule = schema[key];
// If the rule is true, copy the value as-is
if (rule === true) {
result[key] = obj[key];
}
// If the rule is an object, apply the schema recursively
else if (typeof rule === 'object' && rule !== null) {
result[key] = pickBySchema(obj[key], rule);
}
}
}
return result;
}
export function flattenArraysInObject(input: any, inArray: boolean = false): any {
if (Array.isArray(input)) {
// Process each item in the array with inArray=true so that any object
// inside the array is flattened to a string.
const flatItems = input.map(item => flattenArraysInObject(item, true));
return flatItems.join(', ');
} else if (typeof input === 'object' && input !== null) {
if (inArray) {
// When inside an array, ignore the keys and flatten the object's values.
const values = Object.values(input).map(value => flattenArraysInObject(value, true));
return values.join(': ');
} else {
// When not in an array, process each property recursively.
const result: Record<string, any> = {};
for (const key in input) {
if (Object.prototype.hasOwnProperty.call(input, key)) {
result[key] = flattenArraysInObject(input[key], false);
}
}
return result;
}
} else {
// For primitives, simply return the value.
return input;
}
}