browser-storage-utilities
Version:
A front-end library that provides utility methods to facilitate CRUD operations to data stored in the browser, and more.
147 lines • 5.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const storage_base_operations_1 = require("./storage-base-operations");
const rxjs_1 = require("rxjs");
const _window = require('global/window');
class StorageBase extends storage_base_operations_1.StorageBaseOperations {
constructor(settings) {
super();
this._stateSubject = new rxjs_1.BehaviorSubject(null);
this._settings = settings;
this._validateSettings(settings);
}
setSettings(settings) {
this._settings = settings;
this._validateSettings(settings);
}
get(key, type, isUpdateProp) {
type && this._validateStorageType(type);
return this._getWithExpiry(key, type, isUpdateProp);
}
add(key, value, expiry, type, isUpdateProp) {
type && this._validateStorageType(type);
if (expiry) {
value = this._setWithExpiry(value, expiry, isUpdateProp);
}
else if (this._settings.setExpiryMilliseconds) {
value = this._setWithExpiry(value, this._settings.setExpiryMilliseconds, isUpdateProp);
}
if (this._settings.notifiedOfStateChanges) {
const state = {
storage: type || this._settings.type,
oldValue: this._deserializer(_window[type || this._settings.type].getItem(this._settings.keyPrefix + key)),
newValue: value
};
this._setStorageState(state);
}
_window[type || this._settings.type].setItem(this._settings.keyPrefix + key, this._serializer(value));
}
remove(key, type) {
type && this._validateStorageType(type);
if (this._settings.notifiedOfStateChanges) {
const state = {
storage: type || this._settings.type,
oldValue: this._deserializer(_window[type || this._settings.type].getItem(this._settings.keyPrefix + key)),
newValue: null
};
this._setStorageState(state);
}
_window[type || this._settings.type].removeItem(this._settings.keyPrefix + key);
}
removeAll(keys, type) {
_.forEach(keys, (key) => {
this.remove(key, type);
});
}
updateProp(key, propName, newValue, type) {
let item = this.get(key, type, true);
if (item) {
if (item.expiry) {
item.value[propName] = newValue;
this.add(key, item.value, item.expiry, type, true);
}
else {
item[propName] = newValue;
this.add(key, item, null, type, true);
}
}
return item;
}
removeProp(key, propName, type) {
let item = this.get(key, type, true);
if (item) {
if (item.expiry) {
delete item.value[propName];
this.add(key, item.value, item.expiry, type, true);
}
else {
delete item[propName];
this.add(key, item, null, type, true);
}
}
return item;
}
getStorage(type) {
type && this._validateStorageType(type);
return _window[type || this._settings.type];
}
clear(type) {
type && this._validateStorageType(type);
_window[type || this._settings.type].clear();
}
getNotifierObservable() {
return this._stateSubject.asObservable();
}
_setStorageState(state) {
this._stateSubject.next({
storage: state.storage,
oldValue: state.oldValue,
newValue: state.newValue
});
}
_deserializer(json) {
return _.isString(json) ? JSON.parse(json) : json;
}
_serializer(obj) {
if (_.isUndefined(obj))
return undefined;
return JSON.stringify(obj);
}
_setWithExpiry(value, expiry, isUpdateProp) {
const now = new Date();
return {
value,
expiry: isUpdateProp ? expiry : now.getTime() + expiry
};
}
_getWithExpiry(key, type, isUpdateProp) {
const item = this._deserializer(_window[type || this._settings.type].getItem(this._settings.keyPrefix + key));
if (!item) {
return null;
}
if (!item.expiry) {
return item;
}
const now = new Date();
if (now.getTime() > item.expiry) {
this.remove(key, type);
return null;
}
return isUpdateProp ? item : item.value;
}
_validateStorageType(type) {
if (_.isNil(_window[type])) {
throw new Error(`The storage type ${type} is not supported by the browser!`);
}
}
_validateSettings(settings) {
if (!_window.process || _.get(process, 'env.NODE_ENV') !== 'test') {
if (_.isNil(_window)) {
throw new Error('The global window object is undefined!');
}
this._validateStorageType(settings.type);
}
}
}
exports.StorageBase = StorageBase;
//# sourceMappingURL=storage-base.js.map