business-as-code 0.2.1 → 2.0.2

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.
Files changed (190) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +17 -0
  3. package/IMPLEMENTATION.md +226 -0
  4. package/README.md +1133 -193
  5. package/dist/business.d.ts +62 -0
  6. package/dist/business.d.ts.map +1 -0
  7. package/dist/business.js +109 -0
  8. package/dist/business.js.map +1 -0
  9. package/dist/dollar.d.ts +60 -0
  10. package/dist/dollar.d.ts.map +1 -0
  11. package/dist/dollar.js +107 -0
  12. package/dist/dollar.js.map +1 -0
  13. package/dist/entities/assets.d.ts +21 -0
  14. package/dist/entities/assets.d.ts.map +1 -0
  15. package/dist/entities/assets.js +323 -0
  16. package/dist/entities/assets.js.map +1 -0
  17. package/dist/entities/business.d.ts +36 -0
  18. package/dist/entities/business.d.ts.map +1 -0
  19. package/dist/entities/business.js +370 -0
  20. package/dist/entities/business.js.map +1 -0
  21. package/dist/entities/communication.d.ts +21 -0
  22. package/dist/entities/communication.d.ts.map +1 -0
  23. package/dist/entities/communication.js +255 -0
  24. package/dist/entities/communication.js.map +1 -0
  25. package/dist/entities/customers.d.ts +58 -0
  26. package/dist/entities/customers.d.ts.map +1 -0
  27. package/dist/entities/customers.js +989 -0
  28. package/dist/entities/customers.js.map +1 -0
  29. package/dist/entities/financials.d.ts +59 -0
  30. package/dist/entities/financials.d.ts.map +1 -0
  31. package/dist/entities/financials.js +932 -0
  32. package/dist/entities/financials.js.map +1 -0
  33. package/dist/entities/goals.d.ts +58 -0
  34. package/dist/entities/goals.d.ts.map +1 -0
  35. package/dist/entities/goals.js +800 -0
  36. package/dist/entities/goals.js.map +1 -0
  37. package/dist/entities/index.d.ts +299 -0
  38. package/dist/entities/index.d.ts.map +1 -0
  39. package/dist/entities/index.js +198 -0
  40. package/dist/entities/index.js.map +1 -0
  41. package/dist/entities/legal.d.ts +21 -0
  42. package/dist/entities/legal.d.ts.map +1 -0
  43. package/dist/entities/legal.js +301 -0
  44. package/dist/entities/legal.js.map +1 -0
  45. package/dist/entities/market.d.ts +21 -0
  46. package/dist/entities/market.d.ts.map +1 -0
  47. package/dist/entities/market.js +301 -0
  48. package/dist/entities/market.js.map +1 -0
  49. package/dist/entities/marketing.d.ts +67 -0
  50. package/dist/entities/marketing.d.ts.map +1 -0
  51. package/dist/entities/marketing.js +1157 -0
  52. package/dist/entities/marketing.js.map +1 -0
  53. package/dist/entities/offerings.d.ts +51 -0
  54. package/dist/entities/offerings.d.ts.map +1 -0
  55. package/dist/entities/offerings.js +727 -0
  56. package/dist/entities/offerings.js.map +1 -0
  57. package/dist/entities/operations.d.ts +58 -0
  58. package/dist/entities/operations.d.ts.map +1 -0
  59. package/dist/entities/operations.js +787 -0
  60. package/dist/entities/operations.js.map +1 -0
  61. package/dist/entities/organization.d.ts +57 -0
  62. package/dist/entities/organization.d.ts.map +1 -0
  63. package/dist/entities/organization.js +807 -0
  64. package/dist/entities/organization.js.map +1 -0
  65. package/dist/entities/partnerships.d.ts +21 -0
  66. package/dist/entities/partnerships.d.ts.map +1 -0
  67. package/dist/entities/partnerships.js +300 -0
  68. package/dist/entities/partnerships.js.map +1 -0
  69. package/dist/entities/planning.d.ts +87 -0
  70. package/dist/entities/planning.d.ts.map +1 -0
  71. package/dist/entities/planning.js +271 -0
  72. package/dist/entities/planning.js.map +1 -0
  73. package/dist/entities/projects.d.ts +25 -0
  74. package/dist/entities/projects.d.ts.map +1 -0
  75. package/dist/entities/projects.js +349 -0
  76. package/dist/entities/projects.js.map +1 -0
  77. package/dist/entities/risk.d.ts +21 -0
  78. package/dist/entities/risk.d.ts.map +1 -0
  79. package/dist/entities/risk.js +293 -0
  80. package/dist/entities/risk.js.map +1 -0
  81. package/dist/entities/sales.d.ts +72 -0
  82. package/dist/entities/sales.d.ts.map +1 -0
  83. package/dist/entities/sales.js +1248 -0
  84. package/dist/entities/sales.js.map +1 -0
  85. package/dist/financials.d.ts +130 -0
  86. package/dist/financials.d.ts.map +1 -0
  87. package/dist/financials.js +297 -0
  88. package/dist/financials.js.map +1 -0
  89. package/dist/goals.d.ts +87 -0
  90. package/dist/goals.d.ts.map +1 -0
  91. package/dist/goals.js +215 -0
  92. package/dist/goals.js.map +1 -0
  93. package/dist/index.d.ts +97 -4
  94. package/dist/index.d.ts.map +1 -0
  95. package/dist/index.js +131 -1079
  96. package/dist/index.js.map +1 -1
  97. package/dist/kpis.d.ts +118 -0
  98. package/dist/kpis.d.ts.map +1 -0
  99. package/dist/kpis.js +232 -0
  100. package/dist/kpis.js.map +1 -0
  101. package/dist/metrics.d.ts +448 -0
  102. package/dist/metrics.d.ts.map +1 -0
  103. package/dist/metrics.js +325 -0
  104. package/dist/metrics.js.map +1 -0
  105. package/dist/okrs.d.ts +123 -0
  106. package/dist/okrs.d.ts.map +1 -0
  107. package/dist/okrs.js +269 -0
  108. package/dist/okrs.js.map +1 -0
  109. package/dist/organization.d.ts +585 -0
  110. package/dist/organization.d.ts.map +1 -0
  111. package/dist/organization.js +173 -0
  112. package/dist/organization.js.map +1 -0
  113. package/dist/process.d.ts +112 -0
  114. package/dist/process.d.ts.map +1 -0
  115. package/dist/process.js +241 -0
  116. package/dist/process.js.map +1 -0
  117. package/dist/product.d.ts +85 -0
  118. package/dist/product.d.ts.map +1 -0
  119. package/dist/product.js +145 -0
  120. package/dist/product.js.map +1 -0
  121. package/dist/queries.d.ts +304 -0
  122. package/dist/queries.d.ts.map +1 -0
  123. package/dist/queries.js +415 -0
  124. package/dist/queries.js.map +1 -0
  125. package/dist/roles.d.ts +340 -0
  126. package/dist/roles.d.ts.map +1 -0
  127. package/dist/roles.js +255 -0
  128. package/dist/roles.js.map +1 -0
  129. package/dist/service.d.ts +61 -0
  130. package/dist/service.d.ts.map +1 -0
  131. package/dist/service.js +140 -0
  132. package/dist/service.js.map +1 -0
  133. package/dist/types.d.ts +459 -0
  134. package/dist/types.d.ts.map +1 -0
  135. package/dist/types.js +5 -0
  136. package/dist/types.js.map +1 -0
  137. package/dist/vision.d.ts +38 -0
  138. package/dist/vision.d.ts.map +1 -0
  139. package/dist/vision.js +68 -0
  140. package/dist/vision.js.map +1 -0
  141. package/dist/workflow.d.ts +115 -0
  142. package/dist/workflow.d.ts.map +1 -0
  143. package/dist/workflow.js +247 -0
  144. package/dist/workflow.js.map +1 -0
  145. package/examples/basic-usage.ts +307 -0
  146. package/package.json +19 -60
  147. package/src/business.ts +121 -0
  148. package/src/dollar.ts +132 -0
  149. package/src/entities/assets.ts +332 -0
  150. package/src/entities/business.ts +406 -0
  151. package/src/entities/communication.ts +264 -0
  152. package/src/entities/customers.ts +1072 -0
  153. package/src/entities/financials.ts +1011 -0
  154. package/src/entities/goals.ts +871 -0
  155. package/src/entities/index.ts +383 -0
  156. package/src/entities/legal.ts +310 -0
  157. package/src/entities/market.ts +310 -0
  158. package/src/entities/marketing.ts +1249 -0
  159. package/src/entities/offerings.ts +789 -0
  160. package/src/entities/operations.ts +861 -0
  161. package/src/entities/organization.ts +876 -0
  162. package/src/entities/partnerships.ts +309 -0
  163. package/src/entities/planning.ts +307 -0
  164. package/src/entities/projects.ts +360 -0
  165. package/src/entities/risk.ts +302 -0
  166. package/src/entities/sales.ts +1352 -0
  167. package/src/financials.ts +352 -0
  168. package/src/goals.ts +250 -0
  169. package/src/index.test.ts +336 -0
  170. package/src/index.ts +530 -0
  171. package/src/kpis.ts +275 -0
  172. package/src/metrics.ts +825 -0
  173. package/src/okrs.ts +325 -0
  174. package/src/organization.ts +909 -0
  175. package/src/process.ts +272 -0
  176. package/src/product.ts +178 -0
  177. package/src/queries.ts +767 -0
  178. package/src/roles.ts +686 -0
  179. package/src/service.ts +164 -0
  180. package/src/types.ts +493 -0
  181. package/src/vision.ts +88 -0
  182. package/src/workflow.ts +280 -0
  183. package/tsconfig.json +9 -0
  184. package/dist/loaders/index.d.ts +0 -174
  185. package/dist/loaders/index.js +0 -366
  186. package/dist/loaders/index.js.map +0 -1
  187. package/dist/schema/index.d.ts +0 -146
  188. package/dist/schema/index.js +0 -716
  189. package/dist/schema/index.js.map +0 -1
  190. package/dist/types-CJ9eGS_C.d.ts +0 -86
