@zeyolabs/shopping 2026.11.10 → 2026.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -2
- package/stock.ts +43 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeyolabs/shopping",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.11",
|
|
4
4
|
"description": "Shop and order fulfilment for SvelteKit 3 on Cloudflare: catalogue, basket, Stripe Checkout, orders, Royal Mail Click & Drop, order emails, packing, pages and admin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"packing.ts",
|
|
35
35
|
"royalmail.ts",
|
|
36
36
|
"shipping-rules.ts",
|
|
37
|
+
"stock.ts",
|
|
37
38
|
"!**/*.test.ts"
|
|
38
39
|
],
|
|
39
40
|
"scripts": {
|
|
@@ -57,7 +58,8 @@
|
|
|
57
58
|
"./remote/*": "./remote/*.ts",
|
|
58
59
|
"./royalmail": "./royalmail.ts",
|
|
59
60
|
"./routes/*": "./routes/*.ts",
|
|
60
|
-
"./server/*": "./server/*.ts"
|
|
61
|
+
"./server/*": "./server/*.ts",
|
|
62
|
+
"./stock": "./stock.ts"
|
|
61
63
|
},
|
|
62
64
|
"dependencies": {
|
|
63
65
|
"@lucide/svelte": "1.52.0",
|
package/stock.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Stock arithmetic, pure and framework-free (a library: an object of static functions, no db). Stock
|
|
2
|
+
// is held in single units; a product sold in packs has `packSize` above 1 and sells in whole packs.
|
|
3
|
+
|
|
4
|
+
/** One component of a combined product: `quantity` of it goes into each set. */
|
|
5
|
+
export type StockComponent = StockLine & { quantity: number };
|
|
6
|
+
export type StockLine = { packSize: number; stock: number };
|
|
7
|
+
|
|
8
|
+
/** What the badge on a product card should say. */
|
|
9
|
+
export type StockBadge = { count: number; kind: 'low' } | { kind: 'in' } | { kind: 'out' };
|
|
10
|
+
|
|
11
|
+
export const Stock = {
|
|
12
|
+
/** What the card shows: out of stock at 0, "only N left" at or below the threshold, else in stock. */
|
|
13
|
+
badge(sellable: number, lowStockThreshold: null | number | undefined): StockBadge {
|
|
14
|
+
if (sellable <= 0) return { kind: 'out' };
|
|
15
|
+
if (lowStockThreshold != null && sellable <= lowStockThreshold) {
|
|
16
|
+
return { count: sellable, kind: 'low' };
|
|
17
|
+
}
|
|
18
|
+
return { kind: 'in' };
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Complete sets a combined product can be sold as: the fewest the components allow, each
|
|
23
|
+
* component's whole packs divided by the quantity one set takes. No components: 0.
|
|
24
|
+
*/
|
|
25
|
+
combinedAvailable(components: readonly StockComponent[]): number {
|
|
26
|
+
if (components.length === 0) return 0;
|
|
27
|
+
return Math.min(
|
|
28
|
+
...components.map((c) =>
|
|
29
|
+
Math.floor(Stock.sellable(c.stock, c.packSize) / Math.max(1, c.quantity))
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/** Total sellable across the variations of one group, for listing pages. Never stored. */
|
|
35
|
+
groupTotal(variants: readonly StockLine[]): number {
|
|
36
|
+
return variants.reduce((sum, v) => sum + Stock.sellable(v.stock, v.packSize), 0);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
/** Whole packs available. A negative stock or a pack size below 1 is treated as empty / single. */
|
|
40
|
+
sellable(stock: number, packSize = 1): number {
|
|
41
|
+
return Math.floor(Math.max(0, stock) / Math.max(1, packSize));
|
|
42
|
+
},
|
|
43
|
+
};
|