summaryrefslogtreecommitdiff
path: root/src/core/utils/address.js
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-04-11 09:47:25 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-04-11 09:47:25 +0700
commit92c2a229d9c9b510d71b928978872a8b107e9d5a (patch)
tree8d8161a49a0bdc46d4c28d3f2682bb485314a41d /src/core/utils/address.js
parent62bebc1d33fd090d7666e18e7a0326ef7ef36897 (diff)
Documentation and refactor code
Diffstat (limited to 'src/core/utils/address.js')
-rw-r--r--src/core/utils/address.js35
1 files changed, 30 insertions, 5 deletions
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 }