@@ -0,0 +1,307 @@
1
+ /**
2
+ * Basic usage example for business-as-code
3
+ *
4
+ * This example demonstrates how to define business entities, goals, products,
5
+ * KPIs, OKRs, and calculate financial metrics.
6
+ */
7
+
8
+ import {
9
+ Business,
10
+ Vision,
11
+ Goals,
12
+ Product,
13
+ Service,
14
+ Process,
15
+ Workflow,
16
+ kpis,
17
+ okrs,
18
+ financials,
19
+ $,
20
+ } from '../src/index.js'
21
+
22
+ // Define your business
23
+ const company = Business({
24
+ name: 'Acme Corp',
25
+ description: 'Building the future of widgets',
26
+ industry: 'Technology',
27
+ mission: 'To make widgets accessible to everyone',
28
+ values: ['Innovation', 'Customer Focus', 'Integrity'],
29
+ targetMarket: 'SMB and Enterprise',
30
+ foundedAt: new Date('2020-01-01'),
31
+ teamSize: 50,
32
+ structure: {
33
+ departments: [
34
+ {
35
+ name: 'Engineering',
36
+ head: 'Jane Smith',
37
+ members: ['Alice', 'Bob', 'Charlie'],
38
+ budget: 2000000,
39
+ },
40
+ {
41
+ name: 'Sales',
42
+ head: 'John Doe',
43
+ members: ['David', 'Eve'],
44
+ budget: 1000000,
45
+ },
46
+ ],
47
+ },
48
+ })
49
+
50
+ // Define vision
51
+ const vision = Vision({
52
+ statement: 'To become the world\'s most trusted widget platform',
53
+ timeframe: '5 years',
54
+ successIndicators: [
55
+ '10M+ active users',
56
+ 'Present in 50+ countries',
57
+ 'Industry-leading NPS score',
58
+ '$1B+ annual revenue',
59
+ ],
60
+ })
61
+
62
+ // Define goals
63
+ const goals = Goals([
64
+ {
65
+ name: 'Launch MVP',
66
+ description: 'Ship minimum viable product to early customers',
67
+ category: 'strategic',
68
+ targetDate: new Date('2024-06-30'),
69
+ owner: 'Product Team',
70
+ metrics: ['User signups', 'Feature completion rate'],
71
+ status: 'in-progress',
72
+ progress: 65,
73
+ },
74
+ {
75
+ name: 'Achieve Product-Market Fit',
76
+ description: 'Validate product value with target customers',
77
+ category: 'strategic',
78
+ targetDate: new Date('2024-12-31'),
79
+ owner: 'CEO',
80
+ metrics: ['NPS > 50', 'Churn < 5%', '100+ paying customers'],
81
+ status: 'in-progress',
82
+ progress: 30,
83
+ dependencies: ['Launch MVP'],
84
+ },
85
+ ])
86
+
87
+ // Define products
88
+ const product = Product({
89
+ name: 'Widget Pro',
90
+ description: 'Enterprise-grade widget management platform',
91
+ category: 'SaaS',
92
+ targetSegment: 'Enterprise',
93
+ valueProposition: 'Reduce widget management costs by 50%',
94
+ pricingModel: 'subscription',
95
+ price: 99,
96
+ currency: 'USD',
97
+ cogs: 20,
98
+ features: [
99
+ 'Unlimited widgets',
100
+ 'Advanced analytics',
101
+ 'API access',
102
+ '24/7 support',
103
+ ],
104
+ roadmap: [
105
+ {
106
+ name: 'Mobile app',
107
+ description: 'Native iOS and Android apps',
108
+ targetDate: new Date('2024-09-01'),
109
+ priority: 'high',
110
+ status: 'in-progress',
111
+ },
112
+ ],
113
+ })
114
+
115
+ // Define services
116
+ const service = Service({
117
+ name: 'Widget Consulting',
118
+ description: 'Expert widget implementation and optimization',
119
+ category: 'Consulting',
120
+ targetSegment: 'Enterprise',
121
+ valueProposition: 'Get expert help implementing widgets in 2 weeks',
122
+ pricingModel: 'fixed',
123
+ price: 5000,
124
+ currency: 'USD',
125
+ deliveryTime: '2 weeks',
126
+ sla: {
127
+ uptime: 99.9,
128
+ responseTime: '< 24 hours',
129
+ supportHours: 'Business hours (9-5 EST)',
130
+ penalties: '10% refund per day of delay',
131
+ },
132
+ })
133
+
134
+ // Define a business process
135
+ const process = Process({
136
+ name: 'Customer Onboarding',
137
+ description: 'Process for onboarding new customers',
138
+ category: 'core',
139
+ owner: 'Customer Success Team',
140
+ steps: [
141
+ {
142
+ order: 1,
143
+ name: 'Welcome Email',
144
+ description: 'Send personalized welcome email',
145
+ responsible: 'CS Manager',
146
+ duration: '5 minutes',
147
+ automationLevel: 'automated',
148
+ },
149
+ {
150
+ order: 2,
151
+ name: 'Initial Setup Call',
152
+ description: 'Schedule and conduct setup call',
153
+ responsible: 'CS Rep',
154
+ duration: '30 minutes',
155
+ automationLevel: 'manual',
156
+ },
157
+ ],
158
+ inputs: ['Customer Information', 'Subscription Plan'],
159
+ outputs: ['Configured Account', 'Training Materials'],
160
+ })
161
+
162
+ // Define a workflow
163
+ const workflow = Workflow({
164
+ name: 'New Customer Welcome',
165
+ description: 'Automated welcome sequence for new customers',
166
+ trigger: {
167
+ type: 'event',
168
+ event: 'Customer.created',
169
+ },
170
+ actions: [
171
+ {
172
+ order: 1,
173
+ type: 'send',
174
+ description: 'Send welcome email',
175
+ params: {
176
+ template: 'welcome_email',
177
+ to: '{{customer.email}}',
178
+ },
179
+ },
180
+ {
181
+ order: 2,
182
+ type: 'create',
183
+ description: 'Create onboarding task',
184
+ params: {
185
+ type: 'Task',
186
+ title: 'Onboard {{customer.name}}',
187
+ assignee: 'customer_success_team',
188
+ },
189
+ },
190
+ ],
191
+ })
192
+
193
+ // Track KPIs
194
+ const kpiList = kpis([
195
+ {
196
+ name: 'Monthly Recurring Revenue',
197
+ description: 'Total predictable revenue per month',
198
+ category: 'financial',
199
+ unit: 'USD',
200
+ target: 100000,
201
+ current: 85000,
202
+ frequency: 'monthly',
203
+ dataSource: 'Billing System',
204
+ formula: 'SUM(active_subscriptions.price)',
205
+ },
206
+ {
207
+ name: 'Customer Churn Rate',
208
+ description: 'Percentage of customers lost per month',
209
+ category: 'customer',
210
+ unit: 'percent',
211
+ target: 5,
212
+ current: 3.2,
213
+ frequency: 'monthly',
214
+ dataSource: 'CRM',
215
+ formula: '(churned_customers / total_customers) * 100',
216
+ },
217
+ {
218
+ name: 'Net Promoter Score',
219
+ description: 'Customer satisfaction and loyalty metric',
220
+ category: 'customer',
221
+ unit: 'score',
222
+ target: 50,
223
+ current: 48,
224
+ frequency: 'quarterly',
225
+ dataSource: 'Survey Platform',
226
+ },
227
+ ])
228
+
229
+ // Define OKRs
230
+ const okrList = okrs([
231
+ {
232
+ objective: 'Achieve Product-Market Fit',
233
+ description: 'Validate that our product solves a real problem for customers',
234
+ period: 'Q2 2024',
235
+ owner: 'CEO',
236
+ keyResults: [
237
+ {
238
+ description: 'Increase Net Promoter Score',
239
+ metric: 'NPS',
240
+ startValue: 40,
241
+ targetValue: 60,
242
+ currentValue: 52,
243
+ unit: 'score',
244
+ },
245
+ {
246
+ description: 'Reduce monthly churn rate',
247
+ metric: 'Churn Rate',
248
+ startValue: 8,
249
+ targetValue: 4,
250
+ currentValue: 5.5,
251
+ unit: 'percent',
252
+ },
253
+ {
254
+ description: 'Achieve customer retention',
255
+ metric: 'Customers with 3+ months',
256
+ startValue: 50,
257
+ targetValue: 200,
258
+ currentValue: 125,
259
+ unit: 'customers',
260
+ },
261
+ ],
262
+ status: 'on-track',
263
+ confidence: 75,
264
+ },
265
+ ])
266
+
267
+ // Calculate financials
268
+ const metrics = financials({
269
+ revenue: 1000000,
270
+ cogs: 300000,
271
+ operatingExpenses: 500000,
272
+ currency: 'USD',
273
+ period: 'monthly',
274
+ })
275
+
276
+ // Use $ helper for calculations
277
+ console.log('\n=== Business Operations with $ ===')
278
+ console.log('Format currency:', $.format(1234.56))
279
+ console.log('Calculate percentage:', $.percent(25, 100))
280
+ console.log('Calculate growth:', $.growth(120, 100))
281
+ console.log('Calculate margin:', $.margin(100, 60))
282
+ console.log('Calculate ROI:', $.roi(150, 100))
283
+ console.log('Calculate LTV:', $.ltv(100, 12, 24))
284
+ console.log('Calculate CAC:', $.cac(10000, 100))
285
+ console.log('Calculate burn rate:', $.burnRate(100000, 70000, 3))
286
+ console.log('Calculate runway:', $.runway(100000, 10000))
287
+
288
+ // Display results
289
+ console.log('\n=== Business Summary ===')
290
+ console.log(`Company: ${company.name}`)
291
+ console.log(`Mission: ${company.mission}`)
292
+ console.log(`Team Size: ${company.teamSize}`)
293
+ console.log(`\nVision: ${vision.statement}`)
294
+ console.log(`Timeframe: ${vision.timeframe}`)
295
+ console.log(`\nGoals: ${goals.length}`)
296
+ console.log(`Products: ${product.name} - ${$.format(product.price!)} ${product.pricingModel}`)
297
+ console.log(`Services: ${service.name} - ${$.format(service.price!)} ${service.pricingModel}`)
298
+ console.log(`\nKPIs: ${kpiList.length}`)
299
+ console.log(`OKRs: ${okrList.length}`)
300
+ console.log(`\n=== Financial Metrics ===`)
301
+ console.log(`Revenue: ${$.format(metrics.revenue!)}`)
302
+ console.log(`Gross Profit: ${$.format(metrics.grossProfit!)}`)
303
+ console.log(`Gross Margin: ${metrics.grossMargin!.toFixed(1)}%`)
304
+ console.log(`Operating Income: ${$.format(metrics.operatingIncome!)}`)
305
+ console.log(`Operating Margin: ${metrics.operatingMargin!.toFixed(1)}%`)
306
+ console.log(`Net Income: ${$.format(metrics.netIncome!)}`)
307
+ console.log(`Net Margin: ${metrics.netMargin!.toFixed(1)}%`)
package/package.json CHANGED
@@ -1,75 +1,34 @@
1
1
  {
2
2
  "name": "business-as-code",
3
- "version": "0.2.1",
4
- "description": "Define, launch, experiment, iterate, and grow your business entirely in code",
3
+ "version": "2.0.2",
4
+ "description": "Primitives for expressing business logic and processes as code",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
7
- "module": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
10
  "import": "./dist/index.js",
12
11
  "types": "./dist/index.d.ts"
13
- },
14
- "./schema": {
15
- "import": "./dist/schema/index.js",
16
- "types": "./dist/schema/index.d.ts"
17
- },
18
- "./loaders": {
19
- "import": "./dist/loaders/index.js",
20
- "types": "./dist/loaders/index.d.ts"
21
12
  }
22
13
  },
23
- "files": [
24
- "dist"
25
- ],
26
14
  "scripts": {
27
- "build": "tsup",
28
- "dev": "tsup --watch",
29
- "test": "vitest run",
30
- "test:watch": "vitest",
31
- "typecheck": "tsc --noEmit"
15
+ "build": "tsc",
16
+ "dev": "tsc --watch",
17
+ "test": "vitest",
18
+ "typecheck": "tsc --noEmit",
19
+ "lint": "eslint .",
20
+ "clean": "rm -rf dist"
21
+ },
22
+ "dependencies": {
23
+ "ai-database": "2.0.2",
24
+ "ai-functions": "2.0.2",
25
+ "rpc.do": "^0.1.0"
32
26
  },
33
27
  "keywords": [
34
28
  "business",
35
- "schema",
36
- "dsl",
37
- "mdx",
38
- "yaml",
39
- "json",
40
- "startups",
41
- "platform",
42
- "ai",
43
- "human-ai-collaboration",
44
- "organization",
45
- "processes",
46
- "departments",
47
- "roles",
48
- "entities",
49
- "nouns"
29
+ "logic",
30
+ "code",
31
+ "primitives"
50
32
  ],
51
- "author": "Startups.Studio",
52
- "license": "MIT",
53
- "repository": {
54
- "type": "git",
55
- "url": "https://github.com/StartupsStudio/db.sb",
56
- "directory": "packages/business-as-code"
57
- },
58
- "homepage": "https://business-as.code",
59
- "bugs": {
60
- "url": "https://github.com/StartupsStudio/db.sb/issues"
61
- },
62
- "dependencies": {
63
- "db.sb": "^0.1.7",
64
- "yaml": "^2.7.0"
65
- },
66
- "devDependencies": {
67
- "@types/node": "^22.0.0",
68
- "tsup": "^8.0.0",
69
- "typescript": "^5.5.0",
70
- "vitest": "~3.2.0"
71
- },
72
- "engines": {
73
- "node": ">=18"
74
- }
33
+ "license": "MIT"
75
34
  }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Business entity definition
