Skip to content

Commit

Permalink
chore: add snake_case and camelCase conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-hh-aa committed Dec 2, 2024
1 parent bc13342 commit 97ee946
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions api.planx.uk/modules/analytics/metabase/collection/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetabaseError, metabaseClient } from "../shared/client.js";
import type { NewCollectionParams } from "./types.js";
import axios from "axios";
import { toSnakeCase } from "../shared/utils.js";

export async function authentication(): Promise<boolean> {
try {
Expand Down Expand Up @@ -28,11 +28,11 @@ export async function newCollection({
}

// If no existing collection, create new one
const requestBody: any = {
const requestBody = toSnakeCase({
name,
description,
parent_id,
};
});

// Remove undefined properties
Object.keys(requestBody).forEach(
Expand Down
34 changes: 34 additions & 0 deletions api.planx.uk/modules/analytics/metabase/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Converts object keys from camelCase to snake_case
*/
export function toSnakeCase<T extends object>(obj: T): Record<string, unknown> {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
// Convert camelCase to snake_case
const snakeKey = key.replace(
/[A-Z]/g,
(letter) => `_${letter.toLowerCase()}`,
);
acc[snakeKey] = value;
return acc;
},
{} as Record<string, unknown>,
);
}

/**
* Converts object keys from snake_case to camelCase
*/
export function toCamelCase<T extends object>(obj: T): Record<string, unknown> {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
// Convert snake_case to camelCase
const camelKey = key.replace(/_([a-z])/g, (_, letter) =>
letter.toUpperCase(),
);
acc[camelKey] = value;
return acc;
},
{} as Record<string, unknown>,
);
}

0 comments on commit 97ee946

Please sign in to comment.