Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "keyv", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Simple key/value store with support for multiple backends", | ||
@@ -34,3 +34,2 @@ "main": "src/index.js", | ||
"coveralls": "^2.13.1", | ||
"delay": "^2.0.0", | ||
"eslint-config-xo-lukechilds": "^1.0.0", | ||
@@ -37,0 +36,0 @@ "nyc": "^10.3.2", |
@@ -10,11 +10,12 @@ 'use strict'; | ||
get(key) { | ||
return Promise.resolve(this.opts.store.get(key)).then(data => { | ||
const store = this.opts.store; | ||
return Promise.resolve(store.get(key)).then(data => { | ||
if (!data) { | ||
return undefined; | ||
} | ||
if (!this.opts.store.ttlSupport && Date.now() > data.expires) { | ||
if (!store.ttlSupport && Date.now() > data.expires) { | ||
this.delete(key); | ||
return undefined; | ||
} | ||
return this.opts.store.ttlSupport ? data : data.value; | ||
return store.ttlSupport ? data : data.value; | ||
}); | ||
@@ -25,9 +26,10 @@ } | ||
ttl = ttl || this.opts.ttl; | ||
const store = this.opts.store; | ||
let set; | ||
if (this.opts.store.ttlSupport) { | ||
set = this.opts.store.set(key, value, ttl); | ||
if (store.ttlSupport) { | ||
set = store.set(key, value, ttl); | ||
} else { | ||
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : undefined; | ||
const data = { value, expires }; | ||
set = this.opts.store.set(key, data); | ||
set = store.set(key, data); | ||
} | ||
@@ -38,3 +40,4 @@ return Promise.resolve(set).then(() => value); | ||
delete(key) { | ||
return Promise.resolve(this.opts.store.delete(key)); | ||
const store = this.opts.store; | ||
return Promise.resolve(store.delete(key)); | ||
} | ||
@@ -41,0 +44,0 @@ } |
import test from 'ava'; | ||
import delay from 'delay'; | ||
import Keyv from '../'; | ||
import testStore from './helpers/test-store'; | ||
@@ -64,1 +65,4 @@ test('Keyv is a class', t => { | ||
}); | ||
const store = new Map(); | ||
testStore(store); |
8327
5
148