phpvms/resources/js/storage.js
Nabeel S 0d1f38cf85
Refactor all JS API calls #360 (#375)
* Refactor all JS API calls #360

* Remove unused imports

* Lint JS

* Fix doubled api key

* Formatting

* Added extra logging to distance lookup

* Remove the .editorconfig file in distrib

* shell check fixes

* Remove the .editorconfig file in distrib
2019-08-30 08:08:00 -04:00

77 lines
1.5 KiB
JavaScript

/**
* Simple browser storage interface
*/
export default class Storage {
constructor(name, default_value) {
this.name = name;
// Read the object from storage; if it doesn't exist, set
// it to the default value
const st = window.localStorage.getItem(this.name);
if (!st) {
console.log('Nothing found in storage, starting from default');
this.data = default_value;
} else {
console.log('Found in storage: ', st);
this.data = JSON.parse(st);
}
}
/**
* Save to local storage
*/
save() {
window.localStorage.setItem(this.name, JSON.stringify(this.data));
}
/**
* Return a list from a given key
*
* @param {String} key
*
* @returns {Array|*}
*/
getList(key) {
if (!(key in this.data)) {
return [];
}
return this.data[key];
}
/**
* Add `value` to a given `key`
*
* @param {string} key
* @param {*} value
*/
addToList(key, value) {
if (!(key in this.data)) {
this.data[key] = [];
}
const index = this.data[key].indexOf(value);
if (index === -1) {
this.data[key].push(value);
}
}
/**
* Remove `value` from the given `key`
*
* @param {String} key
* @param {*} value
*/
removeFromList(key, value) {
if (!(key in this.data)) {
return;
}
const index = this.data[key].indexOf(value);
if (index !== -1) {
this.data[key].splice(index, 1);
}
}
}