Skip to content

Commit

Permalink
fix: do not allow numeric strings, and add tests for both empty strin…
Browse files Browse the repository at this point in the history
…g and null namespace
  • Loading branch information
vitormv committed Feb 5, 2021
1 parent fdc9acb commit a195600
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -47,7 +47,7 @@ class Keyv extends EventEmitter {
}

_getKeyPrefix(key) {
return this.opts.namespace ? `${this.opts.namespace}:${key}` : key;
return this.opts.namespace ? `${this.opts.namespace}:${key}` : String(key);
}

get(key, opts) {
Expand Down
25 changes: 22 additions & 3 deletions test/keyv.js
Expand Up @@ -128,21 +128,40 @@ test.serial('Keyv uses a default namespace', async t => {
const store = new Map();
const keyv = new Keyv({ store });
await keyv.set('foo', 'bar');
const savedValue = await keyv.get('foo');

t.is([...store.keys()][0], 'keyv:foo');
t.is(savedValue, 'bar');
});

test.serial('Default namespace can be overridden', async t => {
const store = new Map();
const keyv = new Keyv({ store, namespace: 'magic' });
await keyv.set('foo', 'bar');
const savedValue = await keyv.get('foo');

t.is([...store.keys()][0], 'magic:foo');
t.is(savedValue, 'bar');
});

test.serial('An empty namespace stores the key as-is', async t => {
test.serial('An empty namespace can be used', async t => {
const store = new Map();
const keyv = new Keyv({ store, namespace: '' });
await keyv.set(42, 'foo');
t.is([...store.keys()][0], 42);
await keyv.set(42, 'bar');
const savedValue = await keyv.get('42');

t.is([...store.keys()][0], '42');
t.is(savedValue, 'bar');
});

test.serial('A null namespace can be used', async t => {
const store = new Map();
const keyv = new Keyv({ store, namespace: null });
await keyv.set(42, 'bar');
const savedValue = await keyv.get('42');

t.is([...store.keys()][0], '42');
t.is(savedValue, 'bar');
});

const store = () => new Map();
Expand Down

0 comments on commit a195600

Please sign in to comment.