From e81916227dc1a059ae4080cdb5fb4c0ef383788d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 5 Mar 2022 18:26:37 +0100 Subject: [PATCH] Add isEmptyString() helper and use it MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 6 +++--- src/utils/FileUtils.ts | 3 ++- src/utils/Utils.ts | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) 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'; } -- 2.34.1