@blue-impact-engine/blue-impact-engine-client
Version:
Blue Impact Engine API Client
75 lines • 2.29 kB
JavaScript
/**
* Data transformation utilities
*/
export const transform = {
/**
* Convert object keys to camelCase
* @param obj - Object to transform
* @returns object - Object with camelCase keys
*/
toCamelCase(obj) {
const camelCaseObj = {};
for (const [key, value] of Object.entries(obj)) {
const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
camelCaseObj[camelKey] = value;
}
return camelCaseObj;
},
/**
* Convert object keys to snake_case
* @param obj - Object to transform
* @returns object - Object with snake_case keys
*/
toSnakeCase(obj) {
const snakeCaseObj = {};
for (const [key, value] of Object.entries(obj)) {
const snakeKey = key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
snakeCaseObj[snakeKey] = value;
}
return snakeCaseObj;
},
/**
* Deep clone an object
* @param obj - Object to clone
* @returns object - Cloned object
*/
deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
},
/**
* Remove undefined and null values from object
* @param obj - Object to clean
* @returns object - Cleaned object
*/
removeEmptyValues(obj) {
const cleaned = {};
for (const [key, value] of Object.entries(obj)) {
if (value !== undefined && value !== null) {
cleaned[key] = value;
}
}
return cleaned;
},
/**
* Flatten nested object
* @param obj - Object to flatten
* @param prefix - Key prefix
* @returns object - Flattened object
*/
flattenObject(obj, prefix = "") {
const flattened = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === "object" &&
value !== null &&
!Array.isArray(value)) {
Object.assign(flattened, this.flattenObject(value, newKey));
}
else {
flattened[newKey] = value;
}
}
return flattened;
},
};
//# sourceMappingURL=transformationUtils.js.map