From: Jérôme Benoit Date: Mon, 11 Mar 2024 22:51:26 +0000 (+0100) Subject: refactor: convert home made helpers to rambda ones X-Git-Tag: v1.3.1~50 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=38ae4ce2dba0f31e0f21c20490be0e7d376ce47c;p=e-mobility-charging-stations-simulator.git refactor: convert home made helpers to rambda ones Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 31ce3342..2f758aa3 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -8,7 +8,7 @@ import { URL } from 'node:url' import { parentPort } from 'node:worker_threads' import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns' -import { mergeDeepRight } from 'rambda' +import { mergeDeepRight, once } from 'rambda' import { type RawData, WebSocket } from 'ws' import { BaseError, OCPPError } from '../exception/index.js' @@ -96,7 +96,6 @@ import { logger, logPrefix, min, - once, roundTo, secureRandom, sleep, @@ -1156,7 +1155,7 @@ export class ChargingStation extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const stationTemplate = this.getTemplateFromFile()! checkTemplate(stationTemplate, this.logPrefix(), this.templateFile) - const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this) + const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation) warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile) if (stationTemplate.Connectors != null) { checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile) diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 726a0ab3..916cf499 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -21,6 +21,7 @@ import { toDate } from 'date-fns' import { maxTime } from 'date-fns/constants' +import { isEmpty } from 'rambda' import { BaseError } from '../exception/index.js' import { @@ -60,8 +61,6 @@ import { convertToInt, DCElectricUtils, isArraySorted, - isEmptyObject, - isEmptyString, isNotEmptyArray, isNotEmptyString, isValidDate, @@ -254,12 +253,12 @@ export const checkTemplate = ( logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (isEmptyObject(stationTemplate)) { + if (isEmpty(stationTemplate)) { const errorMsg = `Empty charging station information from template file ${templateFile}` logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (stationTemplate.idTagsFile == null || isEmptyString(stationTemplate.idTagsFile)) { + if (stationTemplate.idTagsFile == null || isEmpty(stationTemplate.idTagsFile)) { logger.warn( `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator` ) @@ -276,7 +275,7 @@ export const checkConfiguration = ( logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (isEmptyObject(stationConfiguration)) { + if (isEmpty(stationConfiguration)) { const errorMsg = `Empty charging station configuration from file ${configurationFile}` logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index b2a90e0f..7f459a4f 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,7 +1,8 @@ import { LRUMapWithDelete as LRUCache } from 'mnemonist' +import { isEmpty } from 'rambda' import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' -import { isEmptyObject, isNotEmptyArray, isNotEmptyString } from '../utils/index.js' +import { isNotEmptyArray, isNotEmptyString } from '../utils/index.js' import { Bootstrap } from './Bootstrap.js' enum CacheType { @@ -116,8 +117,8 @@ export class SharedLRUCache { chargingStationConfiguration.automaticTransactionGenerator != null && chargingStationConfiguration.configurationHash != null && isNotEmptyArray(chargingStationConfiguration.configurationKey) && - !isEmptyObject(chargingStationConfiguration.stationInfo) && - !isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator) && + !isEmpty(chargingStationConfiguration.stationInfo) && + !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) && isNotEmptyString(chargingStationConfiguration.configurationHash) ) } diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 1a825e39..cb152baf 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -1,4 +1,5 @@ import { secondsToMilliseconds } from 'date-fns' +import { isEmpty } from 'rambda' import { BaseError, type OCPPError } from '../../exception/index.js' import { @@ -36,13 +37,7 @@ import { type StopTransactionRequest, type StopTransactionResponse } from '../../types/index.js' -import { - Constants, - convertToInt, - isAsyncFunction, - isEmptyObject, - logger -} from '../../utils/index.js' +import { Constants, convertToInt, isAsyncFunction, logger } from '../../utils/index.js' import type { ChargingStation } from '../ChargingStation.js' import { getConfigurationKey } from '../ConfigurationKeyUtils.js' import { buildMeterValue } from '../ocpp/index.js' @@ -293,7 +288,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne let responsePayload: BroadcastChannelResponsePayload | undefined this.commandHandler(command, requestPayload) .then(commandResponse => { - if (commandResponse == null || isEmptyObject(commandResponse)) { + if (commandResponse == null || isEmpty(commandResponse)) { responsePayload = { hashId: this.chargingStation.stationInfo?.hashId, status: ResponseStatus.SUCCESS @@ -420,7 +415,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne return ResponseStatus.FAILURE case BroadcastChannelProcedureName.STATUS_NOTIFICATION: case BroadcastChannelProcedureName.METER_VALUES: - if (isEmptyObject(commandResponse)) { + if (isEmpty(commandResponse)) { return ResponseStatus.SUCCESS } return ResponseStatus.FAILURE diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index a61cb219..04182664 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -14,6 +14,7 @@ import { secondsToMilliseconds } from 'date-fns' import { maxTime } from 'date-fns/constants' +import { isEmpty } from 'rambda' import { create } from 'tar' import { @@ -105,7 +106,6 @@ import { formatDurationMilliSeconds, getRandomInteger, isAsyncFunction, - isEmptyArray, isNotEmptyArray, isNotEmptyString, logger, @@ -943,8 +943,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { } const connectorStatus = chargingStation.getConnectorStatus(connectorId) if ( - isEmptyArray(connectorStatus?.chargingProfiles) && - isEmptyArray(chargingStation.getConnectorStatus(0)?.chargingProfiles) + isEmpty(connectorStatus?.chargingProfiles) && + isEmpty(chargingStation.getConnectorStatus(0)?.chargingProfiles) ) { return OCPP16Constants.OCPP_RESPONSE_REJECTED } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 46ad889c..0b2fa348 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -4,7 +4,7 @@ import { env } from 'node:process' import { fileURLToPath } from 'node:url' import chalk from 'chalk' -import { mergeDeepRight } from 'rambda' +import { mergeDeepRight, once } from 'rambda' import { ApplicationProtocol, @@ -36,7 +36,7 @@ import { logPrefix } from './ConfigurationUtils.js' import { Constants } from './Constants.js' -import { hasOwnProp, isCFEnvironment, once } from './Utils.js' +import { hasOwnProp, isCFEnvironment } from './Utils.js' type ConfigurationSectionType = | LogConfiguration @@ -82,8 +82,7 @@ export class Configuration { public static getStationTemplateUrls (): StationTemplateUrl[] | undefined { const checkDeprecatedConfigurationKeysOnce = once( - Configuration.checkDeprecatedConfigurationKeys.bind(Configuration), - Configuration + Configuration.checkDeprecatedConfigurationKeys.bind(Configuration) ) checkDeprecatedConfigurationKeysOnce() return Configuration.getConfigurationData()?.stationTemplateUrls @@ -567,7 +566,7 @@ export class Configuration { event === 'change' ) { Configuration.configurationFileReloading = true - const consoleWarnOnce = once(console.warn, this) + const consoleWarnOnce = once(console.warn) consoleWarnOnce( `${chalk.green(logPrefix())} ${chalk.yellow( `${FileType.Configuration} ${this.configurationFile} file have changed, reload` diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 4c4c20f2..fc3cf9a9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -14,7 +14,6 @@ import { } from 'date-fns' import { - type EmptyObject, type JsonType, MapStringifyFormat, type TimestampedData, @@ -233,19 +232,6 @@ export const isObject = (value: unknown): value is object => { return value != null && typeof value === 'object' && !Array.isArray(value) } -export const isEmptyObject = (object: object): object is EmptyObject => { - if (object.constructor !== Object) { - return false - } - // Iterates over the keys of an object, if - // any exist, return false. - // eslint-disable-next-line no-unreachable-loop - for (const _ in object) { - return false - } - return true -} - export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => { return isObject(value) && Object.hasOwn(value, property) } @@ -258,18 +244,10 @@ const isString = (value: unknown): value is string => { return typeof value === 'string' } -export const isEmptyString = (value: unknown): value is '' | undefined | null => { - return value == null || (isString(value) && value.trim().length === 0) -} - export const isNotEmptyString = (value: unknown): value is string => { return isString(value) && value.trim().length > 0 } -export const isEmptyArray = (value: unknown): value is [] => { - return Array.isArray(value) && value.length === 0 -} - export const isNotEmptyArray = (value: unknown): value is unknown[] => { return Array.isArray(value) && value.length > 0 } @@ -368,22 +346,6 @@ export const isArraySorted = (array: T[], compareFn: (a: T, b: T) => number): return true } -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export const once = ( - fn: (...args: A) => R, - context: T -): ((...args: A) => R) => { - let result: R - return (...args: A) => { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (fn != null) { - result = fn.apply(context, args) - ;(fn as unknown as undefined) = (context as unknown as undefined) = undefined - } - return result - } -} - export const min = (...args: number[]): number => args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) diff --git a/src/utils/index.ts b/src/utils/index.ts index daa67094..726a69db 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -45,9 +45,6 @@ export { getWebSocketCloseEventStatusString, isArraySorted, isAsyncFunction, - isEmptyArray, - isEmptyObject, - isEmptyString, isNotEmptyArray, isNotEmptyString, isValidDate, @@ -55,7 +52,6 @@ export { logPrefix, max, min, - once, roundTo, secureRandom, sleep, diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 0ed7d6b6..630831a8 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -21,16 +21,12 @@ import { hasOwnProp, isArraySorted, isAsyncFunction, - isEmptyArray, - isEmptyObject, - isEmptyString, isNotEmptyArray, isNotEmptyString, isObject, isValidDate, max, min, - once, roundTo, secureRandom, sleep, @@ -375,24 +371,6 @@ await describe('Utils test suite', async () => { expect(hasOwnProp({ 1: '1' }, 2)).toBe(false) }) - await it('Verify isEmptyString()', () => { - expect(isEmptyString('')).toBe(true) - expect(isEmptyString(' ')).toBe(true) - expect(isEmptyString(' ')).toBe(true) - expect(isEmptyString('test')).toBe(false) - expect(isEmptyString(' test')).toBe(false) - expect(isEmptyString('test ')).toBe(false) - expect(isEmptyString(undefined)).toBe(true) - expect(isEmptyString(null)).toBe(true) - expect(isEmptyString(0)).toBe(false) - expect(isEmptyString({})).toBe(false) - expect(isEmptyString([])).toBe(false) - expect(isEmptyString(new Map())).toBe(false) - expect(isEmptyString(new Set())).toBe(false) - expect(isEmptyString(new WeakMap())).toBe(false) - expect(isEmptyString(new WeakSet())).toBe(false) - }) - await it('Verify isNotEmptyString()', () => { expect(isNotEmptyString('')).toBe(false) expect(isNotEmptyString(' ')).toBe(false) @@ -411,22 +389,6 @@ await describe('Utils test suite', async () => { expect(isNotEmptyString(new WeakSet())).toBe(false) }) - await it('Verify isEmptyArray()', () => { - expect(isEmptyArray([])).toBe(true) - expect(isEmptyArray([1, 2])).toBe(false) - expect(isEmptyArray(['1', '2'])).toBe(false) - expect(isEmptyArray(undefined)).toBe(false) - expect(isEmptyArray(null)).toBe(false) - expect(isEmptyArray('')).toBe(false) - expect(isEmptyArray('test')).toBe(false) - expect(isEmptyArray(0)).toBe(false) - expect(isEmptyArray({})).toBe(false) - expect(isEmptyArray(new Map())).toBe(false) - expect(isEmptyArray(new Set())).toBe(false) - expect(isEmptyArray(new WeakMap())).toBe(false) - expect(isEmptyArray(new WeakSet())).toBe(false) - }) - await it('Verify isNotEmptyArray()', () => { expect(isNotEmptyArray([])).toBe(false) expect(isNotEmptyArray([1, 2])).toBe(true) @@ -443,17 +405,6 @@ await describe('Utils test suite', async () => { expect(isNotEmptyArray(new WeakSet())).toBe(false) }) - await it('Verify isEmptyObject()', () => { - expect(isEmptyObject({})).toBe(true) - expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false) - expect(isEmptyObject([])).toBe(false) - expect(isEmptyObject([1, 2])).toBe(false) - expect(isEmptyObject(new Map())).toBe(false) - expect(isEmptyObject(new Set())).toBe(false) - expect(isEmptyObject(new WeakMap())).toBe(false) - expect(isEmptyObject(new WeakSet())).toBe(false) - }) - await it('Verify isArraySorted()', () => { expect( isArraySorted([], (a, b) => { @@ -470,21 +421,6 @@ await describe('Utils test suite', async () => { expect(isArraySorted([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false) }) - await it('Verify once()', () => { - let called = 0 - const fn = (): number => ++called - const onceFn = once(fn, this) - const result1 = onceFn() - expect(called).toBe(1) - expect(result1).toBe(1) - const result2 = onceFn() - expect(called).toBe(1) - expect(result2).toBe(1) - const result3 = onceFn() - expect(called).toBe(1) - expect(result3).toBe(1) - }) - await it('Verify min()', () => { expect(min()).toBe(Infinity) expect(min(0, 1)).toBe(0)