good enough for government (or habitat) work

This commit is contained in:
Jordan
2024-07-01 12:23:45 -07:00
parent 379f43dcd9
commit ecdc9db085
18 changed files with 325 additions and 169 deletions

View File

@ -66,7 +66,7 @@ export function dimensionArea(d: dimensions_t) {
export class Product {
public id? : Id;
public id?: Id;
constructor(public pricePerUnit: number, public dimensions: dimensions_t, public attributes: ProductAttributes = {},
id?: Id,
@ -74,12 +74,13 @@ export class Product {
this.id = id || uuid.v4().toString();
}
public priceFor(dimensions: dimensions_t): number {
public priceFor(dimensions: dimensions_t, damage : number): number {
if (Number.isNaN(damage)) damage = 0;
const dim = matchDimensions(dimensions, this.dimensions);
return (
dim.w ? dimensionArea(dim) / dimensionArea(this.dimensions) * this.pricePerUnit
: (dim.l / this.dimensions.l) * this.pricePerUnit
)
) * (1.0 - damage);
}
get priceDisplay() {
@ -91,9 +92,9 @@ export class Product {
get pricePerUnitDisplay() {
const p = this.priceDisplay;
const {l, u} = this.dimensions;
const { l, u } = this.dimensions;
const w = (this.dimensions as area_t).w || null;
const d = w ? `${l}${u} x ${l}${u}` : `${l}${u}`;
const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`;
return `$${p} per ${d}`
}
@ -113,16 +114,16 @@ export class Product {
);
}
get asObject() : ProductData {
get asObject(): ProductData {
return {
id: this.id,
pricePerUnit: this.pricePerUnit,
dimensions: JSON.parse(JSON.stringify(this.dimensions)),
dimensions: this.dimensions,
attributes: this.attributes,
}
}
static fromObject({id, pricePerUnit, dimensions, attributes} : ProductData) {
static fromObject({ id, pricePerUnit, dimensions, attributes }: ProductData) {
return new Product(
pricePerUnit,
dimensions,