import {length as en_length, area as en_area} from "enheter" import {Measure, LengthUnit, AreaUnit, } from "enheter" import uuid from "react-native-uuid"; export type Id = string; export type Currency = "USD"; export const CURRENCY_SYMBOLS : Map<"USD", string> = { "USD": "$", }; 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)}`; } } export type ProductAttributes = { id?: string, name?: string, image?: string, description?: string, depth?: string, currency?: Currency, [key:string]: any, } 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(); } public priceFor(measurement : Measure<"length" | "area">): number { const ratio = measurement.convertTo(this.measurement.unit).divideBy(this.measurement).value; return this.pricePerUnit * ratio } get isArea() { return this.measurement.unit.dimension.length === 2; } get isLength() { return this.measurement.unit.dimension.length === 1; } get attributesAsList() { return Object.entries(this.attributes).map(([key, value]) => { return {key, value} }) } public removeAttribute(key : string) { delete this.attributes[key] } }