@xallindrome-labs/pricing-domain 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrew Wilson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,25 @@
1
- # Temporary Holding Version
1
+ # @xallindrome-labs/pricing-domain
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A small, dependency-free pricing model: a base package, per-unit overage and monthly add-ons. Pure functions, so it runs the same in a browser widget, a serverless function or a test. "Unit" is whatever you sell in quantity (posts, hours, seats, deliverables).
4
+
5
+ ```bash
6
+ npm install @xallindrome-labs/pricing-domain
7
+ ```
8
+
9
+ ```js
10
+ import { calculatePricing, EXAMPLE_PRICING_CONFIG } from "@xallindrome-labs/pricing-domain";
11
+
12
+ const result = calculatePricing(
13
+ { packageKey: "starter", premiumUnits: 2, baseUnits: 5, selectedAddOnKeys: ["monthly_review"] },
14
+ EXAMPLE_PRICING_CONFIG
15
+ );
16
+ console.log(result.totalMonthlyRevenue); // 830
17
+ ```
18
+
19
+ Also exported: `parsePricingConfig` / `parsePublicPricingConfig` (JSON string to config, `null` if malformed), `toPublicPricingConfigResponse` and `toPublicPricingEstimate` (shape output for visitors, without margin or overhead), and the TypeScript types. `EXAMPLE_PRICING_CONFIG` holds example numbers only; replace them with your own.
20
+
21
+ Part of [xallindrome-widgets](https://github.com/xallindrome-labs/xallindrome-widgets). MIT licensed.
22
+
23
+ ---
24
+
25
+ **Built by [Xallindrome](https://xallindrome.co)**: we build automation and AI workflows for small businesses. If you'd like something like this for your business, [book a diagnostic](https://xallindrome.co).
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Small, dependency-free pricing model: a base package plus per-unit overage
3
+ * and monthly add-ons. Pure functions only, so it runs in a browser widget,
4
+ * a serverless function or a test the same way.
5
+ *
6
+ * "Unit" is whatever you sell in quantity (posts, hours, seats, deliverables).
7
+ */
8
+ export type PricingPackage = {
9
+ key: string;
10
+ name: string;
11
+ /** Monthly price of the package. */
12
+ price: number;
13
+ /** Units included in the package. */
14
+ units: number;
15
+ };
16
+ export type PricingAddOn = {
17
+ key: string;
18
+ name: string;
19
+ monthlyPrice: number;
20
+ };
21
+ export type PricingConfig = {
22
+ /** Price of one extra "premium" unit beyond the package. */
23
+ premiumUnitPrice: number;
24
+ /** Price of one extra "base" unit beyond the package. */
25
+ baseUnitPrice: number;
26
+ /** Your fixed monthly cost, used only for the margin figure. Defaults to 0. */
27
+ fixedOverhead?: number;
28
+ packages: PricingPackage[];
29
+ addOns: PricingAddOn[];
30
+ };
31
+ export type PublicPricingPackage = PricingPackage & {
32
+ description?: string;
33
+ priceCue?: string;
34
+ };
35
+ export type PublicPricingConfig = {
36
+ premiumUnitPrice: number;
37
+ baseUnitPrice: number;
38
+ fixedOverhead?: number;
39
+ packages: PublicPricingPackage[];
40
+ addOns: PricingAddOn[];
41
+ disclaimer?: string;
42
+ ctaLabel?: string;
43
+ ctaUrl?: string;
44
+ premiumUnitLabel?: string;
45
+ baseUnitLabel?: string;
46
+ };
47
+ export type PricingCalculationInput = {
48
+ packageKey: string;
49
+ premiumUnits: number;
50
+ baseUnits: number;
51
+ selectedAddOnKeys: string[];
52
+ };
53
+ export type PricingCalculationResult = {
54
+ packageName: string;
55
+ basePrice: number;
56
+ packageUnits: number;
57
+ premiumUnits: number;
58
+ premiumRevenue: number;
59
+ baseUnits: number;
60
+ baseRevenue: number;
61
+ activeAddOns: PricingAddOn[];
62
+ addOnSubtotal: number;
63
+ extraUnits: number;
64
+ extraRevenue: number;
65
+ totalMonthlyRevenue: number;
66
+ totalUnits: number;
67
+ effectivePricePerUnit: number;
68
+ monthlyMargin: number;
69
+ };
70
+ export type PublicPricingConfigResponse = {
71
+ packages: Array<{
72
+ key: string;
73
+ name: string;
74
+ description?: string;
75
+ priceCue: string;
76
+ }>;
77
+ premiumUnitPrice: number;
78
+ baseUnitPrice: number;
79
+ premiumUnitLabel: string;
80
+ baseUnitLabel: string;
81
+ disclaimer: string;
82
+ ctaLabel: string;
83
+ ctaUrl: string;
84
+ };
85
+ export type PublicPricingEstimate = {
86
+ packageName: string;
87
+ premiumUnits: number;
88
+ baseUnits: number;
89
+ extraRevenue: number;
90
+ totalMonthlyRevenue: number;
91
+ };
92
+ /** Example numbers only. Replace them with your own pricing. */
93
+ export declare const EXAMPLE_PRICING_CONFIG: PricingConfig;
94
+ export declare const EXAMPLE_PUBLIC_PRICING_CONFIG: PublicPricingConfig;
95
+ /** Parses a JSON string into a PricingConfig. Returns null if it is empty or malformed. */
96
+ export declare function parsePricingConfig(value: string | undefined | null, fallback?: PricingConfig): PricingConfig | null;
97
+ /** Parses a JSON string into a PublicPricingConfig. Returns null if it is empty or malformed. */
98
+ export declare function parsePublicPricingConfig(value: string | undefined | null, fallback?: PublicPricingConfig): PublicPricingConfig | null;
99
+ export declare function calculatePricing(input: PricingCalculationInput, config: PricingConfig): PricingCalculationResult;
100
+ /** Shapes a config for public display: no overhead, formatted price cues and labels. */
101
+ export declare function toPublicPricingConfigResponse(config: PublicPricingConfig): PublicPricingConfigResponse;
102
+ /** Strips a calculation down to the fields that are safe to show a visitor (no margin, no overhead). */
103
+ export declare function toPublicPricingEstimate(result: PricingCalculationResult): PublicPricingEstimate;
package/dist/index.js ADDED
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Small, dependency-free pricing model: a base package plus per-unit overage
3
+ * and monthly add-ons. Pure functions only, so it runs in a browser widget,
4
+ * a serverless function or a test the same way.
5
+ *
6
+ * "Unit" is whatever you sell in quantity (posts, hours, seats, deliverables).
7
+ */
8
+ function formatCurrency(value) {
9
+ return new Intl.NumberFormat("en-US", {
10
+ style: "currency",
11
+ currency: "USD",
12
+ maximumFractionDigits: 0
13
+ }).format(value);
14
+ }
15
+ /** Example numbers only. Replace them with your own pricing. */
16
+ export const EXAMPLE_PRICING_CONFIG = {
17
+ premiumUnitPrice: 40,
18
+ baseUnitPrice: 20,
19
+ packages: [
20
+ { key: "starter", name: "Starter", price: 500, units: 10 },
21
+ { key: "growth", name: "Growth", price: 1200, units: 30 },
22
+ { key: "scale", name: "Scale", price: 2500, units: 80 },
23
+ { key: "none", name: "No Base Package", price: 0, units: 0 }
24
+ ],
25
+ addOns: [
26
+ { key: "monthly_review", name: "Monthly Review Call", monthlyPrice: 150 },
27
+ { key: "priority_support", name: "Priority Support", monthlyPrice: 100 }
28
+ ]
29
+ };
30
+ export const EXAMPLE_PUBLIC_PRICING_CONFIG = {
31
+ premiumUnitPrice: EXAMPLE_PRICING_CONFIG.premiumUnitPrice,
32
+ baseUnitPrice: EXAMPLE_PRICING_CONFIG.baseUnitPrice,
33
+ packages: EXAMPLE_PRICING_CONFIG.packages.map((pkg) => ({
34
+ ...pkg,
35
+ priceCue: pkg.price > 0 ? `Starts at ${formatCurrency(pkg.price)}/month` : "Custom add-ons only"
36
+ })),
37
+ addOns: EXAMPLE_PRICING_CONFIG.addOns,
38
+ disclaimer: "Estimates are provided for planning only and may vary based on scope and requirements.",
39
+ ctaLabel: "Book a call",
40
+ ctaUrl: "https://example.com/book",
41
+ premiumUnitLabel: `Premium units (${formatCurrency(EXAMPLE_PRICING_CONFIG.premiumUnitPrice)} each)`,
42
+ baseUnitLabel: `Base units (${formatCurrency(EXAMPLE_PRICING_CONFIG.baseUnitPrice)} each)`
43
+ };
44
+ function parsePackages(value) {
45
+ return value.map((item) => {
46
+ const pkg = item;
47
+ return {
48
+ key: String(pkg.key),
49
+ name: String(pkg.name),
50
+ price: Number(pkg.price),
51
+ units: Number(pkg.units)
52
+ };
53
+ });
54
+ }
55
+ function parseAddOns(value, fallback) {
56
+ if (!Array.isArray(value)) {
57
+ return fallback;
58
+ }
59
+ return value.map((item) => {
60
+ const addOn = item;
61
+ return {
62
+ key: String(addOn.key),
63
+ name: String(addOn.name),
64
+ monthlyPrice: Number(addOn.monthlyPrice)
65
+ };
66
+ });
67
+ }
68
+ /** Parses a JSON string into a PricingConfig. Returns null if it is empty or malformed. */
69
+ export function parsePricingConfig(value, fallback = EXAMPLE_PRICING_CONFIG) {
70
+ if (!value) {
71
+ return null;
72
+ }
73
+ try {
74
+ const parsed = JSON.parse(value);
75
+ if (!Array.isArray(parsed.packages)) {
76
+ return null;
77
+ }
78
+ return {
79
+ premiumUnitPrice: Number(parsed.premiumUnitPrice ?? fallback.premiumUnitPrice),
80
+ baseUnitPrice: Number(parsed.baseUnitPrice ?? fallback.baseUnitPrice),
81
+ fixedOverhead: Number(parsed.fixedOverhead ?? fallback.fixedOverhead ?? 0),
82
+ packages: parsePackages(parsed.packages),
83
+ addOns: parseAddOns(parsed.addOns, fallback.addOns)
84
+ };
85
+ }
86
+ catch {
87
+ return null;
88
+ }
89
+ }
90
+ /** Parses a JSON string into a PublicPricingConfig. Returns null if it is empty or malformed. */
91
+ export function parsePublicPricingConfig(value, fallback = EXAMPLE_PUBLIC_PRICING_CONFIG) {
92
+ if (!value) {
93
+ return null;
94
+ }
95
+ try {
96
+ const parsed = JSON.parse(value);
97
+ if (!Array.isArray(parsed.packages)) {
98
+ return null;
99
+ }
100
+ const str = (v, fb) => (typeof v === "string" ? v : fb);
101
+ return {
102
+ premiumUnitPrice: Number(parsed.premiumUnitPrice ?? fallback.premiumUnitPrice),
103
+ baseUnitPrice: Number(parsed.baseUnitPrice ?? fallback.baseUnitPrice),
104
+ fixedOverhead: Number(parsed.fixedOverhead ?? fallback.fixedOverhead ?? 0),
105
+ packages: parsed.packages.map((item) => {
106
+ const pkg = item;
107
+ return {
108
+ key: String(pkg.key),
109
+ name: String(pkg.name),
110
+ price: Number(pkg.price),
111
+ units: Number(pkg.units),
112
+ description: str(pkg.description, undefined),
113
+ priceCue: str(pkg.priceCue, undefined)
114
+ };
115
+ }),
116
+ addOns: parseAddOns(parsed.addOns, fallback.addOns),
117
+ disclaimer: str(parsed.disclaimer, fallback.disclaimer),
118
+ ctaLabel: str(parsed.ctaLabel, fallback.ctaLabel),
119
+ ctaUrl: str(parsed.ctaUrl, fallback.ctaUrl),
120
+ premiumUnitLabel: str(parsed.premiumUnitLabel, fallback.premiumUnitLabel),
121
+ baseUnitLabel: str(parsed.baseUnitLabel, fallback.baseUnitLabel)
122
+ };
123
+ }
124
+ catch {
125
+ return null;
126
+ }
127
+ }
128
+ export function calculatePricing(input, config) {
129
+ if (!Number.isInteger(input.premiumUnits) ||
130
+ !Number.isInteger(input.baseUnits) ||
131
+ input.premiumUnits < 0 ||
132
+ input.baseUnits < 0) {
133
+ throw new Error("Unit counts must be non-negative integers.");
134
+ }
135
+ const selectedPackage = config.packages.find((pkg) => pkg.key === input.packageKey);
136
+ if (!selectedPackage) {
137
+ throw new Error("Invalid package key.");
138
+ }
139
+ const fixedOverhead = Number(config.fixedOverhead ?? 0);
140
+ const premiumRevenue = input.premiumUnits * config.premiumUnitPrice;
141
+ const baseRevenue = input.baseUnits * config.baseUnitPrice;
142
+ const selectedAddOnKeys = Array.isArray(input.selectedAddOnKeys)
143
+ ? Array.from(new Set(input.selectedAddOnKeys.map((key) => String(key))))
144
+ : [];
145
+ const activeAddOns = config.addOns.filter((addOn) => selectedAddOnKeys.includes(addOn.key));
146
+ const addOnSubtotal = activeAddOns.reduce((total, addOn) => total + addOn.monthlyPrice, 0);
147
+ const extraRevenue = premiumRevenue + baseRevenue;
148
+ const totalMonthlyRevenue = selectedPackage.price + extraRevenue + addOnSubtotal;
149
+ const totalUnits = selectedPackage.units + input.premiumUnits + input.baseUnits;
150
+ const effectivePricePerUnit = totalUnits > 0 ? totalMonthlyRevenue / totalUnits : 0;
151
+ const monthlyMargin = totalMonthlyRevenue - fixedOverhead;
152
+ return {
153
+ packageName: selectedPackage.name,
154
+ basePrice: selectedPackage.price,
155
+ packageUnits: selectedPackage.units,
156
+ premiumUnits: input.premiumUnits,
157
+ premiumRevenue,
158
+ baseUnits: input.baseUnits,
159
+ baseRevenue,
160
+ activeAddOns,
161
+ addOnSubtotal: Number(addOnSubtotal.toFixed(2)),
162
+ extraUnits: input.premiumUnits + input.baseUnits,
163
+ extraRevenue,
164
+ totalMonthlyRevenue,
165
+ totalUnits,
166
+ effectivePricePerUnit: Number(effectivePricePerUnit.toFixed(2)),
167
+ monthlyMargin: Number(monthlyMargin.toFixed(2))
168
+ };
169
+ }
170
+ /** Shapes a config for public display: no overhead, formatted price cues and labels. */
171
+ export function toPublicPricingConfigResponse(config) {
172
+ return {
173
+ packages: config.packages.map((pkg) => ({
174
+ key: pkg.key,
175
+ name: pkg.name,
176
+ description: pkg.description,
177
+ priceCue: pkg.priceCue ??
178
+ (pkg.price > 0 ? `Starts at ${formatCurrency(pkg.price)}/month` : "Custom add-ons only")
179
+ })),
180
+ premiumUnitPrice: config.premiumUnitPrice,
181
+ baseUnitPrice: config.baseUnitPrice,
182
+ premiumUnitLabel: config.premiumUnitLabel ?? `Premium units (${formatCurrency(config.premiumUnitPrice)} each)`,
183
+ baseUnitLabel: config.baseUnitLabel ?? `Base units (${formatCurrency(config.baseUnitPrice)} each)`,
184
+ disclaimer: config.disclaimer ??
185
+ EXAMPLE_PUBLIC_PRICING_CONFIG.disclaimer ??
186
+ "Estimates may vary by scope.",
187
+ ctaLabel: config.ctaLabel ?? EXAMPLE_PUBLIC_PRICING_CONFIG.ctaLabel ?? "Book a call",
188
+ ctaUrl: config.ctaUrl ?? EXAMPLE_PUBLIC_PRICING_CONFIG.ctaUrl ?? "https://example.com/book"
189
+ };
190
+ }
191
+ /** Strips a calculation down to the fields that are safe to show a visitor (no margin, no overhead). */
192
+ export function toPublicPricingEstimate(result) {
193
+ return {
194
+ packageName: result.packageName,
195
+ premiumUnits: result.premiumUnits,
196
+ baseUnits: result.baseUnits,
197
+ extraRevenue: result.extraRevenue,
198
+ totalMonthlyRevenue: result.totalMonthlyRevenue
199
+ };
200
+ }
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@xallindrome-labs/pricing-domain",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Dependency-free pricing model: base package + per-unit overage + monthly add-ons.",
5
+ "keywords": [
6
+ "pricing",
7
+ "quote",
8
+ "calculator",
9
+ "packages",
10
+ "add-ons",
11
+ "typescript"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "Andrew Wilson (https://xallindrome.co)",
15
+ "homepage": "https://github.com/xallindrome-labs/xallindrome-widgets/tree/main/libs/pricing-domain#readme",
16
+ "bugs": "https://github.com/xallindrome-labs/xallindrome-widgets/issues",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/xallindrome-labs/xallindrome-widgets.git",
20
+ "directory": "libs/pricing-domain"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "type": "module",
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "sideEffects": false,
37
+ "files": [
38
+ "dist/index.js",
39
+ "dist/index.d.ts"
40
+ ],
41
+ "scripts": {
42
+ "build": "tsc -p tsconfig.json",
43
+ "typecheck": "tsc --noEmit -p tsconfig.json",
44
+ "test": "npm run build && node --test test/*.test.mjs",
45
+ "prepublishOnly": "npm run build"
46
+ },
47
+ "devDependencies": {
48
+ "typescript": "~5.9.3"
49
+ }
50
+ }