31 lines
1,021 B
TypeScript
31 lines
1,021 B
TypeScript
export function cleanOptions(options: Object): Object {
|
|
return Object.fromEntries(
|
|
Object.entries(options).filter(([_, value]) => value !== undefined && value !== null)
|
|
);
|
|
}
|
|
|
|
export function camelToSnake(obj) {
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(camelToSnake);
|
|
} else if (obj !== null && typeof obj === 'object') {
|
|
return Object.keys(obj).reduce((acc, key) => {
|
|
const snakeKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
|
|
acc[snakeKey] = camelToSnake(obj[key]);
|
|
return acc;
|
|
}, {});
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
export function snakeToCamel(obj) {
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(snakeToCamel);
|
|
} else if (obj !== null && typeof obj === 'object') {
|
|
return Object.keys(obj).reduce((acc, key) => {
|
|
const camelKey = key.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
|
|
acc[camelKey] = snakeToCamel(obj[key]);
|
|
return acc;
|
|
}, {});
|
|
}
|
|
return obj;
|
|
}
|