This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding storage helpers, getLocale and fix canUseDOM (#2)
- Loading branch information
1 parent
e08e24a
commit cfeab91
Showing
7 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
/** | ||
* Function to check is can use the DOM or not | ||
* @returns {boolean} | ||
*/ | ||
function canUseDOM() { | ||
if (typeof window !== 'undefined' && window.document && window.document.createElement) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
const canUseDOM = typeof window !== 'undefined' && window.document && window.document.createElement; | ||
|
||
export default canUseDOM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
interface LocaleResult { | ||
id: string; | ||
en: string; | ||
} | ||
|
||
/** | ||
* Function to get locale text by Id or En | ||
* @param {string} id | ||
* @param {string} en | ||
* @returns {LocaleResult} | ||
*/ | ||
export default function getLocale(id: string, en: string): LocaleResult { | ||
return { | ||
id, | ||
en, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import canUseDOM from '../canUseDOM'; | ||
|
||
/** | ||
* Function to remove item in localstorage | ||
* @param {string} KEY | ||
* @returns {boolean} | ||
*/ | ||
function deleteItem(KEY: string): boolean { | ||
try { | ||
if (canUseDOM) { | ||
localStorage.removeItem(KEY); | ||
return true; | ||
} | ||
|
||
return false; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export default deleteItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import canUseDOM from '../canUseDOM'; | ||
|
||
/** | ||
* Function to get value to localstorage | ||
* @param {string} KEY | ||
* @returns {any | undefined} | ||
*/ | ||
function getItem(KEY: string): any | undefined { | ||
try { | ||
if (canUseDOM) { | ||
const result = localStorage.getItem(KEY); | ||
return result; | ||
} | ||
|
||
return undefined; | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
export default getItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * as set from './set'; | ||
export * as delete from './delete'; | ||
export * as get from './get'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import canUseDOM from '../canUseDOM'; | ||
|
||
/** | ||
* Function to set value to localstorage | ||
* @param {string} KEY | ||
* @param {string} value | ||
* @returns {boolean} | ||
*/ | ||
function setItem(KEY: string, value: any): boolean { | ||
try { | ||
if (canUseDOM) { | ||
localStorage.setItem(KEY, value); | ||
return true; | ||
} | ||
|
||
return false; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export default setItem; |