From 92c2a229d9c9b510d71b928978872a8b107e9d5a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 11 Apr 2023 09:47:25 +0700 Subject: Documentation and refactor code --- src/core/utils/slug.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/core/utils/slug.js') diff --git a/src/core/utils/slug.js b/src/core/utils/slug.js index 7010008a..d5eecd3e 100644 --- a/src/core/utils/slug.js +++ b/src/core/utils/slug.js @@ -1,5 +1,14 @@ import toTitleCase from './toTitleCase' +/** + * Creates a slug from input parameters by converting the name and appending it with an ID. + * The slug is generated by removing special characters, converting to lowercase, and joining with hyphens. + * + * @param {string} prefix - The prefix to be added to the generated slug. + * @param {string} name - The name used to generate the slug. + * @param {number} id - The ID to be appended to the slug. + * @returns {string} - The generated slug with the prefix, name, and ID. + */ const createSlug = (prefix, name, id) => { let slug = name @@ -13,11 +22,26 @@ const createSlug = (prefix, name, id) => { return prefix + filterSlugFromEmptyChar.join('-') } +/** + * Extracts the ID from a slug. + * The ID is retrieved from the last segment of the slug, separated by hyphens. + * + * @param {string} slug - The slug from which to extract the ID. + * @returns {string} - The extracted ID from the slug. + */ const getIdFromSlug = (slug) => { let id = slug.split('-') return id[id.length - 1] } +/** + * Extracts the name from a slug. + * The name is retrieved from all segments of the slug, except for the last one, separated by hyphens. + * The retrieved name is then converted to title case. + * + * @param {string} slug - The slug from which to extract the name. + * @returns {string} - The extracted name from the slug in title case. + */ const getNameFromSlug = (slug) => { let name = slug.split('-') name.pop() -- cgit v1.2.3