mstorage
v1.3.4
Published
special data storages
Downloads
2
Maintainers
Readme
mstorage
npm i -S mstorage
Special serializer and deserializer for mstorage
: mstorage-restore
import { KV } from 'mstorage'
import assert from 'assert'
var kv = new KV()
var key = { a: 'a' }
var val = [ 1, 2 ]
kv.set(key, val)
console.log(kv.has(key))
console.log(kv.hasByValue(val))
console.log(kv.get(key))
console.log(kv.getByValue(val))
import { Queue } from 'mstorage'
import assert from 'assert'
var q = new Queue()
var id1 = q.add({ 1: 1 })
var id2 = q.add({ 2: 2 })
q.toTail(id1)
var next = q.next()
assert.deepEqual(next, { 2: 2 })
import { HashVault } from 'mstorage'
import assert from 'assert'
var array = []
for (var index = 0; index !== 1e6; index++)
array.push({ 1: index })
var hv = new HashVault(array)
assert(hv.exists({ 1: 100 }))
assert(hv.exists({ 1: 10000 }))
assert(hv.exists({ 1: 10000000 }))
Methods
KV
drop
copy
dump
&restore
keys
values
has
hasByValue
alias forexists
get
getByValue
alias fotid
set
alias foradd
del
delByValue
size
Queue
drop
copy
dump
&restore
each
alias forforEach
map
add
replace
del
delByValue
has
hasByValue
alias forexists
get
getByValue
alias forid
all
shuffle
next
alias forshift
last
alias forpop
toTail
toHead
size
HashVault
init
drop
copy
alias forrestore
dump
&restore
getByValue
alias forid
add
del
delByValue
has
hasByValue
alias forexists
size
KV
Key-Value storage
drop
- erase storage [🛨]
var kv = new KV()
kv.set('a', 1)
kv.drop()
assert.equal(kv.size(), 0)
copy
[🛨]
var kv1 = new KV()
var kv2 = new KV()
kv1.set('a', 1)
kv2.copy(kv1)
assert.deepEqual(kv1.keys(), kv2.keys())
dump
& restore
[🛨]
var kv1 = new KV()
var kv2 = new KV()
kv1.set('a', 1)
var str = JSON.stringify(kv1.dump())
// or just call JSON.stringify(kv1)
kv2.restore(JSON.parse(str))
// WARNING! all null keys and values will be replaced to undefined
assert.deepEqual(kv1.keys(), kv2.keys())
keys
- return list of keys [🛨]
var kv = new KV()
kv.set('a', 1)
assert.deepEqual(kv.keys(), [ 'a' ])
values
- return list of values [🛨]
var kv = new KV()
kv.set('a', 1)
assert.deepEqual(kv.values(), [ 1 ])
has
[🛨]
var kv = new KV()
kv.set('a', 1)
assert(kv.has('a'))
hasByValue
alias for exists
[🛨]
var kv = new KV()
kv.set('a', 1)
assert(kv.hasByValue(1))
get
[🛨]
var kv = new KV()
kv.set('a', 1)
assert.equal(kv.get('a'), 1)
getByValue
alias for id
[🛨]
var kv = new KV()
kv.set('a', 1)
assert.equal(kv.getByValue(1), 'a')
set
alias for add
[🛨]
var kv = new KV()
kv.set('a', 1)
assert.equal(kv.get('a'), 1)
del
[🛨]
var kv = new KV()
kv.set('a', 1)
kv.del('a')
assert.equal(kv.size(), 0)
delByValue
[🛨]
var kv = new KV()
kv.set('a', 1)
kv.delByValue(1)
assert.equal(kv.size(), 0)
size
[🛨]
var kv = new KV()
kv.set('a', 1)
assert.equal(kv.size(), 1)
Queue
drop
[🛨]
var q = new Queue()
q.add(1)
q.drop()
assert.equal(q.size(), 0)
copy
[🛨]
var q1 = new Queue()
var q2 = new Queue()
q1.add(1)
q2.copy(q1)
assert.deepEqual(q1.all(), q2.all())
dump
& restore
[🛨]
var q1 = new Queue()
var q2 = new Queue()
q1.add(1)
var str = JSON.stringify(q1.dump())
// or just call JSON.stringify(q1)
q2.restore(JSON.parse(str))
// WARNING! all null values will be replaced to undefined
assert.deepEqual(q1.all(), q2.all())
each
alias for forEach
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
q.each(console.log)
map
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
q.map(el => el+1)
assert.deepEqual(q.all(), [ 2, 3 ])
add
[🛨]
var q = new Queue()
q.add(1)
assert.equal(q.size(), 1)
replace
[🛨]
var q = new Queue()
var id1 = q.add(1)
q.replace(id1, 2)
assert.equal(q.get(id1), 2)
del
[🛨]
var q = new Queue()
var id = q.add(1)
q.del(id)
assert.equal(q.size(), 0)
delByValue
[🛨]
var q = new Queue()
q.add(1)
q.delByValue(1)
assert.equal(q.size(), 0)
has
[🛨]
var q = new Queue()
var id = q.add(1)
assert(q.has(id))
hasByValue
alias for exists
[🛨]
var q = new Queue()
q.add(1)
assert(q.hasByValue(1))
get
[🛨]
var q = new Queue()
var id = q.add(1)
assert.equal(q.get(id), 1)
getByValue
alias for id
[🛨]
var q = new Queue()
var id = q.add(1)
assert.equal(q.getByValue(1), id)
all
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
assert.deepEqual(q.all(), [ 1, 2 ])
shuffle
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
console.log(q.shuffle())
next
alias for shift
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
assert.equal(q.next(), 1)
assert.equal(q.next(), 2)
assert.equal(q.size(), 0)
last
alias for pop
[🛨]
var q = new Queue()
q.add(1)
q.add(2)
assert.equal(q.last(), 2)
assert.equal(q.last(), 1)
assert.equal(q.size(), 0)
toTail
[🛨]
var q = new Queue()
var id1 = q.add(1)
var id2 = q.add(2)
q.toTail(id1)
assert.equal(q.next(), 2)
toHead
[🛨]
var q = new Queue()
var id1 = q.add(1)
var id2 = q.add(2)
q.toHead(id2)
assert.equal(q.next(), 2)
size
[🛨]
var q = new Queue()
q.add(1)
assert.equal(q.size(), 1)
HashVault
storage of sorted hashes
init
[🛨]
var hv = new HashVault()
var array = [ 1, 2, 3, 4, 5 ]
hv.init(array)
assert.equal(hv.size(), array.length)
drop
[🛨]
var hv = new HashVault()
hv.add(1)
hv.drop()
assert.equal(hv.size(), 0)
copy
alias for restore
[🛨]
var hv1 = new HashVault()
var hv2 = new HashVault()
hv1.add(1)
hv1.add(2)
hv2.copy(hv1)
assert.equal(hv1.size(), hv2.size())
dump
& restore
[🛨]
var hv1 = new HashVault()
var hv2 = new HashVault()
hv1.add(1)
hv1.add(2)
var str = JSON.stringify(hv1.dump())
// or just call JSON.stringify(hv1)
hv2.copy(JSON.parse(str))
assert.ok(hv2.exists(1))
assert.ok(hv2.exists(2))
getByValue
alias for id
[🛨]
var hv = new HashVault()
var id = hv.add(1)
assert.equal(hv.getByValue(1), id)
add
[🛨]
var hv = new HashVault()
hv.add(1)
assert.equal(hv.size(), 1)
del
[🛨]
var hv = new HashVault()
var id = hv.add(1)
hv.del(id)
assert.equal(hv.size(), 0)
delByValue
[🛨]
var hv = new HashVault()
hv.add(1)
hv.delByValue(1)
assert.equal(hv.size(), 0)
has
[🛨]
var hv = new HashVault()
var id = hv.add(1)
assert(hv.has(id))
hasByValue
alias for exists
[🛨]
var hv = new HashVault()
hv.add(1)
assert(hv.hasByValue(1))
size
[🛨]
var hv = new HashVault()
hv.add(1)
assert.equal(hv.size(), 1)