Skip to content
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

fix: getKebabCase #294 #313

Merged
merged 18 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/__tests__/string-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { getKebabCase } from "../string-utils";
describe("getKebabCase", () => {
it.each`
input | expected
${"DKI Jakarta"} | ${"dkijakarta"}
${"Daftar Kontak Fasilitas & Alat Kesehatan per Provinsi"} | ${"daftar-kontak-fasilitas-alat-kesehatanper-provinsi"}
${"Laman Edukasi COVID-19"} | ${"laman-edukasi-covid19"}
${"Donasi dan Penggalangan Dana"} | ${"donasidan-penggalangan-dana"}
${"D.I. Yogyakarta"} | ${"d.i.yogyakarta"}
${"Ambulancesiaga.com"} | ${"ambulancesiaga.com"}
${'"PUTRA SOERADI" Jual Peti Mati'} | ${"putrasoeradijual-peti-mati"}
${"DKI Jakarta"} | ${"dki-jakarta"}
${"Daftar Kontak Fasilitas & Alat Kesehatan per Provinsi"} | ${"daftar-kontak-fasilitas-alat-kesehatan-per-provinsi"}
${"Laman Edukasi COVID-19"} | ${"laman-edukasi-covid-19"}
${"Donasi dan Penggalangan Dana"} | ${"donasi-dan-penggalangan-dana"}
${"D.I. Yogyakarta"} | ${"d-i-yogyakarta"}
${"Ambulancesiaga.com"} | ${"ambulancesiaga-com"}
${'"PUTRA SOERADI" Jual Peti Mati'} | ${"putra-soeradi-jual-peti-mati"}
${"Hoo Hap Hwee (Perkumpulan Budi Abadi)"} | ${"hoo-hap-hwee-perkumpulan-budi-abadi"}
${"UD. Bpk. Giyanto Sedia/Jual Peti Jenazah"} | ${"ud.bpk.giyanto-sedia-jual-peti-jenazah"}
${"Samator, PT. Aneka Gas Industri, Tbk."} | ${"samator-pt.aneka-gas-industri-tbk."}
${"UD. Bpk. Giyanto Sedia/Jual Peti Jenazah"} | ${"ud-bpk-giyanto-sedia-jual-peti-jenazah"}
${"Samator, PT. Aneka Gas Industri, Tbk."} | ${"samator-pt-aneka-gas-industri-tbk"}
`(
"should return '$expected' when '$input' is provided",
({ input, expected }) => {
Expand Down
13 changes: 6 additions & 7 deletions lib/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function replaceSpacesWithCamelCase(str: string): string {
* @returns {string} `str`, without non-alphanumeric characters
*/
export function replaceSpecialCharacterWithSpace(str: string): string {
return str.replace(/[^a-zA-Z0-9. ]/g, " ");
return str.replace(/[^a-zA-Z0-9]/g, " ");
}

/**
Expand All @@ -60,7 +60,10 @@ export function removeSpaces(str: string): string {
* @returns {string} kebab-cased version of `str`
*/
export function convertToKebabCase(str: string): string {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
return str
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_.]+/g, "-")
.toLowerCase();
}

/**
Expand All @@ -70,11 +73,7 @@ export function convertToKebabCase(str: string): string {
* @returns {string} kebab-cased version of `str`
*/
export function getKebabCase(str?: string): string {
return convertToKebabCase(
removeSpaces(
replaceSpecialCharacterWithSpace(replaceSpacesWithCamelCase(str ?? "")),
),
);
return convertToKebabCase(replaceSpecialCharacterWithSpace(str ?? "").trim());
zainfathoni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down