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'
logger,
logPrefix,
min,
- once,
roundTo,
secureRandom,
sleep,
// 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)
toDate
} from 'date-fns'
import { maxTime } from 'date-fns/constants'
+import { isEmpty } from 'rambda'
import { BaseError } from '../exception/index.js'
import {
convertToInt,
DCElectricUtils,
isArraySorted,
- isEmptyObject,
- isEmptyString,
isNotEmptyArray,
isNotEmptyString,
isValidDate,
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`
)
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)
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 {
chargingStationConfiguration.automaticTransactionGenerator != null &&
chargingStationConfiguration.configurationHash != null &&
isNotEmptyArray(chargingStationConfiguration.configurationKey) &&
- !isEmptyObject(chargingStationConfiguration.stationInfo) &&
- !isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator) &&
+ !isEmpty(chargingStationConfiguration.stationInfo) &&
+ !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) &&
isNotEmptyString(chargingStationConfiguration.configurationHash)
)
}
import { secondsToMilliseconds } from 'date-fns'
+import { isEmpty } from 'rambda'
import { BaseError, type OCPPError } from '../../exception/index.js'
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'
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
return ResponseStatus.FAILURE
case BroadcastChannelProcedureName.STATUS_NOTIFICATION:
case BroadcastChannelProcedureName.METER_VALUES:
- if (isEmptyObject(commandResponse)) {
+ if (isEmpty(commandResponse)) {
return ResponseStatus.SUCCESS
}
return ResponseStatus.FAILURE
secondsToMilliseconds
} from 'date-fns'
import { maxTime } from 'date-fns/constants'
+import { isEmpty } from 'rambda'
import { create } from 'tar'
import {
formatDurationMilliSeconds,
getRandomInteger,
isAsyncFunction,
- isEmptyArray,
isNotEmptyArray,
isNotEmptyString,
logger,
}
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
}
import { fileURLToPath } from 'node:url'
import chalk from 'chalk'
-import { mergeDeepRight } from 'rambda'
+import { mergeDeepRight, once } from 'rambda'
import {
ApplicationProtocol,
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
public static getStationTemplateUrls (): StationTemplateUrl[] | undefined {
const checkDeprecatedConfigurationKeysOnce = once(
- Configuration.checkDeprecatedConfigurationKeys.bind(Configuration),
- Configuration
+ Configuration.checkDeprecatedConfigurationKeys.bind(Configuration)
)
checkDeprecatedConfigurationKeysOnce()
return Configuration.getConfigurationData()?.stationTemplateUrls
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`
} from 'date-fns'
import {
- type EmptyObject,
type JsonType,
MapStringifyFormat,
type TimestampedData,
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)
}
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
}
return true
}
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-export const once = <T, A extends any[], R>(
- 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<T, A, R>(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)
getWebSocketCloseEventStatusString,
isArraySorted,
isAsyncFunction,
- isEmptyArray,
- isEmptyObject,
- isEmptyString,
isNotEmptyArray,
isNotEmptyString,
isValidDate,
logPrefix,
max,
min,
- once,
roundTo,
secureRandom,
sleep,
hasOwnProp,
isArraySorted,
isAsyncFunction,
- isEmptyArray,
- isEmptyObject,
- isEmptyString,
isNotEmptyArray,
isNotEmptyString,
isObject,
isValidDate,
max,
min,
- once,
roundTo,
secureRandom,
sleep,
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)
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)
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) => {
expect(isArraySorted<number>([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)