Skip to content

Commit 91519f6

Browse files
Blackheart340TimeRaider
authored andcommitted
feat: add drop method
1 parent 3f51d2e commit 91519f6

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"cz-conventional-changelog": "^1.2.0",
6666
"eslint-config-unity": "^1.0.1",
6767
"husky": "^0.11.9",
68+
"mock-webstorage": "^1.0.3",
6869
"nyc": "^8.4.0",
6970
"rimraf": "^2.5.4",
7071
"sinon": "^1.17.6",
@@ -81,6 +82,7 @@
8182
"failFast": false,
8283
"tap": false,
8384
"require": [
85+
"./test/setup.js",
8486
"babel-register",
8587
"babel-polyfill"
8688
],

src/factory.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ export default function factory(localforage) {
3535
}
3636

3737
async function get(bin, key, validate = true) {
38-
const isValid = validate && cacheBins[bin] ? await cacheBins[EXPIRE_BIN].getItem(getExpireKey(bin, key)) > Date.now() : true;
38+
const expired = await cacheBins[EXPIRE_BIN].getItem(getExpireKey(bin, key));
39+
const isValid = validate && cacheBins[bin] ? expired > Date.now() : true;
40+
3941
if (!isValid) {
4042
await cacheBins[EXPIRE_BIN].removeItem(getExpireKey(bin, key));
4143
}
44+
4245
return isValid ? await cacheBins[bin].getItem(key) : null; // localForage return null if item doesn't exist
4346
}
4447

@@ -62,10 +65,21 @@ export default function factory(localforage) {
6265
return {
6366
get,
6467
set,
65-
remove
68+
remove,
69+
drop
6670
};
6771
}
6872

73+
function drop(bins) {
74+
75+
if (!Array.isArray(bins)) {
76+
bins = [ bins ];
77+
}
78+
79+
bins.map(bin => {
80+
return cacheBins[bin].clear();
81+
});
82+
}
83+
6984
return createCache;
7085
}
71-

test/cache.spec.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
import test from 'ava';
2-
import factory from '../src/factory'
2+
import createCache from '../src/'
33
import UnityCacheError from '../src/error';
4-
import localforageStub from './localforage.stub';
54

65
test.beforeEach(t => {
7-
t.context.cache = factory(localforageStub)(['store', 'store2']);
6+
t.context.cache = createCache(['store', 'store2', 'drop_store']);
87
});
98

109
test('set/get val with default expiration period', async t => {
11-
await t.context.cache.set('store', 'key', 'val');
12-
const cachedVal = await t.context.cache.get('store', 'key');
10+
await t.context.cache.set('store', 'key-not-expired', 'val');
11+
const cachedVal = await t.context.cache.get('store', 'key-not-expired');
1312
t.is(cachedVal, 'val');
1413
});
1514

1615
test('set/get val with set expiration period', async t => {
17-
await t.context.cache.set('store', 'key', 'val', 1000);
18-
const cachedVal = await t.context.cache.get('store', 'key');
19-
t.is(cachedVal, 'val');
16+
await t.context.cache.set('store', 'key2', 'val2', 1000);
17+
const cachedVal = await t.context.cache.get('store', 'key2');
18+
t.is(cachedVal, 'val2');
2019
});
2120

2221
test('set/get expired val', async t => {
23-
await t.context.cache.set('store', 'key', 'val', 0);
24-
const cachedVal = await t.context.cache.get('store', 'key');
22+
await t.context.cache.set('store', 'key-expired', 'val', 0);
23+
const cachedVal = await t.context.cache.get('store', 'key-expired');
24+
2525
t.is(cachedVal, null);
2626
});
2727

2828
test('set/get expired val without validation', async t => {
29-
await t.context.cache.set('store', 'key', 'val');
30-
const cachedVal = await t.context.cache.get('store', 'key', false);
29+
await t.context.cache.set('store', 'key-expired-2', 'val', 0);
30+
const cachedVal = await t.context.cache.get('store', 'key-expired-2', false);
3131
t.is(cachedVal, 'val');
3232
});
3333

3434
test('get non-existent val', async t => {
35-
const cachedVal = await t.context.cache.get('store', 'key');
35+
const cachedVal = await t.context.cache.get('store', 'key-non-exist');
3636
t.is(cachedVal, null);
3737
});
3838

@@ -50,17 +50,21 @@ test('remove val', async t => {
5050
});
5151

5252
test('remove non-existent key', async t => {
53-
t.doesNotThrow(async () => await t.context.cache.remove('store', 'key-not-exist'));
53+
t.notThrows(async () => await t.context.cache.remove('store', 'key-not-exist'));
5454
});
5555

5656
test('remove val on non-existent store', async t => {
5757
t.throws(t.context.cache.remove('store-not-exist', 'key'), Error);
5858
});
5959

6060
test('illegal store name', async t => {
61-
t.throws(() => factory(localforageStub)(['with spaces']), UnityCacheError);
61+
t.throws(() => createCache(['with spaces']), UnityCacheError);
6262
});
6363

6464
test('does not throw on cache params', async t => {
65-
t.doesNotThrow(() => factory(localforageStub)(['store'], 'test', 'test database', 'localStorage'));
65+
t.notThrows(() => createCache(['store'], 'test', 'test database', 'localStorageWrapper'));
66+
});
67+
68+
test('drop store', async t => {
69+
t.notThrows(async () => await t.context.cache.drop([ 'drop_store' ]));
6670
});

test/localforage.stub.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/setup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var MockWebStorage = require('mock-webstorage');
2+
3+
global.localStorage = new MockWebStorage();

0 commit comments

Comments
 (0)