mirror of
https://github.com/ANL-CEEESA/UnitCommitment.jl.git
synced 2025-12-08 01:08:50 -06:00
web: ProfiledUnits: Rename and delete
This commit is contained in:
39
web/src/core/Operations/commonOps.ts
Normal file
39
web/src/core/Operations/commonOps.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
|
||||
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
|
||||
* Released under the modified BSD license. See COPYING.md for more details.
|
||||
*/
|
||||
|
||||
import { ValidationError } from "../Validation/validate";
|
||||
|
||||
export const renameItemInObject = <T>(
|
||||
oldName: string,
|
||||
newName: string,
|
||||
container: { [key: string]: T },
|
||||
): [{ [key: string]: T }, ValidationError | null] => {
|
||||
if (newName in container) {
|
||||
return [container, { message: `${newName} already exists` }];
|
||||
}
|
||||
const newContainer = Object.keys(container).reduce(
|
||||
(acc, val) => {
|
||||
if (val === oldName) {
|
||||
acc[newName] = container[val]!;
|
||||
} else {
|
||||
acc[val] = container[val]!;
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{} as { [key: string]: T },
|
||||
);
|
||||
return [newContainer, null];
|
||||
};
|
||||
|
||||
export const generateUniqueName = (container: any, prefix: string): string => {
|
||||
let counter = 1;
|
||||
let name = `${prefix}${counter}`;
|
||||
while (name in container) {
|
||||
counter++;
|
||||
name = `${prefix}${counter}`;
|
||||
}
|
||||
return name;
|
||||
};
|
||||
Reference in New Issue
Block a user