js-storage-manager
Advanced tools
Comparing version 0.0.16 to 0.0.17
# Changelog | ||
## v0.0.17 (18/03/2019) | ||
#### New: | ||
* Add a queue management class into its own object (See #1) | ||
--- | ||
## v0.0.16 (17/03/2019) | ||
#### New: | ||
* Adding "how to" items in the description | ||
--- | ||
## v0.0.15 (17/03/2019) | ||
@@ -4,0 +17,0 @@ #### New: |
@@ -297,5 +297,59 @@ var StorageManager = | ||
*/ | ||
initQueue (key = 'queue') { | ||
let values = this.get(key, []) | ||
initQueue (key = 'queue', reset = false) { | ||
return new QueueManager(this, key, reset) | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
class QueueManager { | ||
constructor (sm, key = 'queue', reset = false) { | ||
this.sm = sm | ||
this.key = key | ||
this.init(reset) | ||
} | ||
/** | ||
* Initialize the queue. | ||
*/ | ||
init (reset = false) { | ||
if (reset) { | ||
this.sm.set(this.key, []) | ||
} | ||
let values = this.sm.get(this.key, []) | ||
if (!Array.isArray(values)) { | ||
@@ -306,3 +360,3 @@ console.error('initQueue: The given values must be an array!') | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -315,6 +369,6 @@ | ||
*/ | ||
getQueue (key = 'queue') { | ||
this.initQueue(key) | ||
getAll () { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -334,6 +388,6 @@ if (!Array.isArray(values)) { | ||
*/ | ||
pushQueue (value, key = 'queue') { | ||
this.initQueue(key) | ||
push (value) { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -347,3 +401,3 @@ if (!Array.isArray(values)) { | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -357,4 +411,4 @@ | ||
*/ | ||
getNumberOfQueuesItems (key = 'queue') { | ||
return this.getQueue(key).length | ||
getNumber () { | ||
return this.getAll().length | ||
} | ||
@@ -368,6 +422,6 @@ | ||
*/ | ||
getNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
getNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -378,3 +432,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -392,6 +446,6 @@ } | ||
*/ | ||
deleteNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
deleteNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(key, []) | ||
@@ -402,3 +456,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -409,36 +463,2 @@ } | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
@@ -449,3 +469,2 @@ | ||
/***/ }) | ||
@@ -452,0 +471,0 @@ |
@@ -297,5 +297,59 @@ var StorageManager = | ||
*/ | ||
initQueue (key = 'queue') { | ||
let values = this.get(key, []) | ||
initQueue (key = 'queue', reset = false) { | ||
return new QueueManager(this, key, reset) | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
class QueueManager { | ||
constructor (sm, key = 'queue', reset = false) { | ||
this.sm = sm | ||
this.key = key | ||
this.init(reset) | ||
} | ||
/** | ||
* Initialize the queue. | ||
*/ | ||
init (reset = false) { | ||
if (reset) { | ||
this.sm.set(this.key, []) | ||
} | ||
let values = this.sm.get(this.key, []) | ||
if (!Array.isArray(values)) { | ||
@@ -306,3 +360,3 @@ console.error('initQueue: The given values must be an array!') | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -315,6 +369,6 @@ | ||
*/ | ||
getQueue (key = 'queue') { | ||
this.initQueue(key) | ||
getAll () { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -334,6 +388,6 @@ if (!Array.isArray(values)) { | ||
*/ | ||
pushQueue (value, key = 'queue') { | ||
this.initQueue(key) | ||
push (value) { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -347,3 +401,3 @@ if (!Array.isArray(values)) { | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -357,4 +411,4 @@ | ||
*/ | ||
getNumberOfQueuesItems (key = 'queue') { | ||
return this.getQueue(key).length | ||
getNumber () { | ||
return this.getAll().length | ||
} | ||
@@ -368,6 +422,6 @@ | ||
*/ | ||
getNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
getNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -378,3 +432,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -392,6 +446,6 @@ } | ||
*/ | ||
deleteNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
deleteNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(key, []) | ||
@@ -402,3 +456,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -409,36 +463,2 @@ } | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
@@ -449,3 +469,2 @@ | ||
/***/ }) | ||
@@ -452,0 +471,0 @@ |
{ | ||
"name": "js-storage-manager", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "A library to store data within the web storage.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -289,3 +289,3 @@ # Javascript Storage Manager | ||
### How the StorageManager saves changes to the storage itself | ||
### How to let the StorageManager automatically save changes to the storage data object in web storage | ||
@@ -292,0 +292,0 @@ ```javascript |
const storage_manager = require("./storage-manager"); | ||
module.exports = storage_manager | ||
@@ -200,5 +200,59 @@ /** | ||
*/ | ||
initQueue (key = 'queue') { | ||
let values = this.get(key, []) | ||
initQueue (key = 'queue', reset = false) { | ||
return new QueueManager(this, key, reset) | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
class QueueManager { | ||
constructor (sm, key = 'queue', reset = false) { | ||
this.sm = sm | ||
this.key = key | ||
this.init(reset) | ||
} | ||
/** | ||
* Initialize the queue. | ||
*/ | ||
init (reset = false) { | ||
if (reset) { | ||
this.sm.set(this.key, []) | ||
} | ||
let values = this.sm.get(this.key, []) | ||
if (!Array.isArray(values)) { | ||
@@ -209,3 +263,3 @@ console.error('initQueue: The given values must be an array!') | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -218,6 +272,6 @@ | ||
*/ | ||
getQueue (key = 'queue') { | ||
this.initQueue(key) | ||
getAll () { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -237,6 +291,6 @@ if (!Array.isArray(values)) { | ||
*/ | ||
pushQueue (value, key = 'queue') { | ||
this.initQueue(key) | ||
push (value) { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -250,3 +304,3 @@ if (!Array.isArray(values)) { | ||
this.set(key, values) | ||
this.sm.set(this.key, values) | ||
} | ||
@@ -260,4 +314,4 @@ | ||
*/ | ||
getNumberOfQueuesItems (key = 'queue') { | ||
return this.getQueue(key).length | ||
getNumber () { | ||
return this.getAll().length | ||
} | ||
@@ -271,6 +325,6 @@ | ||
*/ | ||
getNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
getNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(this.key, []) | ||
@@ -281,3 +335,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -295,6 +349,6 @@ } | ||
*/ | ||
deleteNextQueueItem (key = 'queue') { | ||
this.initQueue(key) | ||
deleteNext (key = 'queue') { | ||
this.init() | ||
let values = this.get(key, []) | ||
let values = this.sm.get(key, []) | ||
@@ -305,3 +359,3 @@ if (!Array.isArray(values)) { | ||
if (this.getNumberOfQueuesItems(key) <= 0) { | ||
if (this.getNumber() <= 0) { | ||
return null | ||
@@ -312,39 +366,4 @@ } | ||
} | ||
/** | ||
* Adds magic getters and setter methods to the given object to be able to react to changes with a | ||
* callback function. | ||
* | ||
* @param obj | ||
* @param callback | ||
*/ | ||
observe (obj, callback) { | ||
function buildProxy (prefix, obj) { | ||
let changeHandler = { | ||
get: (target, property) => { | ||
const out = target[property] | ||
if (out instanceof Object) { | ||
return buildProxy(prefix + property + '.', out) | ||
} | ||
return out | ||
}, | ||
set: (target, property, value) => { | ||
callback(prefix + property, value) | ||
target[property] = value | ||
return true | ||
} | ||
} | ||
return new Proxy(obj, changeHandler) | ||
} | ||
return buildProxy('', obj) | ||
} | ||
} | ||
module.exports = StorageManager | ||
@@ -138,7 +138,8 @@ /** | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data) | ||
/* Assert */ | ||
assert.deepStrictEqual(s.getLocalStorage(), localStorageExpected) | ||
assert.deepStrictEqual(sm.getLocalStorage(), localStorageExpected) | ||
}) | ||
@@ -153,8 +154,9 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data) | ||
s.pushQueue(data) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data) | ||
qm.push(data) | ||
/* Assert */ | ||
assert.deepStrictEqual(s.getLocalStorage(), localStorageExpected) | ||
assert.deepStrictEqual(sm.getLocalStorage(), localStorageExpected) | ||
}) | ||
@@ -168,9 +170,10 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data) | ||
s.pushQueue(data) | ||
s.pushQueue(data) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data) | ||
qm.push(data) | ||
qm.push(data) | ||
/* Assert */ | ||
assert.equal(s.getNumberOfQueuesItems(), numberExpected) | ||
assert.equal(qm.getNumber(), numberExpected) | ||
}) | ||
@@ -184,8 +187,9 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data) | ||
s.pushQueue(data) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data) | ||
qm.push(data) | ||
/* Assert */ | ||
assert.deepStrictEqual(s.getQueue(), queueExpected) | ||
assert.deepStrictEqual(qm.getAll(), queueExpected) | ||
}) | ||
@@ -200,9 +204,10 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data1) | ||
s.pushQueue(data2) | ||
s.pushQueue(data3) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data1) | ||
qm.push(data2) | ||
qm.push(data3) | ||
/* Assert */ | ||
assert.deepStrictEqual(s.getNextQueueItem(), data1) | ||
assert.deepStrictEqual(qm.getNext(), data1) | ||
}) | ||
@@ -217,9 +222,10 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data1) | ||
s.pushQueue(data2) | ||
s.pushQueue(data3) | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data1) | ||
qm.push(data2) | ||
qm.push(data3) | ||
/* Assert */ | ||
assert.deepStrictEqual(s.deleteNextQueueItem(), data1) | ||
assert.deepStrictEqual(qm.deleteNext(), data1) | ||
}) | ||
@@ -235,10 +241,11 @@ | ||
/* Act */ | ||
let s = new StorageManager(STORAGE_NAMESPACE) | ||
s.pushQueue(data1) | ||
s.pushQueue(data2) | ||
s.pushQueue(data3) | ||
s.deleteNextQueueItem() | ||
let sm = new StorageManager(STORAGE_NAMESPACE) | ||
let qm = sm.initQueue() | ||
qm.push(data1) | ||
qm.push(data2) | ||
qm.push(data3) | ||
qm.deleteNext() | ||
/* Assert */ | ||
assert.deepStrictEqual(s.getQueue(), queueExpected) | ||
assert.deepStrictEqual(qm.getAll(), queueExpected) | ||
}) | ||
@@ -245,0 +252,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
77758
1366