Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
feat: adding storage helpers, getLocale and fix canUseDOM (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
idindrakusuma authored Jan 15, 2022
1 parent e08e24a commit cfeab91
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invitato/helpers",
"version": "0.0.4",
"version": "0.0.5",
"description": "Common helpers for Invitato website projects",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
12 changes: 1 addition & 11 deletions src/canUseDOM.ts
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;
17 changes: 17 additions & 0 deletions src/getLocale.ts
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,
};
}
21 changes: 21 additions & 0 deletions src/storage/delete.ts
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;
21 changes: 21 additions & 0 deletions src/storage/get.ts
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;
3 changes: 3 additions & 0 deletions src/storage/index.ts
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';
22 changes: 22 additions & 0 deletions src/storage/set.ts
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;

0 comments on commit cfeab91

Please sign in to comment.