mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-21 16:14:48 +00:00
63 lines
1.4 KiB
TypeScript
63 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 (value == null) return;
|
|
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);
|
|
};
|