reconstruct
This commit is contained in:
parent
20002030ad
commit
ca8bded949
17 changed files with 1268 additions and 1110 deletions
77
services/retail_metrics.ts
Normal file
77
services/retail_metrics.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
export function purchaseIndex(totalItemsSold: number, numberOfCustomers: number): number {
|
||||
if (numberOfCustomers === 0) {
|
||||
throw new Error('Number of customers cannot be zero');
|
||||
}
|
||||
return (totalItemsSold / numberOfCustomers) * 1000;
|
||||
}
|
||||
|
||||
export function purchaseRate(productPurchases: number, totalTransactions: number): number;
|
||||
export function purchaseRate(productPurchases: number[], totalTransactions: number[]): number[];
|
||||
export function purchaseRate(productPurchases: number | number[], totalTransactions: number | number[]): number | number[] {
|
||||
if (Array.isArray(productPurchases) && Array.isArray(totalTransactions)) {
|
||||
if (productPurchases.length !== totalTransactions.length) throw new Error('Arrays must have the same length');
|
||||
return productPurchases.map((pp, i) => purchaseRate(pp, totalTransactions[i]));
|
||||
}
|
||||
if (typeof productPurchases === 'number' && typeof totalTransactions === 'number') {
|
||||
if (totalTransactions === 0) throw new Error('Total transactions cannot be zero');
|
||||
return (productPurchases / totalTransactions) * 100;
|
||||
}
|
||||
throw new Error('Input types must match');
|
||||
}
|
||||
|
||||
export function liftValue(jointPurchaseRate: number, productAPurchaseRate: number, productBPurchaseRate: number): number;
|
||||
export function liftValue(jointPurchaseRate: number[], productAPurchaseRate: number[], productBPurchaseRate: number[]): number[];
|
||||
export function liftValue(jointPurchaseRate: number | number[], productAPurchaseRate: number | number[], productBPurchaseRate: number | number[]): number | number[] {
|
||||
if (Array.isArray(jointPurchaseRate) && Array.isArray(productAPurchaseRate) && Array.isArray(productBPurchaseRate)) {
|
||||
if (jointPurchaseRate.length !== productAPurchaseRate.length || jointPurchaseRate.length !== productBPurchaseRate.length) throw new Error('Arrays must have the same length');
|
||||
return jointPurchaseRate.map((jpr, i) => liftValue(jpr, productAPurchaseRate[i], productBPurchaseRate[i]));
|
||||
}
|
||||
if (typeof jointPurchaseRate === 'number' && typeof productAPurchaseRate === 'number' && typeof productBPurchaseRate === 'number') {
|
||||
const expectedJointRate = productAPurchaseRate * productBPurchaseRate;
|
||||
if (expectedJointRate === 0) throw new Error('Expected joint rate cannot be zero');
|
||||
return jointPurchaseRate / expectedJointRate;
|
||||
}
|
||||
throw new Error('Input types must match');
|
||||
}
|
||||
|
||||
export function costRatio(cost: number, salePrice: number): number;
|
||||
export function costRatio(cost: number[], salePrice: number[]): number[];
|
||||
export function costRatio(cost: number | number[], salePrice: number | number[]): number | number[] {
|
||||
if (Array.isArray(cost) && Array.isArray(salePrice)) {
|
||||
if (cost.length !== salePrice.length) throw new Error('Arrays must have the same length');
|
||||
return cost.map((c, i) => costRatio(c, salePrice[i]));
|
||||
}
|
||||
if (typeof cost === 'number' && typeof salePrice === 'number') {
|
||||
if (salePrice === 0) throw new Error('Sale price cannot be zero');
|
||||
return cost / salePrice;
|
||||
}
|
||||
throw new Error('Input types must match');
|
||||
}
|
||||
|
||||
export function grossMarginRate(salePrice: number, cost: number): number;
|
||||
export function grossMarginRate(salePrice: number[], cost: number[]): number[];
|
||||
export function grossMarginRate(salePrice: number | number[], cost: number | number[]): number | number[] {
|
||||
if (Array.isArray(salePrice) && Array.isArray(cost)) {
|
||||
if (salePrice.length !== cost.length) throw new Error('Arrays must have the same length');
|
||||
return salePrice.map((sp, i) => grossMarginRate(sp, cost[i]));
|
||||
}
|
||||
if (typeof salePrice === 'number' && typeof cost === 'number') {
|
||||
if (salePrice === 0) throw new Error('Sale price cannot be zero');
|
||||
return (salePrice - cost) / salePrice;
|
||||
}
|
||||
throw new Error('Input types must match');
|
||||
}
|
||||
|
||||
export function averageSpendPerCustomer(totalRevenue: number, numberOfCustomers: number): number;
|
||||
export function averageSpendPerCustomer(totalRevenue: number[], numberOfCustomers: number[]): number[];
|
||||
export function averageSpendPerCustomer(totalRevenue: number | number[], numberOfCustomers: number | number[]): number | number[] {
|
||||
if (Array.isArray(totalRevenue) && Array.isArray(numberOfCustomers)) {
|
||||
if (totalRevenue.length !== numberOfCustomers.length) throw new Error('Arrays must have the same length');
|
||||
return totalRevenue.map((tr, i) => averageSpendPerCustomer(tr, numberOfCustomers[i]));
|
||||
}
|
||||
if (typeof totalRevenue === 'number' && typeof numberOfCustomers === 'number') {
|
||||
if (numberOfCustomers === 0) throw new Error('Number of customers cannot be zero');
|
||||
return totalRevenue / numberOfCustomers;
|
||||
}
|
||||
throw new Error('Input types must match');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue