Skip to content

Commit

Permalink
feat: camel-to-snake 함수추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hansanghyeon committed Mar 19, 2024
1 parent 5320f18 commit b7a2f65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyeon/calc-ts",
"version": "1.1.1",
"version": "1.2.0",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"type": "module",
Expand Down Expand Up @@ -35,7 +35,8 @@
"./fp": {
"types": "./lib/fp.d.ts",
"import": "./lib/fp.cjs",
"require": "./lib/fp.mjs"
"require": "./lib/fp.mjs",
"default": "./lib/fp.js"
},
"./package.json": "./package.json"
},
Expand Down
20 changes: 20 additions & 0 deletions src/camel-to-snake/camel-to-snake.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest';
import { camelToSnake } from './camel-to-snake.ts';

describe('camel-to-snake', () => {
it('should convert camelCase to snake_case correctly', () => {
// 테스트 케이스 정의
const cases = [
{ camel: 'simpleTest', snake: 'simple_test' },
{ camel: 'easy', snake: 'easy' },
{ camel: 'HTMLToPDF', snake: 'html_to_pdf' },
{ camel: 'PDFGenerator', snake: 'pdf_generator' },
{ camel: 'thisIsALongTestString', snake: 'this_is_a_long_test_string' }
];

// 각 테스트 케이스 실행
cases.forEach(({ camel, snake }) => {
expect(camelToSnake(camel)).toEqual(snake);
});
});
});
7 changes: 7 additions & 0 deletions src/camel-to-snake/camel-to-snake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function camelToSnake(camel: string): string {
// 연속된 대문자 또는 대문자와 소문자 사이에 밑줄을 추가
return camel
.replace(/([a-z])([A-Z])/g, '$1_$2') // 소문자와 대문자 사이
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') // 연속된 대문자 다음에 소문자가 오는 경우
.toLowerCase(); // 전체 문자열을 소문자로 변환
}
3 changes: 2 additions & 1 deletion src/fp.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './is-snake-case/is-snake-case.fp.ts'
export * from './snake-to-camel/snake-to-camel.ts'
export * from './snake-to-camel/snake-to-camel.ts'
export * from './camel-to-snake/camel-to-snake.ts'
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './css-to-json/css-to-json.ts'
export * from './snake-to-camel/snake-to-camel.ts'
export * from './snake-to-camel/snake-to-camel.ts'
export * from './camel-to-snake/camel-to-snake.ts'

0 comments on commit b7a2f65

Please sign in to comment.