Skip to content

Commit

Permalink
feat: fpts용 fromSnakeCase 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hansanghyeon committed Jan 10, 2024
1 parent bbebf9c commit 681dc2d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/is-snake-case/is-snake-case.fpts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as O from 'fp-ts/Option'

// O.fromNullable 함수처럼
// false이면 text값을 Some으로 감싸서 반환하고
// true이면 None을 반환하는 함수를 만들어보자.

export function fromSnakeCase(text: string) {
const snakeCaseRegex = /^[a-z0-9]+(_[a-z0-9]+)*$/;
// return snakeCaseRegex.test(text);
return snakeCaseRegex.test(text) ? O.some(text) : O.none
}
16 changes: 15 additions & 1 deletion src/is-snake-case/is-snake-case.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, it } from 'vitest';
import * as O from 'fp-ts/Option'
import { isSnakeCase } from './is-snake-case.ts';
import { fromSnakeCase } from './is-snake-case.fpts.ts';

describe('isSnakeCase', () => {
it('should return true for valid snake case strings', () => {
Expand All @@ -11,4 +13,16 @@ describe('isSnakeCase', () => {
it('should return false for non-snake case strings', () => {
expect(isSnakeCase('ThisIsNotSnakeCase')).toBe(false);
});
});
});

describe('fromSnakeCase', () => {
it('should return None for invalid snake case strings', () => {
expect(fromSnakeCase('ThisIsNotSnakeCase')).toEqual(O.none);
});

it('should return Some for valid snake case strings', () => {
expect(fromSnakeCase('this_is_snake_case')).toEqual(O.some('this_is_snake_case'));
expect(fromSnakeCase('snake_case')).toEqual(O.some('snake_case'));
expect(fromSnakeCase('snake_case123')).toEqual(O.some('snake_case123'));
});
})
2 changes: 1 addition & 1 deletion src/is-snake-case/is-snake-case.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isSnakeCase(text: string): boolean {
const snakeCaseRegex = /^[a-z0-9]+(_[a-z0-9]+)*$/;
return snakeCaseRegex.test(text);
}
}

0 comments on commit 681dc2d

Please sign in to comment.