-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add slugify utility #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { default as escapeHtml } from './escapeHtml'; | ||
| export { default as unescapeHtml } from './unescapeHtml'; | ||
| export { default as escapeHtml } from "./escapeHtml"; | ||
| export { default as unescapeHtml } from "./unescapeHtml"; | ||
| export { default as slugify } from "./slugify"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import slugify from "."; | ||
|
|
||
| describe("slugify", () => { | ||
| test("문자열을 slug 형태로 변환한다.", () => { | ||
| const input = " Hello World! This is a Test. "; | ||
| const output = slugify(input); | ||
| expect(output).toBe("hello-world-this-is-a-test"); | ||
| }); | ||
|
|
||
| test("특수 문자가 포함된 문자열을 올바르게 변환한다.", () => { | ||
| const input = "Café & Restaurant @ Downtown #1"; | ||
| const output = slugify(input); | ||
| expect(output).toBe("caf-restaurant-downtown-1"); | ||
| }); | ||
|
|
||
| test("여러 공백과 대시가 포함된 문자열을 올바르게 변환한다.", () => { | ||
| const input = "This is---a test---string"; | ||
| const output = slugify(input); | ||
| expect(output).toBe("this-is-a-test-string"); | ||
| }); | ||
|
|
||
| test("빈 문자열을 처리한다.", () => { | ||
| expect(slugify("")).toBe(""); | ||
| expect(slugify(" ")).toBe(""); | ||
| }); | ||
|
|
||
| test("숫자가 포함된 문자열을 올바르게 변환한다.", () => { | ||
| const input = "Product 123 - Version 2.0"; | ||
| const output = slugify(input); | ||
| expect(output).toBe("product-123-version-20"); | ||
| }); | ||
|
|
||
| test("특수 문자만 있는 경우를 처리한다.", () => { | ||
| const input = "!@#$%^&*()"; | ||
| const output = slugify(input); | ||
| expect(output).toBe(""); | ||
| }); | ||
|
|
||
| test("한글이 포함된 문자열을 처리한다.", () => { | ||
| expect(slugify("안녕하세요")).toBe("안녕하세요"); | ||
| expect(slugify("한글 테스트")).toBe("한글-테스트"); | ||
| expect(slugify("안녕하세요 Hello World")).toBe("안녕하세요-hello-world"); | ||
| expect(slugify("프로젝트 개발")).toBe("프로젝트-개발"); | ||
| }); | ||
|
|
||
| test("slug 형태의 문자열은 그대로 slug 형태로 반환한다.", () => { | ||
| const input = "Hello-World"; | ||
| const output = slugify(input); | ||
| expect(output).toBe("hello-world"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * 문자열을 URL 친화적인 slug 형태로 변환합니다. | ||
| * 공백을 대시(-)로 변환하고, 특수문자를 제거하며, 소문자로 변환합니다. | ||
| * 한글도 지원합니다. | ||
| * | ||
| * @param text - slug로 변환할 문자열 | ||
| * @returns URL 친화적인 slug 문자열 | ||
| */ | ||
| export default function slugify(text: string): string { | ||
| return text | ||
| .toString() | ||
| .trim() | ||
| .toLowerCase() | ||
| .replace(/\s+/g, "-") | ||
| .replace(/[^a-zA-Z0-9가-힣\-]+/g, "") | ||
| .replace(/\-\-+/g, "-") | ||
| .replace(/^-+/, "") | ||
| .replace(/-+$/, ""); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 테스트 케이스도 궁금할 것 같은데 추가가 가능하실까요?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오!! 한 번 테스트해볼게요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
잘 되네요 :)