cloneObject,
convertToDate,
convertToInt,
+ isArraySorted,
isEmptyObject,
isEmptyString,
isNotEmptyArray,
})
) {
if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
- chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod);
+ const chargingSchedulePeriodCompareFn = (
+ a: ChargingSchedulePeriod,
+ b: ChargingSchedulePeriod,
+ ) => a.startPeriod - b.startPeriod;
+ if (
+ isArraySorted<ChargingSchedulePeriod>(
+ chargingSchedule.chargingSchedulePeriod,
+ chargingSchedulePeriodCompareFn,
+ ) === false
+ ) {
+ logger.warn(
+ `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} schedule periods are not sorted by start period`,
+ );
+ chargingSchedule.chargingSchedulePeriod.sort(chargingSchedulePeriodCompareFn);
+ }
// Check if the first schedule period start period is equal to 0
if (chargingSchedule.chargingSchedulePeriod[0].startPeriod !== 0) {
logger.error(
}
return '(Unknown)';
};
+
+export const isArraySorted = <T>(elements: T[], compareFn: (a: T, b: T) => number): boolean => {
+ for (let i = 0; i < elements.length - 1; ++i) {
+ if (compareFn(elements[i], elements[i + 1]) > 0) {
+ return false;
+ }
+ }
+ return true;
+};
getRandomFloatRounded,
getRandomInteger,
getWebSocketCloseEventStatusString,
+ isArraySorted,
isEmptyArray,
isEmptyObject,
isEmptyString,
getRandomFloat,
getRandomInteger,
hasOwnProp,
+ isArraySorted,
isEmptyArray,
isEmptyObject,
isEmptyString,
expect(isEmptyObject(new WeakMap())).toBe(false);
expect(isEmptyObject(new WeakSet())).toBe(false);
});
+
+ it('Verify isArraySorted()', () => {
+ expect(
+ isArraySorted([], (a, b) => {
+ return a - b;
+ }),
+ ).toBe(true);
+ expect(
+ isArraySorted([1], (a, b) => {
+ return a - b;
+ }),
+ ).toBe(true);
+ expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
+ expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
+ });
});