chore(utils): improve to handle util (#8487)

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-07 11:33:19 -03:00
committed by GitHub
parent eb590417be
commit c017be2a54
3 changed files with 22 additions and 5 deletions
@@ -1,7 +1,8 @@
import { toHandle } from "../to-handle" import { toHandle } from "../to-handle"
import { isValidHandle } from "../validate-handle"
describe("normalizeHandle", function () { describe("normalizeHandle", function () {
it("should generate URL friendly handles", function () { describe("should generate URL friendly handles", function () {
const expectations = [ const expectations = [
{ {
input: "The fan boy's club", input: "The fan boy's club",
@@ -22,6 +23,7 @@ describe("normalizeHandle", function () {
{ {
input: "-first-product", input: "-first-product",
output: "-first-product", output: "-first-product",
invalid: true,
}, },
{ {
input: "user.product", input: "user.product",
@@ -30,15 +32,30 @@ describe("normalizeHandle", function () {
{ {
input: "_first-product", input: "_first-product",
output: "-first-product", output: "-first-product",
invalid: true,
}, },
{ {
input: "_HELLO_WORLD", input: "_HELLO_WORLD",
output: "-hello-world", output: "-hello-world",
invalid: true,
},
{
input: "title: Hello - World",
output: "title-hello-world",
},
{
input: "hiphenated - title - __bold__",
output: "hiphenated-title-bold-",
invalid: true,
}, },
] ]
expectations.forEach((expectation) => { expectations.forEach((expectation) => {
expect(toHandle(expectation.input)).toEqual(expectation.output) const handle = toHandle(expectation.input)
it(`should convert "${expectation.input}" to "${expectation.output}"`, () => {
expect(handle).toEqual(expectation.output)
expect(isValidHandle(handle)).toBe(!expectation.invalid)
})
}) })
}) })
}) })
+3 -1
View File
@@ -14,5 +14,7 @@ export const toHandle = (value: string): string => {
.toLowerCase() .toLowerCase()
.normalize("NFD") .normalize("NFD")
.replace(/[\u0300-\u036f]/g, "") .replace(/[\u0300-\u036f]/g, "")
).replace(/[^a-z0-9A-Z-_]/g, "") )
.replace(/[^a-z0-9A-Z-]/g, "")
.replace(/-{2,}/g, "-")
} }
@@ -1,5 +1,3 @@
import { kebabCase } from "./to-kebab-case"
/** /**
* Helper method to validate entity "handle" to be URL * Helper method to validate entity "handle" to be URL
* friendly. * friendly.