From e0e6de7ff7e4fad7f4b7ed364caba0a25f075a70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 16 Oct 2024 17:34:45 +0200 Subject: [PATCH] refactor: use rambda has() helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Configuration.ts | 51 +++++++++++++++++++------------------- src/utils/Utils.ts | 8 ------ tests/utils/Utils.test.ts | 35 -------------------------- 3 files changed, 26 insertions(+), 68 deletions(-) diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 32ef183b..ae55c5f0 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -4,6 +4,7 @@ import { dirname, join } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' import { mergeDeepRight, once } from 'rambda' +import { has } from 'rambda' import { ApplicationProtocol, @@ -35,7 +36,7 @@ import { logPrefix, } from './ConfigurationUtils.js' import { Constants } from './Constants.js' -import { hasOwnProp, isCFEnvironment } from './Utils.js' +import { isCFEnvironment } from './Utils.js' type ConfigurationSectionType = | LogConfiguration @@ -129,43 +130,43 @@ export class Configuration { private static buildLogSection (): LogConfiguration { const deprecatedLogConfiguration: LogConfiguration = { - ...(hasOwnProp(Configuration.getConfigurationData(), 'logEnabled') && { + ...(has(Configuration.getConfigurationData(), 'logEnabled') && { // eslint-disable-next-line @typescript-eslint/no-deprecated enabled: Configuration.getConfigurationData()?.logEnabled, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logFile') && { + ...(has(Configuration.getConfigurationData(), 'logFile') && { // eslint-disable-next-line @typescript-eslint/no-deprecated file: Configuration.getConfigurationData()?.logFile, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logErrorFile') && { + ...(has(Configuration.getConfigurationData(), 'logErrorFile') && { // eslint-disable-next-line @typescript-eslint/no-deprecated errorFile: Configuration.getConfigurationData()?.logErrorFile, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logStatisticsInterval') && { + ...(has(Configuration.getConfigurationData(), 'logStatisticsInterval') && { // eslint-disable-next-line @typescript-eslint/no-deprecated statisticsInterval: Configuration.getConfigurationData()?.logStatisticsInterval, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logLevel') && { + ...(has(Configuration.getConfigurationData(), 'logLevel') && { // eslint-disable-next-line @typescript-eslint/no-deprecated level: Configuration.getConfigurationData()?.logLevel, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logConsole') && { + ...(has(Configuration.getConfigurationData(), 'logConsole') && { // eslint-disable-next-line @typescript-eslint/no-deprecated console: Configuration.getConfigurationData()?.logConsole, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logFormat') && { + ...(has(Configuration.getConfigurationData(), 'logFormat') && { // eslint-disable-next-line @typescript-eslint/no-deprecated format: Configuration.getConfigurationData()?.logFormat, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logRotate') && { + ...(has(Configuration.getConfigurationData(), 'logRotate') && { // eslint-disable-next-line @typescript-eslint/no-deprecated rotate: Configuration.getConfigurationData()?.logRotate, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logMaxFiles') && { + ...(has(Configuration.getConfigurationData(), 'logMaxFiles') && { // eslint-disable-next-line @typescript-eslint/no-deprecated maxFiles: Configuration.getConfigurationData()?.logMaxFiles, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'logMaxSize') && { + ...(has(Configuration.getConfigurationData(), 'logMaxSize') && { // eslint-disable-next-line @typescript-eslint/no-deprecated maxSize: Configuration.getConfigurationData()?.logMaxSize, }), @@ -173,7 +174,7 @@ export class Configuration { const logConfiguration: LogConfiguration = { ...defaultLogConfiguration, ...deprecatedLogConfiguration, - ...(hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.log) && + ...(has(Configuration.getConfigurationData(), ConfigurationSection.log) && Configuration.getConfigurationData()?.log), } return logConfiguration @@ -201,7 +202,7 @@ export class Configuration { storageConfiguration = defaultStorageConfiguration break } - if (hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.performanceStorage)) { + if (has(Configuration.getConfigurationData(), ConfigurationSection.performanceStorage)) { storageConfiguration = { ...storageConfiguration, ...Configuration.getConfigurationData()?.performanceStorage, @@ -221,7 +222,7 @@ export class Configuration { private static buildUIServerSection (): UIServerConfiguration { let uiServerConfiguration: UIServerConfiguration = defaultUIServerConfiguration - if (hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.uiServer)) { + if (has(Configuration.getConfigurationData(), ConfigurationSection.uiServer)) { uiServerConfiguration = mergeDeepRight( uiServerConfiguration, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -238,42 +239,42 @@ export class Configuration { private static buildWorkerSection (): WorkerConfiguration { const deprecatedWorkerConfiguration: WorkerConfiguration = { - ...(hasOwnProp(Configuration.getConfigurationData(), 'workerProcess') && { + ...(has(Configuration.getConfigurationData(), 'workerProcess') && { // eslint-disable-next-line @typescript-eslint/no-deprecated processType: Configuration.getConfigurationData()?.workerProcess, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'workerStartDelay') && { + ...(has(Configuration.getConfigurationData(), 'workerStartDelay') && { // eslint-disable-next-line @typescript-eslint/no-deprecated startDelay: Configuration.getConfigurationData()?.workerStartDelay, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'chargingStationsPerWorker') && { + ...(has(Configuration.getConfigurationData(), 'chargingStationsPerWorker') && { // eslint-disable-next-line @typescript-eslint/no-deprecated elementsPerWorker: Configuration.getConfigurationData()?.chargingStationsPerWorker, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'elementAddDelay') && { + ...(has(Configuration.getConfigurationData(), 'elementAddDelay') && { // eslint-disable-next-line @typescript-eslint/no-deprecated elementAddDelay: Configuration.getConfigurationData()?.elementAddDelay, }), - ...(hasOwnProp(Configuration.getConfigurationData()?.worker, 'elementStartDelay') && { + ...(has(Configuration.getConfigurationData()?.worker, 'elementStartDelay') && { // eslint-disable-next-line @typescript-eslint/no-deprecated elementAddDelay: Configuration.getConfigurationData()?.worker?.elementStartDelay, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'workerPoolMinSize') && { + ...(has(Configuration.getConfigurationData(), 'workerPoolMinSize') && { // eslint-disable-next-line @typescript-eslint/no-deprecated poolMinSize: Configuration.getConfigurationData()?.workerPoolMinSize, }), - ...(hasOwnProp(Configuration.getConfigurationData(), 'workerPoolMaxSize') && { + ...(has(Configuration.getConfigurationData(), 'workerPoolMaxSize') && { // eslint-disable-next-line @typescript-eslint/no-deprecated poolMaxSize: Configuration.getConfigurationData()?.workerPoolMaxSize, }), } - hasOwnProp(Configuration.getConfigurationData(), 'workerPoolStrategy') && + has(Configuration.getConfigurationData(), 'workerPoolStrategy') && // eslint-disable-next-line @typescript-eslint/no-deprecated delete Configuration.getConfigurationData()?.workerPoolStrategy const workerConfiguration: WorkerConfiguration = { ...defaultWorkerConfiguration, ...deprecatedWorkerConfiguration, - ...(hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.worker) && + ...(has(Configuration.getConfigurationData(), ConfigurationSection.worker) && Configuration.getConfigurationData()?.worker), } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -492,7 +493,7 @@ export class Configuration { "Use 'uri' instead" ) // uiServer section - if (hasOwnProp(Configuration.getConfigurationData(), 'uiWebSocketServer')) { + if (has(Configuration.getConfigurationData(), 'uiWebSocketServer')) { console.error( `${chalk.green(logPrefix())} ${chalk.red( `Deprecated configuration section 'uiWebSocketServer' usage. Use '${ConfigurationSection.uiServer}' instead` @@ -590,7 +591,7 @@ export class Configuration { } public static getSupervisionUrlDistribution (): SupervisionUrlDistribution | undefined { - return hasOwnProp(Configuration.getConfigurationData(), 'supervisionUrlDistribution') + return has(Configuration.getConfigurationData(), 'supervisionUrlDistribution') ? Configuration.getConfigurationData()?.supervisionUrlDistribution : SupervisionUrlDistribution.ROUND_ROBIN } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 56b528a6..6acb470b 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -221,14 +221,6 @@ export const isAsyncFunction = (fn: unknown): fn is AsyncFunctionType {}).constructor } -export const isObject = (value: unknown): value is object => { - return value != null && !Array.isArray(value) && is(Object, value) -} - -export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => { - return isObject(value) && Object.hasOwn(value, property) -} - export const isCFEnvironment = (): boolean => { return env.VCAP_APPLICATION != null } diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 4f9e9102..e1e7da25 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -21,13 +21,11 @@ import { formatDurationSeconds, generateUUID, getRandomFloat, - hasOwnProp, insertAt, isArraySorted, isAsyncFunction, isNotEmptyArray, isNotEmptyString, - isObject, isValidDate, roundTo, secureRandom, @@ -207,23 +205,6 @@ await describe('Utils test suite', async () => { expect(extractTimeSeriesValues(circularBuffer)).toEqual([1.1, 2.2, 3.3]) }) - await it('Verify isObject()', () => { - expect(isObject('test')).toBe(false) - expect(isObject(undefined)).toBe(false) - expect(isObject(null)).toBe(false) - expect(isObject(0)).toBe(false) - expect(isObject([])).toBe(false) - expect(isObject([0, 1])).toBe(false) - expect(isObject(['0', '1'])).toBe(false) - expect(isObject({})).toBe(true) - expect(isObject({ 1: 1 })).toBe(true) - expect(isObject({ 1: '1' })).toBe(true) - expect(isObject(new Map())).toBe(true) - expect(isObject(new Set())).toBe(true) - expect(isObject(new WeakMap())).toBe(true) - expect(isObject(new WeakSet())).toBe(true) - }) - await it('Verify isAsyncFunction()', () => { expect(isAsyncFunction(null)).toBe(false) expect(isAsyncFunction(undefined)).toBe(false) @@ -331,22 +312,6 @@ await describe('Utils test suite', async () => { expect(() => clone(weakSet)).toThrowError(new Error('# could not be cloned.')) }) - await it('Verify hasOwnProp()', () => { - expect(hasOwnProp('test', '')).toBe(false) - expect(hasOwnProp(undefined, '')).toBe(false) - expect(hasOwnProp(null, '')).toBe(false) - expect(hasOwnProp([], '')).toBe(false) - expect(hasOwnProp({}, '')).toBe(false) - expect(hasOwnProp({ 1: 1 }, 1)).toBe(true) - expect(hasOwnProp({ 1: 1 }, '1')).toBe(true) - expect(hasOwnProp({ 1: 1 }, 2)).toBe(false) - expect(hasOwnProp({ 1: 1 }, '2')).toBe(false) - expect(hasOwnProp({ 1: '1' }, '1')).toBe(true) - expect(hasOwnProp({ 1: '1' }, 1)).toBe(true) - expect(hasOwnProp({ 1: '1' }, '2')).toBe(false) - expect(hasOwnProp({ 1: '1' }, 2)).toBe(false) - }) - await it('Verify isNotEmptyString()', () => { expect(isNotEmptyString('')).toBe(false) expect(isNotEmptyString(' ')).toBe(false) -- 2.34.1