fix: convert camelcase function move to util.js
This commit is contained in:
parent
f54260fc27
commit
d21865ca65
@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { convertToSnakeCase } from '../route'
|
||||
import { convertToSnakeCase } from '@/utils/common-utils'
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/libs/prisma'
|
||||
|
||||
import { convertToSnakeCase } from '@/utils/common-utils'
|
||||
/**
|
||||
* 검색 파라미터
|
||||
*/
|
||||
@ -219,30 +219,6 @@ export async function PUT(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// 카멜케이스를 스네이크케이스로 변환하는 함수
|
||||
export const toSnakeCase = (str: string) => {
|
||||
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
||||
}
|
||||
|
||||
// 객체의 키를 스네이크케이스로 변환하는 함수
|
||||
export const convertToSnakeCase = (obj: any): Record<string, any> => {
|
||||
if (obj === null || obj === undefined) return obj;
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map((item: any) => convertToSnakeCase(item))
|
||||
}
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
return Object.keys(obj).reduce((acc, key) => {
|
||||
const snakeKey = toSnakeCase(key).toUpperCase();
|
||||
acc[snakeKey] = convertToSnakeCase(obj[key]);
|
||||
return acc;
|
||||
}, {} as Record<string, any>);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
|
||||
@ -185,3 +185,28 @@ export const isEqualObjects = (obj1, obj2) => {
|
||||
function isObject(value) {
|
||||
return value !== null && typeof value === 'object'
|
||||
}
|
||||
|
||||
|
||||
// 카멜케이스를 스네이크케이스로 변환하는 함수
|
||||
export const toSnakeCase = (str) => {
|
||||
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
||||
}
|
||||
|
||||
// 객체의 키를 스네이크케이스로 변환하는 함수
|
||||
export const convertToSnakeCase = (obj) => {
|
||||
if (obj === null || obj === undefined) return obj;
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map((item) => convertToSnakeCase(item))
|
||||
}
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
return Object.keys(obj).reduce((acc, key) => {
|
||||
const snakeKey = toSnakeCase(key).toUpperCase();
|
||||
acc[snakeKey] = convertToSnakeCase(obj[key]);
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user