feat(storage): support search using a string

This commit is contained in:
Jovan Krunić
2019-01-24 14:34:48 +01:00
parent 86b9bff09a
commit 4334cad68c
2 changed files with 28 additions and 9 deletions

View File

@@ -24,7 +24,7 @@ describe('StorageProvider', () => {
const sampleEntries: Map<string, any> = new Map([
['foo', 'Bar'],
['bar', {foo: 'BarFoo'} as any],
['foobar', 123],
['foo.bar', 123],
]);
beforeEach(async () => {
@@ -101,7 +101,7 @@ describe('StorageProvider', () => {
const entries = await storageProvider.getAll();
expect(Array.from(entries.values()).map((item) => (item.foo) ? item.foo : item).sort())
.toEqual(Array.from(sampleEntries.values()).map((item) => (item.foo) ? item.foo : item).sort());
expect(Array.from(entries.keys()).sort()).toEqual(['bar', 'foo', 'foobar']);
expect(Array.from(entries.keys()).sort()).toEqual(['bar', 'foo', 'foo.bar']);
});
it('should delete one entry', async () => {
@@ -157,7 +157,14 @@ describe('StorageProvider', () => {
it('should allow search by regex', async () => {
await storageProvider.putMultiple(sampleEntries);
const found: Map<string, any> = await storageProvider.search<any>(/bar/);
expect(Array.from(found.keys()).sort()).toEqual(['bar', 'foobar']);
expect(Array.from(found.keys()).sort()).toEqual(['bar', 'foo.bar']);
expect(Array.from(found.values())).toEqual([{foo: 'BarFoo'}, 123]);
});
it('should allow search by string', async () => {
await storageProvider.putMultiple(sampleEntries);
const found: Map<string, any> = await storageProvider.search<any>('foo.ba');
expect(Array.from(found.keys())).toEqual(['foo.bar']);
expect(Array.from(found.values())).toEqual([123]);
});
});

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -54,14 +54,26 @@ export class StorageProvider {
}
/**
* Gets values from the storage using the provided regex
* Gets values from the storage using the provided pattern
*
* @param pattern Regular expression to test existing storage keys with
* @param pattern Regular expression or text to test existing storage keys with
*/
async search<T>(pattern: RegExp): Promise<Map<string, T>> {
async search<T>(pattern: RegExp | string): Promise<Map<string, T>> {
const map: Map<string, any> = new Map();
const check = (input: RegExp | string) => {
if (input instanceof RegExp) {
return (p: any, k: string): boolean => {
return p.test(k);
};
} else {
return (p: any, k: string): boolean => {
return k.includes(p);
};
}
};
const checkIt = check(pattern);
await this.storage.forEach((value, key) => {
if (pattern.test(key)) {
if (checkIt(pattern, key)) {
map.set(key, value);
}
});
@@ -108,7 +120,7 @@ export class StorageProvider {
const map = new Map();
const getToMap = async (key: string) => {
map.set(key, await this.get(key));
}
};
keys.forEach((key) => {
gets.push(getToMap(key));
});