web-storage-manager
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -40,2 +40,19 @@ "use strict"; | ||
* | ||
* @param {string} key - data key | ||
* @param {*} value - data value | ||
* | ||
*/ | ||
exports.setEncodeItem = (key, value) => { | ||
try { | ||
const encoded = (void 0).encode(value); | ||
storage.setItem(key, encoded); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {Object[]} items - collection | ||
@@ -62,2 +79,24 @@ * @param {Object} items[].item - item object | ||
* | ||
* @param {Object[]} items - collection | ||
* @param {Object} items[].item - item object | ||
* @param {string} items[].item.key - data key | ||
* @param {*} items[].item.value - data value | ||
* | ||
*/ | ||
exports.setEncodeMultiple = items => { | ||
try { | ||
for (const i of items) { | ||
const encoded = (void 0).encode(i.value); | ||
storage.setItem(i.key, encoded); | ||
} | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {string} key - data key | ||
@@ -72,5 +111,4 @@ * @param {*} value - data value | ||
const oldData = (void 0).getItem(key); | ||
let newData = (void 0).combineObject(value, oldData); | ||
(void 0).setItem(key, newData); | ||
return true; | ||
const newData = (void 0).combineObject(value, oldData); | ||
return (void 0).setItem(key, newData); | ||
} catch (error) { | ||
@@ -82,2 +120,19 @@ return false; | ||
* | ||
* @param {string} key - data key | ||
* @param {*} value - data value | ||
* | ||
*/ | ||
exports.appendEncodeItem = (key, value) => { | ||
try { | ||
const oldData = (void 0).getEncodeItem(key); | ||
const newData = (void 0).combineObject(value, oldData); | ||
return (void 0).setEncodeItem(key, newData); | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {Object} object - object to combine | ||
@@ -199,4 +254,3 @@ * @param {Object} toObject - object to combine to | ||
objSelf.setItem(parentKey, newCollection); | ||
return true; | ||
return objSelf.setItem(parentKey, newCollection); | ||
} | ||
@@ -211,2 +265,96 @@ } | ||
* | ||
* @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.updateEncodeItemInItem = (parentKey, childKeys, value, attrCompare) => { | ||
// use only for encoded objects | ||
try { | ||
let collection = (void 0).getEncodeItem(parentKey); | ||
if (!collection) return false; // terminate process | ||
let tmpCollection = {}; // iterate through with child keys | ||
for (const _ref5 of childKeys.entries()) { | ||
var _ref6 = _slicedToArray(_ref5, 2); | ||
const idx = _ref6[0]; | ||
const key = _ref6[1]; | ||
collection = mapData(key, collection); | ||
if (idx === childKeys.length - 1) { | ||
if (!Array.isArray(collection)) { | ||
// check if type object | ||
collection = value; // replace with new value | ||
} else { | ||
// collection | ||
const idx = attrCompare ? (void 0).indexOfObject(collection, value, attrCompare) : -1; // append or replace object at index | ||
if (idx >= 0) { | ||
collection[idx] = value; | ||
} else { | ||
collection.push(value); | ||
} | ||
} // add to temp collection | ||
tmpCollection[key] = collection; | ||
mapDataUpdate(void 0, tmpCollection); | ||
} else { | ||
// add to temp collection | ||
tmpCollection[key] = collection; | ||
} | ||
} // map data get value from key path | ||
function mapData(key, collection) { | ||
return collection[key]; | ||
} // map data and update collection | ||
function mapDataUpdate(self, tmpCollection) { | ||
const objSelf = Object(self); // set self | ||
const oldCollection = objSelf.getEncodeItem(parentKey); // get old collection | ||
let newCollection = null; | ||
for (const _ref7 of childKeys.reverse().entries()) { | ||
var _ref8 = _slicedToArray(_ref7, 2); | ||
const idx = _ref8[0]; | ||
const key = _ref8[1]; | ||
// iterate from last key path first | ||
let data = tmpCollection[key]; | ||
if (!newCollection) { | ||
newCollection = { | ||
[key]: data // set initial value | ||
}; | ||
} else { | ||
// update with old data + new data | ||
newCollection = { | ||
[key]: objSelf.combineObject(newCollection, data) | ||
}; | ||
} | ||
if (idx === childKeys.length - 1) { | ||
// add modified data to the parent collection | ||
newCollection = objSelf.combineObject(newCollection, oldCollection); // save and update local | ||
return objSelf.setEncodeItem(parentKey, newCollection); | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
@@ -227,2 +375,18 @@ * | ||
* | ||
* @param {string} key - key name of your saved data | ||
* | ||
*/ | ||
exports.getEncodeItem = key => { | ||
try { | ||
const data = storage.getItem(key); | ||
const decoded = (void 0).decode(data); | ||
return decoded; | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
@@ -253,2 +417,27 @@ * | ||
* | ||
* @param {string[]} keys - key names of your saved data | ||
* | ||
*/ | ||
exports.getEncodeMultiple = keys => { | ||
try { | ||
const items = []; | ||
for (const key of keys) { | ||
const data = storage.getItem(key); | ||
const decoded = (void 0).decode(data); | ||
if (decoded) { | ||
items.push(decoded); | ||
} | ||
} | ||
return items; | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {string} key - key name of your saved data | ||
@@ -299,2 +488,26 @@ * | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {Object} obj - object to be encoded | ||
* | ||
*/ | ||
exports.encode = obj => { | ||
const rawStr = JSON.stringify(obj); | ||
const encObj = window.btoa(rawStr); | ||
return encObj; | ||
}; | ||
/** | ||
* | ||
* @param {Object} encObj - object to be decoded | ||
* | ||
*/ | ||
exports.decode = encObj => { | ||
const decoded = window.atob(encObj); | ||
const obj = JSON.parse(decoded); | ||
return obj; | ||
}; |
{ | ||
"name": "web-storage-manager", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Web utility storage manager to handle save, update and data purge", | ||
@@ -5,0 +5,0 @@ "main": "lib/web-storage-manager.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17013
5
408