angular-persistence
Version:
A library to handle persistence for Angular 2 applications.
86 lines • 2.19 kB
JavaScript
var /** @type {?} */ NULL_VALUE = '_____NULL_VALUE_____';
/**
* This is a container that wraps a browser storage object.
*
* @export
* \@class BrowserContainer
*
* @author Scott O'Bryan
* \@since 1.0
*/
export var BrowserContainer = (function () {
/**
* Creates an instance of BrowserContainer.
* @param {?} _storage
*/
function BrowserContainer(_storage) {
this._storage = _storage;
}
/**
* Sets a value on the browser storage
*
* @param {?} key
* @param {?} value
* @return {?}
*/
BrowserContainer.prototype.set = function (key, value) {
try {
if (value === null) {
value = NULL_VALUE;
}
if (value === undefined) {
this._storage.removeItem(key);
}
else {
this._storage.setItem(key, JSON.stringify(value));
}
}
catch (error) {
return false;
}
return true;
};
/**
* Gets a value from browser storage
*
* @param {?} key
* @return {?}
*/
BrowserContainer.prototype.get = function (key) {
var /** @type {?} */ strval = this._storage.getItem(key);
if (strval === null) {
return undefined;
}
var /** @type {?} */ value = JSON.parse(strval);
if (value === NULL_VALUE) {
return null;
}
return value;
};
/**
* Removes a value from browser storage
*
* @param {?} key
* @return {?}
*/
BrowserContainer.prototype.remove = function (key) {
var /** @type {?} */ curVal = this.get(key);
if (curVal !== undefined) {
this._storage.removeItem(key);
}
return curVal;
};
/**
* Removes all values from browser storage
* @return {?}
*/
BrowserContainer.prototype.removeAll = function () {
this._storage.clear();
};
return BrowserContainer;
}());
function BrowserContainer_tsickle_Closure_declarations() {
/** @type {?} */
BrowserContainer.prototype._storage;
}
//# sourceMappingURL=storage.browser_container.js.map