tfl-ts
Version:
🚇 Fully-typed TypeScript client for Transport for London (TfL) API • Zero dependencies • Auto-generated types • Real-time arrivals • Journey planning • Universal compatibility
32 lines (31 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripTypeFields = void 0;
/**
* Utility function to strip $type fields from objects and handle empty objects
* @param obj - The object to process
* @param keepTflTypes - Whether to keep $type fields
* @returns The processed object with $type fields removed (unless keepTflTypes is true)
*/
const stripTypeFields = (obj, keepTflTypes = false) => {
if (!obj)
return obj;
if (Array.isArray(obj)) {
return obj.map(item => (0, exports.stripTypeFields)(item, keepTflTypes));
}
if (typeof obj === 'object') {
// Handle empty objects with $type
if (Object.keys(obj).length === 1 && obj.$type) {
return 'Unknown';
}
const result = {};
for (const [key, value] of Object.entries(obj)) {
if (key === '$type' && !keepTflTypes)
continue;
result[key] = (0, exports.stripTypeFields)(value, keepTflTypes);
}
return result;
}
return obj;
};
exports.stripTypeFields = stripTypeFields;