web-storage-manager
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "web-storage-manager", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Web utility storage manager to handle save, update and data purge", | ||
@@ -5,0 +5,0 @@ "main": "src/web-storage-manager.js", |
@@ -5,266 +5,261 @@ 'use strict'; | ||
const Storage = { | ||
exports.storage = () => { | ||
storage | ||
} | ||
storage() { | ||
return storage | ||
}, | ||
/** | ||
* | ||
* @param {string} key - data key | ||
* @param {string} value - data value | ||
* | ||
*/ | ||
exports.setItem = (key, value) => { | ||
/** | ||
* | ||
* @param {string} key - data key | ||
* @param {string} value - data value | ||
* | ||
*/ | ||
setItem(key, value) { | ||
try { | ||
storage.setItem(key, JSON.stringify(value)) | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
try { | ||
storage.setItem(key, JSON.stringify(value)) | ||
return true | ||
} catch (error) { | ||
return false | ||
/** | ||
* | ||
* @param {Object[]} items - collection | ||
* @param {Object} items[].item - item object | ||
* @param {string} items[].item.key - data key | ||
* @param {string} items[].item.value - data value | ||
* | ||
*/ | ||
exports.setMultiple = (items) => { | ||
try { | ||
for (const i of items) { | ||
storage.setItem(i.key, JSON.stringify(i.value)) | ||
} | ||
}, | ||
/** | ||
* | ||
* @param {Object[]} items - collection | ||
* @param {Object} items[].item - item object | ||
* @param {string} items[].item.key - data key | ||
* @param {string} items[].item.value - data value | ||
* | ||
*/ | ||
setMultiple(items) { | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
try { | ||
for (const i of items) { | ||
storage.setItem(i.key, JSON.stringify(i.value)) | ||
} | ||
/** | ||
* | ||
* @param {string} key - data key | ||
* @param {string} value - data value | ||
* | ||
*/ | ||
exports.appendItem = (key, value) => { | ||
return true | ||
} catch (error) { | ||
return false | ||
try { | ||
const oldData = this.getItem(key) | ||
const newData = { | ||
...oldData, | ||
...value | ||
} | ||
}, | ||
/** | ||
* | ||
* @param {string} key - data key | ||
* @param {string} value - data value | ||
* | ||
*/ | ||
appendItem(key, value) { | ||
this.setItem(key, newData) | ||
try { | ||
const oldData = this.getItem(key) | ||
const newData = { | ||
...oldData, | ||
...value | ||
} | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
this.setItem(key, newData) | ||
/** | ||
* | ||
* @param {string} parentKey - parent data key | ||
* @param {string[]} childKeys - data keys - key path | ||
* @param {string} value - data value | ||
* @param {string} value - data attrib compare | ||
* | ||
*/ | ||
exports.updateItemInItem = (parentKey, childKeys, value, attrCompare) => { | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
}, | ||
try { | ||
let collection = this.getItem(parentKey) | ||
if (!collection) return false // terminate process | ||
/** | ||
* | ||
* @param {string} parentKey - parent data key | ||
* @param {string[]} childKeys - data keys - key path | ||
* @param {string} value - data value | ||
* @param {string} value - data attrib compare | ||
* | ||
*/ | ||
updateItemInItem(parentKey, childKeys, value, attrCompare) { | ||
let tmpCollection = {} | ||
try { | ||
let collection = this.getItem(parentKey) | ||
if (!collection) return false // terminate process | ||
// iterate through with child keys | ||
for (const [idx, key] of childKeys.entries()) { | ||
collection = mapData(key, collection) | ||
let tmpCollection = {} | ||
if (idx === childKeys.length - 1) { | ||
// iterate through with child keys | ||
for (const [idx, key] of childKeys.entries()) { | ||
collection = mapData(key, collection) | ||
if (!Array.isArray(collection)) { // check if type object | ||
collection = value // replace with new value | ||
}else{ | ||
// collection | ||
const idx = indexOfObject(collection, value, attrCompare) | ||
if (idx === childKeys.length - 1) { | ||
if (!Array.isArray(collection)) { // check if type object | ||
collection = value // replace with new value | ||
// append or replace object at index | ||
if (idx >= 0) { | ||
collection[idx] = value | ||
}else{ | ||
// collection | ||
const idx = indexOfObject(collection, value, attrCompare) | ||
// append or replace object at index | ||
if (idx >= 0) { | ||
collection[idx] = value | ||
}else{ | ||
collection.push(value) | ||
} | ||
collection.push(value) | ||
} | ||
} | ||
// add to temp collection | ||
tmpCollection[key] = collection | ||
mapDataUpdate(this, tmpCollection) | ||
}else{ | ||
// add to temp collection | ||
tmpCollection[key] = collection | ||
} | ||
// add to temp collection | ||
tmpCollection[key] = collection | ||
mapDataUpdate(this, tmpCollection) | ||
}else{ | ||
// add to temp collection | ||
tmpCollection[key] = collection | ||
} | ||
} | ||
// map data get value from key path | ||
function mapData(key, collection) { | ||
// map data get value from key path | ||
function mapData(key, collection) { | ||
return collection[key] | ||
} | ||
return collection[key] | ||
} | ||
// map data and update collection | ||
function mapDataUpdate(self, tmpCollection) { | ||
// map data and update collection | ||
function mapDataUpdate(self, tmpCollection) { | ||
const objSelf = Object(self) // set self | ||
const oldCollection = objSelf.getItem(parentKey) // get old collection | ||
let newCollection = null | ||
const objSelf = Object(self) // set self | ||
const oldCollection = objSelf.getItem(parentKey) // get old collection | ||
let newCollection = null | ||
for (const [idx, key] of childKeys.reverse().entries()) { // iterate from last key path first | ||
for (const [idx, key] of childKeys.reverse().entries()) { // iterate from last key path first | ||
let data = tmpCollection[key] | ||
let data = tmpCollection[key] | ||
if (!newCollection) { | ||
newCollection = { [key]: data } // set initial value | ||
}else{ | ||
if (!newCollection) { | ||
newCollection = { [key]: data } // set initial value | ||
}else{ | ||
// update with old data + new data | ||
newCollection = { | ||
[key]: { | ||
...data, | ||
...newCollection | ||
} | ||
// update with old data + new data | ||
newCollection = { | ||
[key]: { | ||
...data, | ||
...newCollection | ||
} | ||
} | ||
} | ||
if (idx === childKeys.length - 1) { | ||
// add modified data to the parent collection | ||
newCollection = { | ||
...oldCollection, | ||
...newCollection, | ||
} | ||
if (idx === childKeys.length - 1) { | ||
// add modified data to the parent collection | ||
newCollection = { | ||
...oldCollection, | ||
...newCollection, | ||
} | ||
// save and update local | ||
objSelf.setItem(parentKey, newCollection) | ||
// save and update local | ||
objSelf.setItem(parentKey, newCollection) | ||
return true | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
// Collection ~ Ojbect ~ attribute | ||
function indexOfObject(collection, object, attr) { | ||
// Collection ~ Ojbect ~ attribute | ||
function indexOfObject(collection, object, attr) { | ||
for (let i = 0; i < collection.length; i++) { | ||
if (collection[i][attr] === object[attr]) { | ||
return i | ||
} | ||
for (let i = 0; i < collection.length; i++) { | ||
if (collection[i][attr] === object[attr]) { | ||
return i | ||
} | ||
return -1 | ||
} | ||
} catch (error) { | ||
return false | ||
return -1 | ||
} | ||
}, | ||
} catch (error) { | ||
return false | ||
} | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
* | ||
*/ | ||
getItem(key) { | ||
} | ||
try { | ||
const data = storage.getItem(key) | ||
return JSON.parse(data) | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
* | ||
*/ | ||
exports.getItem = (key) => { | ||
} catch (error) { | ||
return null | ||
} | ||
}, | ||
try { | ||
const data = storage.getItem(key) | ||
return JSON.parse(data) | ||
/** | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
* | ||
*/ | ||
getMultiple(keys) { | ||
} catch (error) { | ||
return null | ||
} | ||
} | ||
try { | ||
const items = [] | ||
/** | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
* | ||
*/ | ||
exports.getMultiple = (keys) => { | ||
for (const key of keys) { | ||
const data = storage.getItem(key) | ||
const item = JSON.parse(data) | ||
try { | ||
const items = [] | ||
if (item) { | ||
items.push(item) | ||
} | ||
for (const key of keys) { | ||
const data = storage.getItem(key) | ||
const item = JSON.parse(data) | ||
if (item) { | ||
items.push(item) | ||
} | ||
return items | ||
} catch (error) { | ||
return null | ||
} | ||
}, | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
* | ||
*/ | ||
removeItem(key) { | ||
return items | ||
} catch (error) { | ||
return null | ||
} | ||
} | ||
try { | ||
storage.removeItem(key) | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
}, | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
* | ||
*/ | ||
removeItem = (key) => { | ||
/** | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
* | ||
*/ | ||
removeMultiple(keys) { | ||
try { | ||
storage.removeItem(key) | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
try { | ||
for (const key of keys) { | ||
storage.removeItem(key) | ||
} | ||
/** | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
* | ||
*/ | ||
removeMultiple = (keys) => { | ||
return true | ||
} catch (error) { | ||
return false | ||
try { | ||
for (const key of keys) { | ||
storage.removeItem(key) | ||
} | ||
}, | ||
/** | ||
* | ||
* will purge all saved data under this domain | ||
* | ||
*/ | ||
purge() { | ||
try { | ||
storage.clear() | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
module.exports = Storage; | ||
/** | ||
* | ||
* will purge all saved data under this domain | ||
* | ||
*/ | ||
purge = () => { | ||
try { | ||
storage.clear() | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} |
11255
213