diff --git a/src/app/modules/storage/storage.provider.spec.ts b/src/app/modules/storage/storage.provider.spec.ts index 1ebe6268..99aadcd0 100644 --- a/src/app/modules/storage/storage.provider.spec.ts +++ b/src/app/modules/storage/storage.provider.spec.ts @@ -24,7 +24,7 @@ describe('StorageProvider', () => { const sampleEntries: Map = 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 = await storageProvider.search(/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 = await storageProvider.search('foo.ba'); + expect(Array.from(found.keys())).toEqual(['foo.bar']); + expect(Array.from(found.values())).toEqual([123]); + }); }); diff --git a/src/app/modules/storage/storage.provider.ts b/src/app/modules/storage/storage.provider.ts index d7a84350..802f3844 100644 --- a/src/app/modules/storage/storage.provider.ts +++ b/src/app/modules/storage/storage.provider.ts @@ -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(pattern: RegExp): Promise> { + async search(pattern: RegExp | string): Promise> { const map: Map = 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)); });