refactor: replace TSLint with ESLint

This commit is contained in:
Wieland Schöbl
2021-06-30 13:53:44 +02:00
committed by Jovan Krunić
parent 67fb4a43c9
commit d696215d08
147 changed files with 5471 additions and 2704 deletions

View File

@@ -20,11 +20,7 @@ import {StorageProvider} from './storage.provider';
* Angular storage provider module
*/
@NgModule({
imports: [
IonicStorageModule.forRoot(),
],
providers: [
StorageProvider,
],
imports: [IonicStorageModule.forRoot()],
providers: [StorageProvider],
})
export class StorageModule {}

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -61,17 +62,23 @@ describe('StorageProvider', () => {
it('should properly put and get a value', async () => {
await storageProvider.init();
await storageProvider.put('some-uid', {some: 'thing'});
const result: Map<string, object> = await storageProvider.get<Map<string, object>>('some-uid');
expect(result).toEqual({some: 'thing'});
// eslint-disable-next-line @typescript-eslint/ban-types
const result: Map<string, object> = await storageProvider.get<
// eslint-disable-next-line @typescript-eslint/ban-types
Map<string, object>
>('some-uid');
await expect(result).toEqual({some: 'thing'});
});
it('should throw an error when value is null', async () => {
// eslint-disable-next-line unicorn/error-message
let error: Error = new Error();
// eslint-disable-next-line unicorn/no-null
spyOn(storage, 'get').and.returnValue(Promise.resolve(null));
try {
await storageProvider.get('something-else');
} catch (err) {
error = err;
} catch (error_) {
error = error_;
}
expect(error).toEqual(new Error('Value not found.'));
});
@@ -91,29 +98,34 @@ describe('StorageProvider', () => {
expect((storageProvider.get as jasmine.Spy).calls.count()).toEqual(2);
expect(storageProvider.get).toHaveBeenCalledWith('foo');
expect(storageProvider.get).toHaveBeenCalledWith('bar');
expect(Array.from(entries.values())).toEqual(['Bar', {foo: 'BarFoo'}]);
expect(Array.from(entries.keys())).toEqual(['foo', 'bar']);
expect([...entries.values()]).toEqual(['Bar', {foo: 'BarFoo'}]);
expect([...entries.keys()]).toEqual(['foo', 'bar']);
});
it('should get all values from the storage', async () => {
spyOn(storageProvider, 'get').and.callThrough();
await storageProvider.putMultiple(sampleEntries);
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', 'foo.bar']);
expect(
[...entries.values()].map(item => (item.foo ? item.foo : item)).sort(),
).toEqual(
[...sampleEntries.values()]
.map(item => (item.foo ? item.foo : item))
.sort(),
);
expect([...entries.keys()].sort()).toEqual(['bar', 'foo', 'foo.bar']);
});
it('should delete one or more entries from the storage', async () => {
spyOn(storage, 'remove').and.callThrough();
await storageProvider.putMultiple(sampleEntries);
let entries = await storageProvider.getAll();
expect(Array.from(entries.values()).length).toBe(3);
expect([...entries.values()].length).toBe(3);
await storageProvider.delete('bar');
expect(storage.remove).toHaveBeenCalled();
entries = await storageProvider.getAll();
expect(Array.from(entries.values())).toEqual(['Bar', 123]);
expect([...entries.values()]).toEqual(['Bar', 123]);
await storageProvider.delete('foo', 'foo.bar');
expect(await storageProvider.length()).toBe(0);
@@ -123,12 +135,12 @@ describe('StorageProvider', () => {
spyOn(storage, 'clear').and.callThrough();
await storageProvider.putMultiple(sampleEntries);
let entries = await storageProvider.getAll();
expect(Array.from(entries.values()).length).toBe(3);
expect([...entries.values()].length).toBe(3);
await storageProvider.deleteAll();
entries = await storageProvider.getAll();
expect(storage.clear).toHaveBeenCalled();
expect(Array.from(entries.values()).length).toBe(0);
expect([...entries.values()].length).toBe(0);
});
it('should provide number of entries', async () => {
@@ -152,7 +164,9 @@ describe('StorageProvider', () => {
});
it('should provide information if storage contains a specific entry (key)', async () => {
spyOn(storage, 'keys').and.returnValue(Promise.resolve(Array.from(sampleEntries.keys())));
spyOn(storage, 'keys').and.returnValue(
Promise.resolve([...sampleEntries.keys()]),
);
expect(await storageProvider.has('foo')).toBeTruthy();
expect(await storageProvider.has('something-else')).toBeFalsy();
});
@@ -160,14 +174,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', 'foo.bar']);
expect(Array.from(found.values())).toEqual([{foo: 'BarFoo'}, 123]);
expect([...found.keys()].sort()).toEqual(['bar', 'foo.bar']);
expect([...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]);
expect([...found.keys()]).toEqual(['foo.bar']);
expect([...found.values()]).toEqual([123]);
});
});

View File

@@ -23,8 +23,7 @@ export class StorageProvider {
/**
* @param storage TODO
*/
constructor(private readonly storage: Storage) {
}
constructor(private readonly storage: Storage) {}
/**
* Deletes storage entries using keys used to save them
@@ -32,7 +31,7 @@ export class StorageProvider {
* @param keys Unique identifiers of the resources for deletion
*/
async delete(...keys: string[]): Promise<void> {
keys.forEach(async (key) => {
keys.map(async key => {
await this.storage.remove(key);
});
}
@@ -58,12 +57,13 @@ export class StorageProvider {
return entry;
}
// tslint:disable:no-any
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Retrieves all the storage entries
*/
async getAll(): Promise<Map<string, any>> {
const map: Map<string, any> = new Map();
// eslint-disable-next-line unicorn/no-array-for-each
await this.storage.forEach((value, key) => {
map.set(key, value);
});
@@ -82,7 +82,7 @@ export class StorageProvider {
const getToMap = async (key: string) => {
map.set(key, await this.get(key));
};
keys.forEach((key) => {
keys.map(key => {
gets.push(getToMap(key));
});
await Promise.all(gets);
@@ -137,9 +137,9 @@ export class StorageProvider {
*/
async putMultiple(entries: Map<string, any>): Promise<Map<string, any>> {
const puts: Array<Promise<any>> = [];
entries.forEach(async (value, key) => {
for (const [key, value] of entries.entries()) {
puts.push(this.put(key, value));
});
}
await Promise.all(puts);
return entries;
@@ -164,6 +164,7 @@ export class StorageProvider {
};
};
const checkIt = check(pattern);
// eslint-disable-next-line unicorn/no-array-for-each
await this.storage.forEach((value, key) => {
if (checkIt(pattern, key)) {
map.set(key, value);