From 0a0c497204acbac562700d80f38e74aa9ffcd94e Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 1 Dec 2022 16:26:21 +0700 Subject: dynamic filter, dynamic pagination, detail brand, to title case --- src/helpers/slug.js | 13 +++++++++++-- src/helpers/toTitleCase.js | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/helpers/toTitleCase.js (limited to 'src/helpers') diff --git a/src/helpers/slug.js b/src/helpers/slug.js index ed1a5b6a..b1e67cdf 100644 --- a/src/helpers/slug.js +++ b/src/helpers/slug.js @@ -1,13 +1,22 @@ +import toTitleCase from './toTitleCase'; + const createSlug = (name, id) => { return name?.trim().replace(new RegExp(/[^A-Za-z0-9]/, 'g'), '-').toLowerCase() + '-' + id; } -const getId = (slug) => { +const getIdFromSlug = (slug) => { let id = slug.split('-'); return id[id.length-1]; } +const getNameFromSlug = (slug) => { + let name = slug.split('-'); + name.pop(); + return toTitleCase(name.join(' ')); +} + export { createSlug, - getId + getIdFromSlug, + getNameFromSlug }; \ No newline at end of file diff --git a/src/helpers/toTitleCase.js b/src/helpers/toTitleCase.js new file mode 100644 index 00000000..5cfd70d0 --- /dev/null +++ b/src/helpers/toTitleCase.js @@ -0,0 +1,8 @@ +export default function toTitleCase(str) { + return str.replace( + /\w\S*/g, + function(txt) { + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); + } + ); +} \ No newline at end of file -- cgit v1.2.3