3
+ */
4
+
5
+ import type { BusinessDefinition } from './types.js'
6
+
7
+ /**
8
+ * Define a business entity with organizational structure, mission, and values
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const acme = Business({
13
+ * name: 'Acme Corp',
14
+ * description: 'Building the future of widgets',
15
+ * industry: 'Technology',
16
+ * mission: 'To make widgets accessible to everyone',
17
+ * values: ['Innovation', 'Customer Focus', 'Integrity'],
18
+ * targetMarket: 'SMB and Enterprise',
19
+ * foundedAt: new Date('2020-01-01'),
20
+ * teamSize: 50,
21
+ * structure: {
22
+ * departments: [
23
+ * {
24
+ * name: 'Engineering',
25
+ * head: 'Jane Smith',
26
+ * members: ['Alice', 'Bob', 'Charlie'],
27
+ * budget: 2000000,
28
+ * },
29
+ * {
30
+ * name: 'Sales',
31
+ * head: 'John Doe',
32
+ * members: ['David', 'Eve'],
33
+ * budget: 1000000,
34
+ * },
35
+ * ],
36
+ * },
37
+ * })
38
+ * ```
39
+ */
40
+ export function Business(definition: BusinessDefinition): BusinessDefinition {
41
+ // Validate required fields
42
+ if (!definition.name) {
43
+ throw new Error('Business name is required')
44
+ }
45
+
46
+ // Return validated business definition
47
+ return {
48
+ ...definition,
49
+ foundedAt: definition.foundedAt || new Date(),
50
+ teamSize: definition.teamSize || 0,
51
+ values: definition.values || [],
52
+ metadata: definition.metadata || {},
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Get total budget across all departments
58
+ */
59
+ export function getTotalBudget(business: BusinessDefinition): number {
60
+ if (!business.structure?.departments) return 0
61
+
62
+ return business.structure.departments.reduce((total, dept) => {
63
+ return total + (dept.budget || 0)
64
+ }, 0)
65
+ }
66
+
67
+ /**
68
+ * Get total team size across all departments
69
+ */
70
+ export function getTotalTeamSize(business: BusinessDefinition): number {
71
+ if (!business.structure?.departments) return business.teamSize || 0
72
+
73
+ return business.structure.departments.reduce((total, dept) => {
74
+ return total + (dept.members?.length || 0)
75
+ }, 0)
76
+ }
77
+
78
+ /**
79
+ * Get department by name
80
+ */
81
+ export function getDepartment(business: BusinessDefinition, name: string) {
82
+ return business.structure?.departments?.find(d => d.name === name)
83
+ }
84
+
85
+ /**
86
+ * Get team by name
87
+ */
88
+ export function getTeam(business: BusinessDefinition, name: string) {
89
+ return business.structure?.teams?.find(t => t.name === name)
90
+ }
91
+
92
+ /**
93
+ * Validate business definition
94
+ */
95
+ export function validateBusiness(business: BusinessDefinition): { valid: boolean; errors: string[] } {
96
+ const errors: string[] = []
97
+
98
+ if (!business.name) {
99
+ errors.push('Business name is required')
100
+ }
101
+
102
+ if (business.teamSize && business.teamSize < 0) {
103
+ errors.push('Team size cannot be negative')
104
+ }
105
+
106
+ if (business.structure?.departments) {
107
+ for (const dept of business.structure.departments) {
108
+ if (!dept.name) {
109
+ errors.push('Department name is required')
110
+ }
111
+ if (dept.budget && dept.budget < 0) {
112
+ errors.push(`Department ${dept.name} budget cannot be negative`)
113
+ }
114
+ }
115
+ }
116
+
117
+ return {
118
+ valid: errors.length === 0,
119
+ errors,
120
+ }
121
+ }
package/src/dollar.ts ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * $ - Business operations helper
3
+ *
4
+ * Provides convenient functions for common business calculations and formatting
5
+ */
6
+
7
+ import type { BusinessOperations, BusinessContext, Currency } from './types.js'
8
+ import {
9
+ formatCurrency,
10
+ calculateGrowthRate,
11
+ calculateGrossMargin,
12
+ calculateROI,
13
+ calculateLTV,
14
+ calculateCAC,
15
+ calculateBurnRate,
16
+ calculateRunway,
17
+ } from './financials.js'
18
+
19
+ /**
20
+ * Create a business operations helper with context
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { $ } from 'business-as-code'
25
+ *
26
+ * // Format currency
27
+ * console.log($.format(1234.56)) // "$1,234.56"
28
+ * console.log($.format(1000, 'EUR')) // "€1,000"
29
+ *
30
+ * // Calculate percentage
31
+ * console.log($.percent(25, 100)) // 25
32
+ *
33
+ * // Calculate growth
34
+ * console.log($.growth(120, 100)) // 20
35
+ *
36
+ * // Calculate margin
37
+ * console.log($.margin(100, 60)) // 40
38
+ *
39
+ * // Calculate ROI
40
+ * console.log($.roi(150, 100)) // 50
41
+ *
42
+ * // Calculate LTV
43
+ * console.log($.ltv(100, 12, 24)) // 28800
44
+ *
45
+ * // Calculate CAC
46
+ * console.log($.cac(10000, 100)) // 100
47
+ *
48
+ * // Calculate burn rate
49
+ * console.log($.burnRate(100000, 70000, 3)) // 10000
50
+ *
51
+ * // Calculate runway
52
+ * console.log($.runway(100000, 10000)) // 10
53
+ * ```
54
+ */
55
+ export function createBusinessOperations(initialContext?: BusinessContext): BusinessOperations {
56
+ const context: BusinessContext = initialContext || {}
57
+
58
+ return {
59
+ format: (amount: number, currency?: Currency) => {
60
+ return formatCurrency(amount, currency || context.business?.metadata?.currency as Currency || 'USD')
61
+ },
62
+
63
+ percent: (value: number, total: number) => {
64
+ if (total === 0) return 0
65
+ return (value / total) * 100
66
+ },
67
+
68
+ growth: (current: number, previous: number) => {
69
+ return calculateGrowthRate(current, previous)
70
+ },
71
+
72
+ margin: (revenue: number, cost: number) => {
73
+ return calculateGrossMargin(revenue, cost)
74
+ },
75
+
76
+ roi: (gain: number, cost: number) => {
77
+ return calculateROI(gain, cost)
78
+ },
79
+
80
+ ltv: (averageValue: number, frequency: number, lifetime: number) => {
81
+ // frequency = purchases per year, lifetime = years
82
+ const annualValue = averageValue * frequency
83
+ return annualValue * lifetime
84
+ },
85
+
86
+ cac: (marketingSpend: number, newCustomers: number) => {
87
+ return calculateCAC(marketingSpend, newCustomers)
88
+ },
89
+
90
+ burnRate: (cashStart: number, cashEnd: number, months: number) => {
91
+ return calculateBurnRate(cashStart, cashEnd, months)
92
+ },
93
+
94
+ runway: (cash: number, burnRate: number) => {
95
+ return calculateRunway(cash, burnRate)
96
+ },
97
+
98
+ context,
99
+
100
+ log: (event: string, data?: unknown) => {
101
+ console.log(`[Business Event] ${event}`, data || '')
102
+ },
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Default business operations instance
108
+ */
109
+ export const $: BusinessOperations = createBusinessOperations()
110
+
111
+ /**
112
+ * Update business context
113
+ */
114
+ export function updateContext(updates: Partial<BusinessContext>): void {
115
+ Object.assign($.context, updates)
116
+ }
117
+
118
+ /**
119
+ * Get current business context
120
+ */
121
+ export function getContext(): BusinessContext {
122
+ return $.context
123
+ }
124
+
125
+ /**
126
+ * Reset business context
127
+ */
128
+ export function resetContext(): void {
129
+ Object.keys($.context).forEach(key => {
130
+ delete $.context[key]
131
+ })
132
+ }