Created
June 24, 2021 15:18
-
-
Save farishan/9a1a091256272774858be8904e6fced0 to your computer and use it in GitHub Desktop.
Saloimex
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
/* | |
* Saloimex | |
* Faris Han, 2020 | |
*/ | |
class Saloimex { | |
/** | |
* Saloimex | |
* @constructor | |
*/ | |
constructor(key) { | |
this.key = key ? `${key.toLowerCase().split(' ').join('_')}_data` : 'saloimex_data'; | |
} | |
/** | |
* Save stringified JSON to localStorage. | |
* @param {object} data - The JSON data. | |
* @return {object} data - The passed data. | |
*/ | |
save(data) { | |
localStorage.setItem(this.key, JSON.stringify(data)); | |
return data; | |
} | |
/** | |
* Load stringified JSON from localStorage. | |
* @return {object} data - The JSON data. | |
*/ | |
load() { | |
let data = false; | |
if (localStorage.getItem(this.key)) { | |
data = JSON.parse(localStorage.getItem(this.key)); | |
} | |
return data; | |
} | |
/** | |
* Export JSON to base64. | |
* @param {data} data - The JSON data. | |
* @return {string} result - The base64 string. | |
*/ | |
export(data) { | |
this.save(data); | |
const result = btoa(JSON.stringify(data)); | |
return result; | |
} | |
/** | |
* Import base64 string to JSON. | |
* @param {string} string - The base64 string. | |
* @return {object} data - The JSON data. | |
*/ | |
import(string) { | |
const data = JSON.parse(atob(string)); | |
this.save(data); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment