Merge branch 'develop-product-as-type' into develop
This commit is contained in:
commit
23d957824b
111
lib/product.ts
111
lib/product.ts
@ -1,6 +1,4 @@
|
||||
import uuid from "react-native-uuid";
|
||||
import { Area } from "convert";
|
||||
import { Transform } from "class-transformer";
|
||||
import { dimensions_t, area_t } from "./dimensions";
|
||||
import { matchDimensions } from "./dimensions";
|
||||
|
||||
@ -15,85 +13,66 @@ export type ProductAttributes = {
|
||||
description?: string,
|
||||
depth?: string,
|
||||
currency?: Currency,
|
||||
// [index:string]: any,
|
||||
[index:string]: any,
|
||||
}
|
||||
export function dimensionArea(d: dimensions_t) {
|
||||
return "w" in d ? d.w * d.l : 0;
|
||||
}
|
||||
|
||||
export type ProductData = {
|
||||
export type product_type_t = "lumber" | "area_rug"
|
||||
|
||||
export type Product = {
|
||||
id?: Id;
|
||||
pricePerUnit: number;
|
||||
dimensions: dimensions_t;
|
||||
type: product_type_t;
|
||||
attributes?: ProductAttributes;
|
||||
};
|
||||
|
||||
export type LumberProduct = Product & {
|
||||
type: "lumber"
|
||||
}
|
||||
|
||||
export class Product {
|
||||
export type AreaRugProduct = Product & {
|
||||
type: "lumber"
|
||||
}
|
||||
|
||||
public id?: Id;
|
||||
export function productPriceFor(product : Product, dimensions: dimensions_t, damage: number): number {
|
||||
if (Number.isNaN(damage)) damage = 0;
|
||||
const dim = matchDimensions(dimensions, product.dimensions);
|
||||
return (
|
||||
dim.w ? dimensionArea(dim) / dimensionArea(product.dimensions) * product.pricePerUnit
|
||||
: (dim.l / product.dimensions.l) * product.pricePerUnit
|
||||
) * (1.0 - damage);
|
||||
}
|
||||
|
||||
constructor(public pricePerUnit: number, public dimensions: dimensions_t, public attributes: ProductAttributes = {},
|
||||
id?: Id,
|
||||
) {
|
||||
this.id = id || uuid.v4().toString();
|
||||
}
|
||||
export function priceDisplay(price : number) {
|
||||
return price.toLocaleString(undefined, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
})
|
||||
}
|
||||
|
||||
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() {
|
||||
return this.pricePerUnit.toLocaleString(undefined, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
})
|
||||
}
|
||||
|
||||
get pricePerUnitDisplay() {
|
||||
const p = this.priceDisplay;
|
||||
const { l, u } = this.dimensions;
|
||||
const w = (this.dimensions as area_t).w || null;
|
||||
export function pricePerUnitDisplay(product : Product) {
|
||||
const p = priceDisplay(product.pricePerUnit);
|
||||
const { l, u } = product.dimensions;
|
||||
const w = (product.dimensions as area_t).w || null;
|
||||
const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`;
|
||||
return `$${p} per ${d}`
|
||||
}
|
||||
|
||||
get attributesAsList() {
|
||||
return Object.entries(this.attributes).map(([key, value]) => {
|
||||
return { key, value }
|
||||
})
|
||||
}
|
||||
|
||||
public removeAttribute(key: string) {
|
||||
this.attributes = Object.fromEntries(
|
||||
Object.entries(this.attributes).filter(
|
||||
([k, v]) => {
|
||||
k == key;
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
get asObject(): ProductData {
|
||||
return {
|
||||
id: this.id,
|
||||
pricePerUnit: this.pricePerUnit,
|
||||
dimensions: this.dimensions,
|
||||
attributes: this.attributes,
|
||||
}
|
||||
}
|
||||
|
||||
static fromObject({ id, pricePerUnit, dimensions, attributes }: ProductData) {
|
||||
return new Product(
|
||||
pricePerUnit,
|
||||
dimensions,
|
||||
attributes,
|
||||
id,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function attributesAsList(attributes : {[key:string]: any}) {
|
||||
return Object.entries(attributes).map(([key, value]) => {
|
||||
return { key, value }
|
||||
})
|
||||
}
|
||||
|
||||
export function removeAttribute(attributes: {[key: string]: any}, key: string) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(attributes).filter(
|
||||
([k, v]) => {
|
||||
k == key;
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user