refactor: move to eslint

This commit is contained in:
Rainer Killinger
2022-08-17 16:02:34 +02:00
parent c1dc7b4e8f
commit f864c64efa
92 changed files with 2287 additions and 1871 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 StApps
* Copyright (C) 2019-2022 Open 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.
@@ -16,7 +16,12 @@ import {SCTranslations} from './general/i18n';
import {SCBulkResponse} from './protocol/routes/bulk-request';
import {SCSearchResponse} from './protocol/routes/search';
import {SCMultiSearchResponse} from './protocol/routes/search-multi';
import {SCThing, SCThingTranslatableProperties, SCThingType, SCThingWithoutReferences} from './things/abstract/thing';
import {
SCThing,
SCThingTranslatableProperties,
SCThingType,
SCThingWithoutReferences,
} from './things/abstract/thing';
/**
* Type guard to check if something is a SCThing
@@ -32,16 +37,13 @@ export function isThing(something: unknown): something is SCThing {
return false;
}
// tslint:disable-next-line:completed-docs
const type = (something as { type: unknown; }).type;
const type = (something as {type: unknown}).type;
if (typeof type !== 'string') {
return false;
}
return Object
.values(SCThingType)
.indexOf(type as SCThingType) >= 0;
return Object.values(SCThingType).includes(type as SCThingType);
}
/**
@@ -49,9 +51,9 @@ export function isThing(something: unknown): something is SCThing {
*
* @param thing Thing to check
*/
export function isThingWithTranslations(thing: SCThingWithoutReferences)
// tslint:disable-next-line:completed-docs
: thing is SCThingWithoutReferences & { translations: SCTranslations<SCThingTranslatableProperties>; } {
export function isThingWithTranslations(
thing: SCThingWithoutReferences,
): thing is SCThingWithoutReferences & {translations: SCTranslations<SCThingTranslatableProperties>} {
return typeof thing.translations !== 'undefined';
}
@@ -65,29 +67,31 @@ export function isBulkResponse(something: unknown): something is SCBulkResponse
return false;
}
if (!('expiration' in something)
|| !('source' in something)
|| !('state' in something)
|| !('type' in something)
|| !('uid' in something)) {
if (
!('expiration' in something) ||
!('source' in something) ||
!('state' in something) ||
!('type' in something) ||
!('uid' in something)
) {
return false;
}
const {expiration, source, state, type, uid} = something as {
// tslint:disable:completed-docs
expiration: unknown;
source: unknown;
state: unknown;
type: unknown;
uid: unknown;
// tslint:enable
};
return typeof expiration === 'string'
&& typeof source === 'string'
&& typeof state === 'string'
&& typeof type === 'string'
&& typeof uid === 'string';
return (
typeof expiration === 'string' &&
typeof source === 'string' &&
typeof state === 'string' &&
typeof type === 'string' &&
typeof uid === 'string'
);
}
/**
@@ -99,16 +103,18 @@ export function isSearchResponse(something: unknown): something is SCSearchRespo
if (!(typeof something === 'object') || something === null) {
return false;
}
const somethingObject = (something as { [key: string]: { [key: string]: string; }; });
const somethingObject = something as {[key: string]: {[key: string]: string}};
return Array.isArray(somethingObject.data)
&& Array.isArray(somethingObject.facets)
&& typeof somethingObject.pagination !== 'undefined'
&& typeof somethingObject.pagination.count === 'number'
&& typeof somethingObject.pagination.offset === 'number'
&& typeof somethingObject.pagination.total === 'number'
&& typeof somethingObject.stats !== 'undefined'
&& typeof somethingObject.stats.time === 'number';
return (
Array.isArray(somethingObject.data) &&
Array.isArray(somethingObject.facets) &&
typeof somethingObject.pagination !== 'undefined' &&
typeof somethingObject.pagination.count === 'number' &&
typeof somethingObject.pagination.offset === 'number' &&
typeof somethingObject.pagination.total === 'number' &&
typeof somethingObject.stats !== 'undefined' &&
typeof somethingObject.stats.time === 'number'
);
}
/**
@@ -117,10 +123,10 @@ export function isSearchResponse(something: unknown): something is SCSearchRespo
* @param something Something to check
*/
export function isMultiSearchResponse(something: unknown): something is SCMultiSearchResponse {
const initialValue = Object.keys(something as { [key: string]: string; }).length > 0 ? true : false;
const initialValue = Object.keys(something as {[key: string]: string}).length > 0 ? true : false;
return Object.keys(something as { [key: string]: string; })
.reduce((previousOnesAreSearchResponses, key) => {
return previousOnesAreSearchResponses && isSearchResponse((something as { [key: string]: string; })[key]);
}, initialValue as boolean);
// eslint-disable-next-line unicorn/no-array-reduce
return Object.keys(something as {[key: string]: string}).reduce((previousOnesAreSearchResponses, key) => {
return previousOnesAreSearchResponses && isSearchResponse((something as {[key: string]: string})[key]);
}, initialValue as boolean);
}