mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 01:22:54 +00:00
feat(storage): add search using regex
This commit is contained in:
@@ -153,4 +153,11 @@ describe('StorageProvider', () => {
|
|||||||
expect(await storageProvider.has('foo')).toBeTruthy();
|
expect(await storageProvider.has('foo')).toBeTruthy();
|
||||||
expect(await storageProvider.has('something-else')).toBeFalsy();
|
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]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -53,6 +53,21 @@ export class StorageProvider {
|
|||||||
return entry;
|
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
|
* Deletes a storage entry using the key used to save it
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user