mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-11 04:03:34 +00:00
feat(storage): support search using a string
This commit is contained in:
@@ -24,7 +24,7 @@ describe('StorageProvider', () => {
|
|||||||
const sampleEntries: Map<string, any> = new Map([
|
const sampleEntries: Map<string, any> = new Map([
|
||||||
['foo', 'Bar'],
|
['foo', 'Bar'],
|
||||||
['bar', {foo: 'BarFoo'} as any],
|
['bar', {foo: 'BarFoo'} as any],
|
||||||
['foobar', 123],
|
['foo.bar', 123],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -101,7 +101,7 @@ describe('StorageProvider', () => {
|
|||||||
const entries = await storageProvider.getAll();
|
const entries = await storageProvider.getAll();
|
||||||
expect(Array.from(entries.values()).map((item) => (item.foo) ? item.foo : item).sort())
|
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());
|
.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 () => {
|
it('should delete one entry', async () => {
|
||||||
@@ -157,7 +157,14 @@ describe('StorageProvider', () => {
|
|||||||
it('should allow search by regex', async () => {
|
it('should allow search by regex', async () => {
|
||||||
await storageProvider.putMultiple(sampleEntries);
|
await storageProvider.putMultiple(sampleEntries);
|
||||||
const found: Map<string, any> = await storageProvider.search<any>(/bar/);
|
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]);
|
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]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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
|
* 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
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* 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 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) => {
|
await this.storage.forEach((value, key) => {
|
||||||
if (pattern.test(key)) {
|
if (checkIt(pattern, key)) {
|
||||||
map.set(key, value);
|
map.set(key, value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -108,7 +120,7 @@ export class StorageProvider {
|
|||||||
const map = new Map();
|
const map = new Map();
|
||||||
const getToMap = async (key: string) => {
|
const getToMap = async (key: string) => {
|
||||||
map.set(key, await this.get(key));
|
map.set(key, await this.get(key));
|
||||||
}
|
};
|
||||||
keys.forEach((key) => {
|
keys.forEach((key) => {
|
||||||
gets.push(getToMap(key));
|
gets.push(getToMap(key));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user