@prass/botpress-native
Version:
A simple and powerful SDK for integrating Botpress Chat API with React Native,
51 lines (49 loc) • 1.74 kB
JavaScript
/**
* Cleans an object by removing null, undefined, and empty string values.
* - Recursively removes empty objects and cleans arrays.
*
* @param data - The input object to clean.
* @returns A JSON string of the cleaned object.
*/
const prepareData = (data) => {
if (!data || typeof data !== "object")
return data;
const cleanedData = Object.entries(data).reduce((acc, [key, value]) => {
if (value === null || value === undefined || value === "") {
// Skip null, undefined, and empty strings
return acc;
}
if (typeof value === "object") {
if (Array.isArray(value)) {
// Add type checking for array items
const cleanedArray = value
.map((item) => {
if (item === null || item === undefined)
return null;
return typeof item === "object" ? prepareData(item) : item;
})
.filter(Boolean);
if (cleanedArray.length > 0) {
acc[key] = cleanedArray;
}
}
else {
// Add null check before cleaning nested objects
if (value !== null) {
const cleanedObject = prepareData(value);
if (Object.keys(cleanedObject).length > 0) {
acc[key] = cleanedObject;
}
}
}
}
else {
// Add type validation for primitive values
acc[key] = value;
}
return acc;
}, {});
return cleanedData;
};
export { prepareData };
//# sourceMappingURL=prepareData.js.map