feat(storage): add search using regex

This commit is contained in:
Jovan Krunić
2018-12-14 14:47:20 +01:00
parent 246dddd5a5
commit 86b9bff09a
2 changed files with 22 additions and 0 deletions

View File

@@ -153,4 +153,11 @@ describe('StorageProvider', () => {
expect(await storageProvider.has('foo')).toBeTruthy();
expect(await storageProvider.has('something-else')).toBeFalsy();
});
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.values())).toEqual([{foo: 'BarFoo'}, 123]);
});
});

View File

@@ -53,6 +53,21 @@ export class StorageProvider {
return entry;
}
/**
* Gets values from the storage using the provided regex
*
* @param pattern Regular expression to test existing storage keys with
*/
async search<T>(pattern: RegExp): Promise<Map<string, T>> {
const map: Map<string, any> = new Map();
await this.storage.forEach((value, key) => {
if (pattern.test(key)) {
map.set(key, value);
}
});
return map;
}
/**
* Deletes a storage entry using the key used to save it
*