46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import type { IValueObject } from '@gridpilot/shared/domain';
|
|
|
|
export interface PointsTableProps {
|
|
pointsByPosition: Map<number, number>;
|
|
}
|
|
|
|
export class PointsTable implements IValueObject<PointsTableProps> {
|
|
private readonly pointsByPosition: Map<number, number>;
|
|
|
|
constructor(pointsByPosition: Record<number, number> | Map<number, number>) {
|
|
if (pointsByPosition instanceof Map) {
|
|
this.pointsByPosition = new Map(pointsByPosition);
|
|
} else {
|
|
this.pointsByPosition = new Map(
|
|
Object.entries(pointsByPosition).map(([key, value]) => [Number(key), value]),
|
|
);
|
|
}
|
|
}
|
|
|
|
getPointsForPosition(position: number): number {
|
|
if (!Number.isInteger(position) || position < 1) {
|
|
return 0;
|
|
}
|
|
const value = this.pointsByPosition.get(position);
|
|
return typeof value === 'number' ? value : 0;
|
|
}
|
|
|
|
get props(): PointsTableProps {
|
|
return {
|
|
pointsByPosition: new Map(this.pointsByPosition),
|
|
};
|
|
}
|
|
|
|
equals(other: IValueObject<PointsTableProps>): boolean {
|
|
const a = this.props.pointsByPosition;
|
|
const b = other.props.pointsByPosition;
|
|
|
|
if (a.size !== b.size) return false;
|
|
for (const [key, value] of a.entries()) {
|
|
if (b.get(key) !== value) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} |