I'm looking to optimize this TypeScript code by removing the unnecessary as
keywords. The code includes a method called update
where I'm attempting to improve the organization of sanitizing, validating, and attributing data. However, it seems that my approach is not working with TypeScript as expected. I need some assistance in implementing this more efficiently while keeping the structure of the code intact.
Here's the current code snippet:
export class AccessProfile {
id!: number;
active!: boolean;
name!: string;
createdAt!: Date;
createdBy!: User | null;
createdFrom!: SystemModule;
updatedAt!: Date;
updatedBy!: User | null;
updatedFrom!: SystemModule;
hierarchy!: number | null;
permissions!: Set<string>;
public update(
{
updatedFrom,
updatedBy = null,
name,
active,
hierarchy,
permissions
}: {
updatedFrom: SystemModule,
updatedBy?: DiphycUser | null,
name?: string,
active?: boolean,
hierarchy?: number | null,
permissions?: Set<string>,
}
) {
const changedValues = new Set();
if (name !== undefined) {
name = AccessProfile.sanitizeName(name);
if (this.name !== name) {
AccessProfile.validateName(name);
changedValues.add("name");
}
}
if (active !== undefined && this.active !== active) {
changedValues.add("active");
}
if (hierarchy !== undefined) {
hierarchy = AccessProfile.sanitizeHierarchy(hierarchy);
if (this.hierarchy !== hierarchy) {
AccessProfile.validateHierarchy(hierarchy);
changedValues.add("hierarchy");
}
}
if (
permissions !== undefined
&& !permissionsAreEqual(permissions, this.permissions)
) {
changedValues.add("permissions");
}
if (changedValues.has("hierarchy") && changedValues.has("permissions")) {
AccessProfile.validateHierarchyAndPermissions({
hierarchy: hierarchy as number | null,
permissions: permissions as Set<string>
});
} else if (changedValues.has("hierarchy")) {
AccessProfile.validateHierarchyAndPermissions({
hierarchy: hierarchy as number | null,
permissions: this.permissions
});
} else if (changedValues.has("permissions")) {
AccessProfile.validateHierarchyAndPermissions({
hierarchy: this.hierarchy,
permissions: permissions as Set<string>
});
}
this.updatedFrom = updatedFrom;
this.updatedBy = updatedBy;
if (changedValues.has("name")) this.name = name as string;
if (changedValues.has("active")) this.active = active as boolean;
if (changedValues.has("hierarchy")) this.hierarchy = hierarchy as number | null;
if (changedValues.has("permissions")) this.permissions = permissions as Set<string>;
this.updatedAt = new Date();
return this;
}
}