diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-04-11 09:47:25 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-04-11 09:47:25 +0700 |
| commit | 92c2a229d9c9b510d71b928978872a8b107e9d5a (patch) | |
| tree | 8d8161a49a0bdc46d4c28d3f2682bb485314a41d /src/core/utils/slug.js | |
| parent | 62bebc1d33fd090d7666e18e7a0326ef7ef36897 (diff) | |
Documentation and refactor code
Diffstat (limited to 'src/core/utils/slug.js')
| -rw-r--r-- | src/core/utils/slug.js | 24 |
1 files changed, 24 insertions, 0 deletions
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() |
