Files
maestro/src/utils/CaseConverter.ts
T

64 lines
1.4 KiB
TypeScript

export const objectCamelToSnake = (object) => {
const objectKeys = Object.keys(object);
const objectValues = Object.values(object);
const newKeys = objectKeys.map((key) => {
return key
.split(/(?=[A-Z])/)
.join("_")
.toLowerCase();
});
objectKeys.forEach((key, index) => {
object[newKeys[index]] = object[key];
if (newKeys[index] !== objectKeys[index]) {
delete object[key];
}
});
objectValues.forEach((value) => {
if (typeof value === "object") {
objectCamelToSnake(value);
}
});
return object;
};
export const objectSnakeToCamel = (object) => {
const objectKeys = Object.keys(object);
const objectValues = Object.values(object);
const newKeys = objectKeys.map((key) => {
return key.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
});
});
objectKeys.forEach((key, index) => {
object[newKeys[index]] = object[key];
if (newKeys[index] !== objectKeys[index]) {
delete object[key];
}
});
objectValues.forEach((value) => {
if (typeof value === "object") {
objectSnakeToCamel(value);
}
});
return object;
};
export const trSnakeToCamel = (object) =>{
const convertedParams = object.params.map((param)=>{
return objectSnakeToCamel(param)
})
object.params = convertedParams
return objectSnakeToCamel(object)
}