2024-06-27 16:06:39 +02:00
|
|
|
import {length as en_length, area as en_area} from "enheter"
|
|
|
|
import {Measure, LengthUnit, AreaUnit, } from "enheter"
|
2024-06-27 23:31:59 +02:00
|
|
|
import uuid from "react-native-uuid";
|
2024-06-27 16:06:39 +02:00
|
|
|
export type Id = string;
|
|
|
|
|
|
|
|
export type Currency = "USD";
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
export const CURRENCY_SYMBOLS : Map<"USD", string> = {
|
|
|
|
"USD": "$",
|
|
|
|
};
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
export class Price {
|
|
|
|
constructor(public currency : Currency, public value : number) {}
|
|
|
|
public toString() {
|
|
|
|
const sym = CURRENCY_SYMBOLS.get(this.currency);
|
|
|
|
return `${sym} ${Math.round(this.value)}`;
|
|
|
|
}
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export type ProductAttributes = {
|
2024-06-27 23:31:59 +02:00
|
|
|
id?: string,
|
2024-06-27 16:06:39 +02:00
|
|
|
name?: string,
|
|
|
|
image?: string,
|
|
|
|
description?: string,
|
|
|
|
depth?: string,
|
2024-06-27 23:31:59 +02:00
|
|
|
currency?: Currency,
|
2024-06-27 16:06:39 +02:00
|
|
|
[key:string]: any,
|
|
|
|
}
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
export class Product {
|
|
|
|
public id : string;
|
|
|
|
constructor(public pricePerUnit : number, public measurement : Measure<"length" | "area">, public attributes : ProductAttributes = {}) {
|
|
|
|
this.id = attributes.id || uuid.v4().toString();
|
|
|
|
}
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
public priceFor(measurement : Measure<"length" | "area">): number {
|
|
|
|
const ratio = measurement.convertTo(this.measurement.unit).divideBy(this.measurement).value;
|
|
|
|
return this.pricePerUnit * ratio
|
|
|
|
}
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
get isArea() {
|
|
|
|
return this.measurement.unit.dimension.length === 2;
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
get isLength() {
|
|
|
|
return this.measurement.unit.dimension.length === 1;
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
get attributesAsList() {
|
|
|
|
return Object.entries(this.attributes).map(([key, value]) => {
|
|
|
|
return {key, value}
|
|
|
|
})
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
public removeAttribute(key : string) {
|
|
|
|
delete this.attributes[key]
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
}
|