diff --git a/jest.config.js b/jest.config.js index b09118c80..6b34a50e7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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, diff --git a/lib/__tests__/string-utils.test.ts b/lib/__tests__/string-utils.test.ts index 28caba393..cd4226684 100644 --- a/lib/__tests__/string-utils.test.ts +++ b/lib/__tests__/string-utils.test.ts @@ -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 }) => { diff --git a/lib/string-utils.ts b/lib/string-utils.ts index a73d7c3a9..fd9e3eb12 100644 --- a/lib/string-utils.ts +++ b/lib/string-utils.ts @@ -21,18 +21,6 @@ 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 * @@ -40,17 +28,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, " "); -} - -/** - * 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, " "); } /** @@ -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(); } /** @@ -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()); } /** @@ -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 * @@ -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, param) => { const [key, value] = param.split("="); params[key] = value ? decodeURIComponent(value.replace(/\+/g, " "))