From ef4932d8aec470321413643e3c0647b0b29396de Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 14 Feb 2023 21:52:07 +0100 Subject: [PATCH] fix(simulator): rename and fix object own property detection helper signature MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/types/ChargingStationWorker.ts | 2 +- src/utils/Configuration.ts | 45 ++++++++++++++---------------- src/utils/Utils.ts | 2 +- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/types/ChargingStationWorker.ts b/src/types/ChargingStationWorker.ts index cd2cb005..d2881c0a 100644 --- a/src/types/ChargingStationWorker.ts +++ b/src/types/ChargingStationWorker.ts @@ -10,7 +10,7 @@ import type { } from './internal'; import { type WorkerData, type WorkerMessage, WorkerMessageEvents } from '../worker'; -export interface ChargingStationWorkerOptions extends JsonObject { +interface ChargingStationWorkerOptions extends JsonObject { elementStartDelay?: number; } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 6f1705a6..5cf5d816 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -46,13 +46,13 @@ export class Configuration { "Use 'logStatisticsInterval' instead" ); // Read conf - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logStatisticsInterval') + return Utils.hasOwnProp(Configuration.getConfig(), 'logStatisticsInterval') ? Configuration.getConfig()?.logStatisticsInterval : Constants.DEFAULT_LOG_STATISTICS_INTERVAL; } static getUIServer(): UIServerConfiguration { - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) { + if (Utils.hasOwnProp(Configuration.getConfig(), 'uiWebSocketServer')) { console.error( chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration section 'uiWebSocketServer' usage. Use 'uiServer' instead}` ); @@ -65,7 +65,7 @@ export class Configuration { port: Constants.DEFAULT_UI_SERVER_PORT, }, }; - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { + if (Utils.hasOwnProp(Configuration.getConfig(), 'uiServer')) { uiServerConfiguration = merge( uiServerConfiguration, Configuration.getConfig()?.uiServer @@ -85,7 +85,7 @@ export class Configuration { type: StorageType.JSON_FILE, uri: this.getDefaultPerformanceStorageUri(StorageType.JSON_FILE), }; - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) { + if (Utils.hasOwnProp(Configuration.getConfig(), 'performanceStorage')) { storageConfiguration = { ...storageConfiguration, ...Configuration.getConfig()?.performanceStorage, @@ -111,7 +111,7 @@ export class Configuration { 'Use it in charging station template instead' ); // Read conf - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) { + if (Utils.hasOwnProp(Configuration.getConfig(), 'autoReconnectMaxRetries')) { return Configuration.getConfig()?.autoReconnectMaxRetries; } } @@ -186,31 +186,28 @@ export class Configuration { "Use 'worker' section to define the worker pool strategy instead" ); let workerConfiguration: WorkerConfiguration = { - processType: Utils.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') + processType: Utils.hasOwnProp(Configuration.getConfig(), 'workerProcess') ? Configuration.getConfig()?.workerProcess : WorkerProcessType.WORKER_SET, - startDelay: Utils.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') + startDelay: Utils.hasOwnProp(Configuration.getConfig(), 'workerStartDelay') ? Configuration.getConfig()?.workerStartDelay : WorkerConstants.DEFAULT_WORKER_START_DELAY, - elementsPerWorker: Utils.objectHasOwnProperty( - Configuration.getConfig(), - 'chargingStationsPerWorker' - ) + elementsPerWorker: Utils.hasOwnProp(Configuration.getConfig(), 'chargingStationsPerWorker') ? Configuration.getConfig()?.chargingStationsPerWorker : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, - elementStartDelay: Utils.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay') + elementStartDelay: Utils.hasOwnProp(Configuration.getConfig(), 'elementStartDelay') ? Configuration.getConfig()?.elementStartDelay : WorkerConstants.DEFAULT_ELEMENT_START_DELAY, - poolMinSize: Utils.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') + poolMinSize: Utils.hasOwnProp(Configuration.getConfig(), 'workerPoolMinSize') ? Configuration.getConfig()?.workerPoolMinSize : WorkerConstants.DEFAULT_POOL_MIN_SIZE, - poolMaxSize: Utils.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') + poolMaxSize: Utils.hasOwnProp(Configuration.getConfig(), 'workerPoolMaxSize') ? Configuration.getConfig()?.workerPoolMaxSize : WorkerConstants.DEFAULT_POOL_MAX_SIZE, poolStrategy: Configuration.getConfig()?.workerPoolStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN, }; - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'worker')) { + if (Utils.hasOwnProp(Configuration.getConfig(), 'worker')) { workerConfiguration = { ...workerConfiguration, ...Configuration.getConfig()?.worker }; } return workerConfiguration; @@ -222,45 +219,45 @@ export class Configuration { undefined, "Use 'logConsole' instead" ); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') + return Utils.hasOwnProp(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig()?.logConsole : false; } static getLogFormat(): string | undefined { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') + return Utils.hasOwnProp(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig()?.logFormat : 'simple'; } static getLogRotate(): boolean | undefined { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') + return Utils.hasOwnProp(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig()?.logRotate : true; } static getLogMaxFiles(): number | string | false | undefined { return ( - Utils.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Utils.hasOwnProp(Configuration.getConfig(), 'logMaxFiles') && Configuration.getConfig()?.logMaxFiles ); } static getLogMaxSize(): number | string | false | undefined { return ( - Utils.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Utils.hasOwnProp(Configuration.getConfig(), 'logMaxFiles') && Configuration.getConfig()?.logMaxSize ); } static getLogLevel(): string | undefined { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') + return Utils.hasOwnProp(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig()?.logLevel?.toLowerCase() : 'info'; } static getLogFile(): string | undefined { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFile') + return Utils.hasOwnProp(Configuration.getConfig(), 'logFile') ? Configuration.getConfig()?.logFile : 'combined.log'; } @@ -271,7 +268,7 @@ export class Configuration { undefined, "Use 'logErrorFile' instead" ); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') + return Utils.hasOwnProp(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig()?.logErrorFile : 'error.log'; } @@ -301,7 +298,7 @@ export class Configuration { undefined, "Use 'supervisionUrlDistribution' instead" ); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'supervisionUrlDistribution') + return Utils.hasOwnProp(Configuration.getConfig(), 'supervisionUrlDistribution') ? Configuration.getConfig()?.supervisionUrlDistribution : SupervisionUrlDistribution.ROUND_ROBIN; } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 7b59118c..9323e4be 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -185,7 +185,7 @@ export class Utils { return clone(object); } - public static objectHasOwnProperty(object: unknown, property: string): boolean { + public static hasOwnProp(object: unknown, property: PropertyKey): boolean { return Utils.isObject(object) && Object.hasOwn(object as object, property); } -- 2.34.1