From: Jérôme Benoit Date: Sat, 5 Mar 2022 17:26:37 +0000 (+0100) Subject: Add isEmptyString() helper and use it X-Git-Tag: v1.1.52~25 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e81916227dc1a059ae4080cdb5fb4c0ef383788d;p=e-mobility-charging-stations-simulator.git Add isEmptyString() helper and use it Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index ef1c7db2..37457cee 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -732,9 +732,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ftpClient = new Client(); const accessResponse = await ftpClient.access({ host: uri.host, - ...(uri.port !== '' && { port: Utils.convertToInt(uri.port) }), - ...(uri.username !== '' && { user: uri.username }), - ...(uri.password !== '' && { password: uri.password }), + ...(!Utils.isEmptyString(uri.port) && { port: Utils.convertToInt(uri.port) }), + ...(!Utils.isEmptyString(uri.username) && { user: uri.username }), + ...(!Utils.isEmptyString(uri.password) && { password: uri.password }), }); let uploadResponse: FTPResponse; if (accessResponse.code === 220) { diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 0e8efc61..38df26a9 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,5 +1,6 @@ import { EmptyObject } from '../types/EmptyObject'; import { HandleErrorParams } from '../types/Error'; +import Utils from './Utils'; import chalk from 'chalk'; import logger from './Logger'; @@ -11,7 +12,7 @@ export default class FileUtils { error: NodeJS.ErrnoException, params: HandleErrorParams = { throwError: true, consoleOut: false } ): void { - const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; + const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : ''; if (error.code === 'ENOENT') { if (params?.consoleOut) { console.warn( diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 4fb2ce7b..8eebc2ab 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -171,6 +171,10 @@ export default class Utils { return typeof value === 'string'; } + public static isEmptyString(value: unknown): boolean { + return Utils.isString(value) && (value as string).length === 0; + } + public static isUndefined(value: unknown): boolean { return typeof value === 'undefined'; }