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/address.js | 35 +++++++++++++++++++++++++++++----- src/core/utils/auth.js | 20 +++++++++++++++++--- src/core/utils/cart.js | 41 +++++++++++++++++++++++++++++++++++++--- src/core/utils/currencyFormat.js | 6 ++++++ src/core/utils/getFileBase64.js | 13 +++++++++++-- src/core/utils/greeting.js | 10 ++++++++++ src/core/utils/slug.js | 24 +++++++++++++++++++++++ src/core/utils/toTitleCase.js | 9 +++++++++ 8 files changed, 145 insertions(+), 13 deletions(-) (limited to 'src/core/utils') diff --git a/src/core/utils/address.js b/src/core/utils/address.js index c545d34b..b20feba3 100644 --- a/src/core/utils/address.js +++ b/src/core/utils/address.js @@ -1,28 +1,53 @@ +/** + * Gets the address data from local storage. + * + * @returns {object} - Returns the address data as an object, or an empty object if not found. + */ const getAddress = () => { - if (typeof window !== 'undefined') { + if (typeof window !== 'undefined' && window.localStorage) { const address = localStorage.getItem('address') if (address) return JSON.parse(address) } return {} } +/** + * Sets the address data to local storage. + * + * @param {object} address - The address data to be stored as an object. + * @returns {boolean} - Returns `true` if the address data is successfully stored, `false` otherwise. + */ const setAddress = (address) => { - if (typeof window !== 'undefined') { + if (typeof window !== 'undefined' && window.localStorage) { localStorage.setItem('address', JSON.stringify(address)) + return true } - return + return false } +/** + * Gets the value of a specific key from the address data. + * + * @param {string} key - The key of the address data to be retrieved. + * @returns {*} - The value of the specified key, or false if the key does not exist. + */ const getItemAddress = (key) => { let address = getAddress() - return address[key] + return address[key] || false } +/** + * Updates the value of a specific key in the address data. + * + * @param {string} key - The key of the address data to be updated. + * @param {*} value - The new value to be set for the specified key. + * @returns {boolean} - Returns `true` + */ const updateItemAddress = (key, value) => { let address = getAddress() address[key] = value setAddress(address) - return + return true } export { getItemAddress, updateItemAddress } diff --git a/src/core/utils/auth.js b/src/core/utils/auth.js index 13e0e79d..cddff2b8 100644 --- a/src/core/utils/auth.js +++ b/src/core/utils/auth.js @@ -1,18 +1,32 @@ import { deleteCookie, getCookie, setCookie } from 'cookies-next' +/** + * Retrieves authentication data from cookie and returns it as an object. + * + * @returns {Object|boolean} - Returns the authentication data as an object if available in cookie, otherwise `false`. + */ const getAuth = () => { let auth = getCookie('auth') - if (auth) { - return JSON.parse(auth) - } + if (auth) return JSON.parse(auth) return false } +/** + * Sets the authentication data in cookie with the given user data. + * + * @param {Object} user - The user data to be set as authentication data in cookie. + * @returns {boolean} - Returns `true`. + */ const setAuth = (user) => { setCookie('auth', JSON.stringify(user)) return true } +/** + * Deletes the authentication data stored in cookie. + * + * @returns {boolean} - Returns `true`. + */ const deleteAuth = () => { deleteCookie('auth') return true diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index fd42ee4e..2bdffb1c 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -1,23 +1,51 @@ +/** + * Retrieves cart data from localStorage, if available. + * + * @returns {Object} - The cart data as an object, or an empty object if not found. + */ const getCart = () => { - if (typeof window !== 'undefined') { + if (typeof window !== 'undefined' && window.localStorage) { const cart = localStorage.getItem('cart') if (cart) return JSON.parse(cart) } return {} } +/** + * Saves cart data to localStorage, if available. + * + * @param {Object} cart - The cart data to be saved. + * @returns {boolean} - Returns `true` if cart data is saved successfully, `false` otherwise. + */ const setCart = (cart) => { - if (typeof window !== 'undefined') { + if (typeof window !== 'undefined' && window.localStorage) { localStorage.setItem('cart', JSON.stringify(cart)) + return true } - return true + return false } +/** + * Retrieves an item from the cart data based on the given product ID. + * + * @param {Object} options - The options object containing the productId. + * @param {string} options.productId - The ID of the product to be retrieved from the cart. + * @returns {Object} - Returns the item object from the cart data, or `undefined` if not found. + */ const getItemCart = ({ productId }) => { let cart = getCart() return cart[productId] } +/** + * Updates an item in the cart data with the given productId, quantity, and selected status. + * + * @param {Object} options - The options object containing the productId, quantity, and selected status. + * @param {string} options.productId - The ID of the product to be updated in the cart. + * @param {number} options.quantity - The new quantity of the product in the cart. + * @param {boolean} [options.selected=false] - The new selected status of the product in the cart. Default is `false`. + * @returns {boolean} - Returns `true`. + */ const updateItemCart = ({ productId, quantity, selected = false }) => { let cart = getCart() quantity = parseInt(quantity) @@ -26,6 +54,13 @@ const updateItemCart = ({ productId, quantity, selected = false }) => { return true } +/** + * Deletes an item from the cart data with the given productId. + * + * @param {Object} options - The options object containing the productId. + * @param {string} options.productId - The ID of the product to be deleted from the cart. + * @returns {boolean} - Returns `true`. + */ const deleteItemCart = ({ productId }) => { let cart = getCart() delete cart[productId] diff --git a/src/core/utils/currencyFormat.js b/src/core/utils/currencyFormat.js index 12b68111..d0eba4c4 100644 --- a/src/core/utils/currencyFormat.js +++ b/src/core/utils/currencyFormat.js @@ -1,3 +1,9 @@ +/** + * Formats a numeric value as a currency string in Indonesian Rupiah (IDR) format. + * + * @param {number} value - The numeric value to be formatted as currency. + * @returns {string} - The currency string in IDR format. + */ const currencyFormat = (value) => { const currency = new Intl.NumberFormat('id-ID', { style: 'currency', diff --git a/src/core/utils/getFileBase64.js b/src/core/utils/getFileBase64.js index 4fa7316b..ed6ae000 100644 --- a/src/core/utils/getFileBase64.js +++ b/src/core/utils/getFileBase64.js @@ -1,5 +1,13 @@ -const getFileBase64 = (file) => - new Promise((resolve, reject) => { +/** + * Converts a File object to base64 string using FileReader. + * + * @param {File} file - The File object to be converted. + * @returns {Promise} - A Promise that resolves with the base64 string + * representing the contents of the File, or rejects with an error if there's + * any issue with the file reading process. + */ +const getFileBase64 = (file) => { + return new Promise((resolve, reject) => { let reader = new FileReader() reader.readAsBinaryString(file) reader.onload = () => { @@ -8,5 +16,6 @@ const getFileBase64 = (file) => } reader.onerror = (error) => reject(error) }) +} export default getFileBase64 diff --git a/src/core/utils/greeting.js b/src/core/utils/greeting.js index aaaade7a..d349033e 100644 --- a/src/core/utils/greeting.js +++ b/src/core/utils/greeting.js @@ -1,3 +1,13 @@ +/** + * Generates a greeting based on the current time of day. + * The greeting is determined by the hour of the day: + * - "Selamat Pagi" for hours before 11:00 + * - "Selamat Siang" for hours between 11:00 and 15:00 + * - "Selamat Sore" for hours between 15:00 and 18:00 + * - "Selamat Malam" for hours after 18:00 + * + * @returns {string} - The generated greeting message. + */ const greeting = () => { let hours = new Date().getHours() if (hours < 11) return 'Selamat Pagi' 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() diff --git a/src/core/utils/toTitleCase.js b/src/core/utils/toTitleCase.js index 4335824d..dab1dc33 100644 --- a/src/core/utils/toTitleCase.js +++ b/src/core/utils/toTitleCase.js @@ -1,3 +1,12 @@ +/** + * Converts a string to title case. + * Title case capitalizes the first character of each word in the string, + * and sets the remaining characters to lowercase. + * + * @param {string} str - The input string to be converted to title case. + * @returns {string} - The string in title case. + */ + const toTitleCase = (str) => { return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() -- cgit v1.2.3