feat(shopping): mağazalar, zahmet skoru ve ürünler (marka/paket, dönüşüm, görseller)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { convertQuantity, formatQuantity, UNITS, type Unit } from "./quantity";
|
||||
|
||||
/**
|
||||
* Ürün (marka/varyant) motoru (Bölüm 6.11). Bir paketin ürün tipinin birimindeki karşılığı:
|
||||
* - paket birimi ürün tipinin birimiyle aynıysa paket miktarı,
|
||||
* - aynı boyuttaysa (kg↔g, lt↔ml) çevrilmiş miktar,
|
||||
* - değilse elle girilen dönüşüm ("5 kg = 50 yıkama", "1 paket = 40 tablet"); yoksa null
|
||||
* (kayıtta zorunlu; ürün tipinin birimi sonradan değişirse "dönüşüm gerekli" uyarısı).
|
||||
* Miktarlar binde bir.
|
||||
*/
|
||||
|
||||
const isStandard = (u: string): u is Unit => Object.hasOwn(UNITS, u);
|
||||
|
||||
/** Paket birimi ürün tipinin birimine kendiliğinden çevrilebiliyor mu? */
|
||||
export function autoConvertible(packageUnit: string, typeUnit: string): boolean {
|
||||
if (packageUnit === typeUnit) return true;
|
||||
return (
|
||||
isStandard(packageUnit) &&
|
||||
isStandard(typeUnit) &&
|
||||
UNITS[packageUnit].dimension === UNITS[typeUnit].dimension
|
||||
);
|
||||
}
|
||||
|
||||
/** Bir paketin ürün tipinin birimindeki miktarı (çevrilemiyorsa null). */
|
||||
export function packageInTypeUnit(input: {
|
||||
packageQuantity: number;
|
||||
packageUnit: string;
|
||||
typeUnit: string;
|
||||
/** Elle girilen dönüşüm: bir paket = bu kadar ürün tipi birimi. */
|
||||
typeQuantity: number | null;
|
||||
}): number | null {
|
||||
const { packageQuantity, packageUnit, typeUnit } = input;
|
||||
if (packageUnit === typeUnit) return packageQuantity;
|
||||
if (autoConvertible(packageUnit, typeUnit))
|
||||
return convertQuantity(packageQuantity, packageUnit as Unit, typeUnit as Unit);
|
||||
return input.typeQuantity && input.typeQuantity > 0 ? input.typeQuantity : null;
|
||||
}
|
||||
|
||||
/** "5 kg", dönüşüm varsa "5 kg · 50 yıkama". */
|
||||
export function describePackage(input: {
|
||||
packageQuantity: number;
|
||||
packageUnit: string;
|
||||
typeUnit: string;
|
||||
typeQuantity: number | null;
|
||||
}): string {
|
||||
const pkg = formatQuantity(input.packageQuantity, input.packageUnit);
|
||||
if (input.packageUnit === input.typeUnit) return pkg;
|
||||
const inType = packageInTypeUnit(input);
|
||||
return inType === null ? pkg : `${pkg} · ${formatQuantity(inType, input.typeUnit)}`;
|
||||
}
|
||||
|
||||
/** Barkod: 8–14 rakam (EAN-8, UPC-A, EAN-13, GTIN-14). */
|
||||
export const BARCODE_PATTERN = /^\d{8,14}$/;
|
||||
Reference in New Issue
Block a user