chore(utils): improve to handle util (#8487)
This commit is contained in:
committed by
GitHub
parent
eb590417be
commit
c017be2a54
@@ -1,7 +1,8 @@
|
||||
import { toHandle } from "../to-handle"
|
||||
import { isValidHandle } from "../validate-handle"
|
||||
|
||||
describe("normalizeHandle", function () {
|
||||
it("should generate URL friendly handles", function () {
|
||||
describe("should generate URL friendly handles", function () {
|
||||
const expectations = [
|
||||
{
|
||||
input: "The fan boy's club",
|
||||
@@ -22,6 +23,7 @@ describe("normalizeHandle", function () {
|
||||
{
|
||||
input: "-first-product",
|
||||
output: "-first-product",
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "user.product",
|
||||
@@ -30,15 +32,30 @@ describe("normalizeHandle", function () {
|
||||
{
|
||||
input: "_first-product",
|
||||
output: "-first-product",
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "_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) => {
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,5 +14,7 @@ export const toHandle = (value: string): string => {
|
||||
.toLowerCase()
|
||||
.normalize("NFD")
|
||||
.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
|
||||
* friendly.
|
||||
|
||||
Reference in New Issue
Block a user