Skip to content

Commit

Permalink
Merge pull request #313 from fncolon/patch-1
Browse files Browse the repository at this point in the history
fix: getKebabCase #294
  • Loading branch information
zainfathoni authored Jul 25, 2021
2 parents 703c887 + 138c2ff commit d77441d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
...require("./test/jest-common"),
collectCoverageFrom: ["./(components|etc|lib|pages)/**/*.(ts|tsx|js|jsx)"],
collectCoverageFrom: [
"./(components|etc|lib|pages)/**/*.(ts|tsx|js|jsx)",
"!./(components|etc|lib|pages)/**/__tests__/*.test.(ts|tsx|js|jsx)",
],
coverageThreshold: {
global: {
statements: 6,
Expand Down
46 changes: 36 additions & 10 deletions lib/__tests__/string-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import { getKebabCase } from "../string-utils";
import { getKebabCase, toSnakeCase, toTitleCase } from "../string-utils";

describe("toSnakeCase", () => {
it.each`
input | expected
${"Foo Bar"} | ${"foo_bar"}
${"DKI Jakarta"} | ${"dki_jakarta"}
`(
"should return '$expected' when '$input' is provided",
({ input, expected }) => {
expect(toSnakeCase(input as string)).toBe(expected);
},
);
});

describe("toTitleCase", () => {
it.each`
input | expected
${"foo bar"} | ${"Foo Bar"}
${"DKI Jakarta"} | ${"DKI Jakarta"}
`(
"should return '$expected' when '$input' is provided",
({ input, expected }) => {
expect(toTitleCase(input as string)).toBe(expected);
},
);
});

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
50 changes: 7 additions & 43 deletions lib/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,14 @@ export function toTitleCase(str: string): string {
.join(" ");
}

/**
* Convert all whitespaces in a string to camelCase
*
* @param {string} str input string
* @returns {string} camelCased and whitespace-free version of `str`
*/
export function replaceSpacesWithCamelCase(str: string): string {
return str.replace(/\s+/g, (s) => {
return s.substring(0, 1).toUpperCase() + s.substring(1);
});
}

/**
* Replace all non-alphanumeric character in a string with space
*
* @param {string} str input string
* @returns {string} `str`, without non-alphanumeric characters
*/
export function replaceSpecialCharacterWithSpace(str: string): string {
return str.replace(/[^a-zA-Z0-9. ]/g, " ");
}

/**
* Remove all whitespaces from a string
*
* @param {string} str input string
* @returns {string} whitespace-free version of `str`
*/
export function removeSpaces(str: string): string {
return str.replace(/\s+/g, "");
return str.replace(/[^a-zA-Z0-9]/g, " ");
}

/**
Expand All @@ -60,7 +38,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 +51,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());
}

/**
Expand Down Expand Up @@ -111,19 +88,6 @@ export function toSecond(hrtime: [number, number]): string {
return (hrtime[0] + hrtime[1] / 1e9).toFixed(3);
}

/**
* Build a function pipeline
*
* @param {Function[]} functions array of functions, ordered by the expected
* order of execution
* @returns {Function} chained functions
*/
export function composeFunctions(...functions: Function[]): Function {
return (args: unknown) => {
return functions.reduce((acc, fn) => fn(acc), args);
};
}

/**
* Get the last segment from a kebab-cased string
*
Expand Down Expand Up @@ -164,7 +128,7 @@ export function getQueryParams(query: string): {} {
return query
? (/^[?#]/.test(query) ? query.slice(1) : query)
.split("&")
.reduce((params: any, param) => {
.reduce((params: Record<string, string>, param) => {
const [key, value] = param.split("=");
params[key] = value
? decodeURIComponent(value.replace(/\+/g, " "))
Expand Down

0 comments on commit d77441d

Please sign in to comment.