Skip to content

Commit

Permalink
#169 - Updated API Docs to have a request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi-Jacob committed May 18, 2024
1 parent 97b1a93 commit e2a232e
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/app/shared/utilities/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ export function mapSnakeToCamel<T>(obj: T): T {
}
}

// export const camelToSnakeString = (str: string) =>
// str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1_$2').toLowerCase();

// export function mapCamelToSnake<T>(obj: T): T {
// if (Array.isArray(obj)) {
// return obj.map(item => mapCamelToSnake(item)) as unknown as T;
// } else if (typeof obj === 'object' && obj !== null) {
// const newObj: Record<string, unknown> = {};
// for (const key in obj) {
// if (Object.prototype.hasOwnProperty.call(obj, key)) {
// newObj[camelToSnakeString(key)] = mapCamelToSnake(
// (obj as Record<string, unknown>)[key]
// );
// }
// }
// return newObj as unknown as T;
// } else {
// return obj;
// }
// }

export const camelToSnakeString = (str: string) =>
str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1_$2').toLowerCase();

Expand All @@ -29,9 +50,13 @@ export function mapCamelToSnake<T>(obj: T): T {
const newObj: Record<string, unknown> = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
newObj[camelToSnakeString(key)] = mapCamelToSnake(
(obj as Record<string, unknown>)[key]
);
if (key === 'requestBody') {
newObj[key] = (obj as Record<string, unknown>)[key];
} else {
newObj[camelToSnakeString(key)] = mapCamelToSnake(
(obj as Record<string, unknown>)[key]
);
}
}
}
return newObj as unknown as T;
Expand Down

0 comments on commit e2a232e

Please sign in to comment.