business-as-code 2.0.2 → 2.1.3

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 (89) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +25 -0
  3. package/LICENSE +21 -0
  4. package/dist/canvas/activities.d.ts +19 -0
  5. package/dist/canvas/activities.d.ts.map +1 -0
  6. package/dist/canvas/activities.js +20 -0
  7. package/dist/canvas/activities.js.map +1 -0
  8. package/dist/canvas/channels.d.ts +20 -0
  9. package/dist/canvas/channels.d.ts.map +1 -0
  10. package/dist/canvas/channels.js +21 -0
  11. package/dist/canvas/channels.js.map +1 -0
  12. package/dist/canvas/relationships.d.ts +20 -0
  13. package/dist/canvas/relationships.d.ts.map +1 -0
  14. package/dist/canvas/relationships.js +21 -0
  15. package/dist/canvas/relationships.js.map +1 -0
  16. package/dist/canvas/resources.d.ts +20 -0
  17. package/dist/canvas/resources.d.ts.map +1 -0
  18. package/dist/canvas/resources.js +30 -0
  19. package/dist/canvas/resources.js.map +1 -0
  20. package/dist/canvas/revenue.d.ts +22 -0
  21. package/dist/canvas/revenue.d.ts.map +1 -0
  22. package/dist/canvas/revenue.js +30 -0
  23. package/dist/canvas/revenue.js.map +1 -0
  24. package/dist/canvas/segments.d.ts +20 -0
  25. package/dist/canvas/segments.d.ts.map +1 -0
  26. package/dist/canvas/segments.js +28 -0
  27. package/dist/canvas/segments.js.map +1 -0
  28. package/dist/canvas/types.d.ts +232 -0
  29. package/dist/canvas/types.d.ts.map +1 -0
  30. package/dist/canvas/types.js +8 -0
  31. package/dist/canvas/types.js.map +1 -0
  32. package/dist/canvas/value.d.ts +20 -0
  33. package/dist/canvas/value.d.ts.map +1 -0
  34. package/dist/canvas/value.js +21 -0
  35. package/dist/canvas/value.js.map +1 -0
  36. package/dist/entities/planning.d.ts +0 -87
  37. package/examples/basic-usage.js +282 -0
  38. package/package.json +13 -14
  39. package/src/business.js +108 -0
  40. package/src/canvas/activities.ts +32 -0
  41. package/src/canvas/canvas.ts +482 -0
  42. package/src/canvas/channels.ts +34 -0
  43. package/src/canvas/costs.ts +43 -0
  44. package/src/canvas/economics.ts +99 -0
  45. package/src/canvas/index.ts +206 -0
  46. package/src/canvas/partnerships.ts +34 -0
  47. package/src/canvas/projections.ts +141 -0
  48. package/src/canvas/relationships.ts +34 -0
  49. package/src/canvas/resources.ts +43 -0
  50. package/src/canvas/revenue.ts +56 -0
  51. package/src/canvas/segments.ts +42 -0
  52. package/src/canvas/types.ts +363 -0
  53. package/src/canvas/value.ts +34 -0
  54. package/src/dollar.js +106 -0
  55. package/src/entities/assets.js +322 -0
  56. package/src/entities/business.js +369 -0
  57. package/src/entities/communication.js +254 -0
  58. package/src/entities/customers.js +988 -0
  59. package/src/entities/financials.js +931 -0
  60. package/src/entities/goals.js +799 -0
  61. package/src/entities/index.js +197 -0
  62. package/src/entities/legal.js +300 -0
  63. package/src/entities/market.js +300 -0
  64. package/src/entities/marketing.js +1156 -0
  65. package/src/entities/offerings.js +726 -0
  66. package/src/entities/operations.js +786 -0
  67. package/src/entities/organization.js +806 -0
  68. package/src/entities/partnerships.js +299 -0
  69. package/src/entities/planning.js +270 -0
  70. package/src/entities/projects.js +348 -0
  71. package/src/entities/risk.js +292 -0
  72. package/src/entities/sales.js +1247 -0
  73. package/src/financials.js +296 -0
  74. package/src/goals.js +214 -0
  75. package/src/index.js +131 -0
  76. package/src/index.test.js +274 -0
  77. package/src/kpis.js +231 -0
  78. package/src/metrics.js +324 -0
  79. package/src/okrs.js +268 -0
  80. package/src/organization.js +172 -0
  81. package/src/process.js +240 -0
  82. package/src/product.js +144 -0
  83. package/src/queries.js +414 -0
  84. package/src/roles.js +254 -0
  85. package/src/service.js +139 -0
  86. package/src/types.js +4 -0
  87. package/src/vision.js +67 -0
  88. package/src/workflow.js +246 -0
  89. package/tests/canvas.test.ts +842 -0
@@ -0,0 +1,274 @@
1
+ /**
2
+ * Tests for business-as-code package
3
+ */
4
+ import { describe, it, expect } from 'vitest';
5
+ import { Business, Vision, Goals, Product, Service, Process, Workflow, kpis, okrs, financials, $, calculateGrossMargin, calculateOverallProgress, updateProgress, calculateOKRProgress, updateKeyResult, } from './index.js';
6
+ describe('Business', () => {
7
+ it('should create a business entity', () => {
8
+ const business = Business({
9
+ name: 'Test Corp',
10
+ mission: 'Test mission',
11
+ values: ['Value 1', 'Value 2'],
12
+ });
13
+ expect(business.name).toBe('Test Corp');
14
+ expect(business.mission).toBe('Test mission');
15
+ expect(business.values).toEqual(['Value 1', 'Value 2']);
16
+ });
17
+ it('should throw error if name is missing', () => {
18
+ expect(() => Business({ name: '' })).toThrow('Business name is required');
19
+ });
20
+ });
21
+ describe('Vision', () => {
22
+ it('should create a vision statement', () => {
23
+ const vision = Vision({
24
+ statement: 'To be the best',
25
+ timeframe: '5 years',
26
+ successIndicators: ['Indicator 1', 'Indicator 2'],
27
+ });
28
+ expect(vision.statement).toBe('To be the best');
29
+ expect(vision.timeframe).toBe('5 years');
30
+ expect(vision.successIndicators).toHaveLength(2);
31
+ });
32
+ it('should throw error if statement is missing', () => {
33
+ expect(() => Vision({ statement: '' })).toThrow('Vision statement is required');
34
+ });
35
+ });
36
+ describe('Goals', () => {
37
+ it('should create multiple goals', () => {
38
+ const goals = Goals([
39
+ {
40
+ name: 'Goal 1',
41
+ category: 'strategic',
42
+ status: 'in-progress',
43
+ progress: 50,
44
+ },
45
+ {
46
+ name: 'Goal 2',
47
+ category: 'operational',
48
+ status: 'not-started',
49
+ progress: 0,
50
+ },
51
+ ]);
52
+ expect(goals).toHaveLength(2);
53
+ expect(goals[0].name).toBe('Goal 1');
54
+ expect(goals[1].name).toBe('Goal 2');
55
+ });
56
+ it('should calculate overall progress', () => {
57
+ const goals = Goals([
58
+ { name: 'Goal 1', progress: 50 },
59
+ { name: 'Goal 2', progress: 100 },
60
+ ]);
61
+ const progress = calculateOverallProgress(goals);
62
+ expect(progress).toBe(75);
63
+ });
64
+ it('should update goal progress', () => {
65
+ const goal = { name: 'Test', progress: 0, status: 'not-started' };
66
+ const updated = updateProgress(goal, 100);
67
+ expect(updated.progress).toBe(100);
68
+ expect(updated.status).toBe('completed');
69
+ });
70
+ });
71
+ describe('Product', () => {
72
+ it('should create a product', () => {
73
+ const product = Product({
74
+ name: 'Test Product',
75
+ price: 100,
76
+ cogs: 30,
77
+ });
78
+ expect(product.name).toBe('Test Product');
79
+ expect(product.price).toBe(100);
80
+ expect(product.cogs).toBe(30);
81
+ });
82
+ it('should calculate gross margin', () => {
83
+ const product = Product({
84
+ name: 'Test Product',
85
+ price: 100,
86
+ cogs: 30,
87
+ });
88
+ const margin = calculateGrossMargin(product);
89
+ expect(margin).toBe(70);
90
+ });
91
+ });
92
+ describe('Service', () => {
93
+ it('should create a service', () => {
94
+ const service = Service({
95
+ name: 'Test Service',
96
+ pricingModel: 'hourly',
97
+ price: 100,
98
+ });
99
+ expect(service.name).toBe('Test Service');
100
+ expect(service.pricingModel).toBe('hourly');
101
+ expect(service.price).toBe(100);
102
+ });
103
+ });
104
+ describe('Process', () => {
105
+ it('should create a process', () => {
106
+ const process = Process({
107
+ name: 'Test Process',
108
+ steps: [
109
+ {
110
+ order: 1,
111
+ name: 'Step 1',
112
+ automationLevel: 'automated',
113
+ },
114
+ ],
115
+ });
116
+ expect(process.name).toBe('Test Process');
117
+ expect(process.steps).toHaveLength(1);
118
+ });
119
+ });
120
+ describe('Workflow', () => {
121
+ it('should create a workflow', () => {
122
+ const workflow = Workflow({
123
+ name: 'Test Workflow',
124
+ trigger: { type: 'event', event: 'test.event' },
125
+ actions: [
126
+ {
127
+ order: 1,
128
+ type: 'send',
129
+ description: 'Send notification',
130
+ },
131
+ ],
132
+ });
133
+ expect(workflow.name).toBe('Test Workflow');
134
+ expect(workflow.trigger.type).toBe('event');
135
+ expect(workflow.actions).toHaveLength(1);
136
+ });
137
+ it('should throw error if trigger is missing', () => {
138
+ expect(() => Workflow({
139
+ name: 'Test',
140
+ trigger: undefined,
141
+ })).toThrow('Workflow trigger is required');
142
+ });
143
+ });
144
+ describe('KPIs', () => {
145
+ it('should create KPIs', () => {
146
+ const kpiList = kpis([
147
+ {
148
+ name: 'Revenue',
149
+ target: 100000,
150
+ current: 85000,
151
+ },
152
+ {
153
+ name: 'Churn',
154
+ target: 5,
155
+ current: 3.2,
156
+ },
157
+ ]);
158
+ expect(kpiList).toHaveLength(2);
159
+ expect(kpiList[0].name).toBe('Revenue');
160
+ });
161
+ });
162
+ describe('OKRs', () => {
163
+ it('should create OKRs', () => {
164
+ const okrList = okrs([
165
+ {
166
+ objective: 'Test Objective',
167
+ keyResults: [
168
+ {
169
+ description: 'KR 1',
170
+ metric: 'test_metric',
171
+ startValue: 0,
172
+ targetValue: 100,
173
+ currentValue: 50,
174
+ },
175
+ ],
176
+ },
177
+ ]);
178
+ expect(okrList).toHaveLength(1);
179
+ expect(okrList[0].objective).toBe('Test Objective');
180
+ expect(okrList[0].keyResults).toHaveLength(1);
181
+ });
182
+ it('should calculate OKR progress', () => {
183
+ const okr = {
184
+ objective: 'Test',
185
+ keyResults: [
186
+ {
187
+ description: 'KR 1',
188
+ metric: 'metric',
189
+ startValue: 0,
190
+ targetValue: 100,
191
+ currentValue: 50,
192
+ },
193
+ {
194
+ description: 'KR 2',
195
+ metric: 'metric',
196
+ startValue: 0,
197
+ targetValue: 100,
198
+ currentValue: 100,
199
+ },
200
+ ],
201
+ };
202
+ const progress = calculateOKRProgress(okr);
203
+ expect(progress).toBe(75);
204
+ });
205
+ it('should update key result', () => {
206
+ const okr = {
207
+ objective: 'Test',
208
+ keyResults: [
209
+ {
210
+ description: 'KR 1',
211
+ metric: 'metric',
212
+ startValue: 0,
213
+ targetValue: 100,
214
+ currentValue: 50,
215
+ },
216
+ ],
217
+ };
218
+ const updated = updateKeyResult(okr, 'KR 1', 75);
219
+ expect(updated.keyResults[0].currentValue).toBe(75);
220
+ expect(updated.keyResults[0].progress).toBe(75);
221
+ });
222
+ });
223
+ describe('Financials', () => {
224
+ it('should calculate financial metrics', () => {
225
+ const metrics = financials({
226
+ revenue: 1000000,
227
+ cogs: 300000,
228
+ operatingExpenses: 500000,
229
+ });
230
+ expect(metrics.revenue).toBe(1000000);
231
+ expect(metrics.grossProfit).toBe(700000);
232
+ expect(metrics.grossMargin).toBe(70);
233
+ expect(metrics.operatingIncome).toBe(200000);
234
+ expect(metrics.operatingMargin).toBe(20);
235
+ });
236
+ });
237
+ describe('$ Helper', () => {
238
+ it('should format currency', () => {
239
+ const formatted = $.format(1234.56);
240
+ expect(formatted).toMatch(/1,234\.56/);
241
+ });
242
+ it('should calculate percentage', () => {
243
+ const percent = $.percent(25, 100);
244
+ expect(percent).toBe(25);
245
+ });
246
+ it('should calculate growth', () => {
247
+ const growth = $.growth(120, 100);
248
+ expect(growth).toBe(20);
249
+ });
250
+ it('should calculate margin', () => {
251
+ const margin = $.margin(100, 60);
252
+ expect(margin).toBe(40);
253
+ });
254
+ it('should calculate ROI', () => {
255
+ const roi = $.roi(150, 100);
256
+ expect(roi).toBe(50);
257
+ });
258
+ it('should calculate LTV', () => {
259
+ const ltv = $.ltv(100, 12, 2);
260
+ expect(ltv).toBe(2400);
261
+ });
262
+ it('should calculate CAC', () => {
263
+ const cac = $.cac(10000, 100);
264
+ expect(cac).toBe(100);
265
+ });
266
+ it('should calculate burn rate', () => {
267
+ const burnRate = $.burnRate(100000, 70000, 3);
268
+ expect(burnRate).toBe(10000);
269
+ });
270
+ it('should calculate runway', () => {
271
+ const runway = $.runway(100000, 10000);
272
+ expect(runway).toBe(10);
273
+ });
274
+ });
package/src/kpis.js ADDED
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Key Performance Indicators (KPIs) management
3
+ */
4
+ /**
5
+ * Define Key Performance Indicators for tracking business metrics
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const businessKPIs = kpis([
10
+ * {
11
+ * name: 'Monthly Recurring Revenue',
12
+ * description: 'Total predictable revenue per month',
13
+ * category: 'financial',
14
+ * unit: 'USD',
15
+ * target: 100000,
16
+ * current: 85000,
17
+ * frequency: 'monthly',
18
+ * dataSource: 'Billing System',
19
+ * formula: 'SUM(active_subscriptions.price)',
20
+ * },
21
+ * {
22
+ * name: 'Customer Churn Rate',
23
+ * description: 'Percentage of customers lost per month',
24
+ * category: 'customer',
25
+ * unit: 'percent',
26
+ * target: 5,
27
+ * current: 3.2,
28
+ * frequency: 'monthly',
29
+ * dataSource: 'CRM',
30
+ * formula: '(churned_customers / total_customers) * 100',
31
+ * },
32
+ * {
33
+ * name: 'Net Promoter Score',
34
+ * description: 'Customer satisfaction and loyalty metric',
35
+ * category: 'customer',
36
+ * unit: 'score',
37
+ * target: 50,
38
+ * current: 48,
39
+ * frequency: 'quarterly',
40
+ * dataSource: 'Survey Platform',
41
+ * },
42
+ * ])
43
+ * ```
44
+ */
45
+ export function kpis(definitions) {
46
+ return definitions.map(kpi => validateAndNormalizeKPI(kpi));
47
+ }
48
+ /**
49
+ * Define a single KPI
50
+ */
51
+ export function kpi(definition) {
52
+ return validateAndNormalizeKPI(definition);
53
+ }
54
+ /**
55
+ * Validate and normalize a KPI definition
56
+ */
57
+ function validateAndNormalizeKPI(kpi) {
58
+ if (!kpi.name) {
59
+ throw new Error('KPI name is required');
60
+ }
61
+ return {
62
+ ...kpi,
63
+ category: kpi.category || 'operations',
64
+ frequency: kpi.frequency || 'monthly',
65
+ metadata: kpi.metadata || {},
66
+ };
67
+ }
68
+ /**
69
+ * Calculate KPI achievement percentage
70
+ */
71
+ export function calculateAchievement(kpi) {
72
+ if (kpi.target === undefined || kpi.current === undefined)
73
+ return 0;
74
+ if (kpi.target === 0)
75
+ return 100;
76
+ return (kpi.current / kpi.target) * 100;
77
+ }
78
+ /**
79
+ * Check if KPI meets target
80
+ */
81
+ export function meetsTarget(kpi) {
82
+ if (kpi.target === undefined || kpi.current === undefined)
83
+ return false;
84
+ // For metrics where lower is better (like churn rate)
85
+ const lowerIsBetter = ['churn', 'cost', 'time', 'error', 'downtime'].some(term => kpi.name.toLowerCase().includes(term));
86
+ if (lowerIsBetter) {
87
+ return kpi.current <= kpi.target;
88
+ }
89
+ return kpi.current >= kpi.target;
90
+ }
91
+ /**
92
+ * Update KPI current value
93
+ */
94
+ export function updateCurrent(kpi, value) {
95
+ return {
96
+ ...kpi,
97
+ current: value,
98
+ };
99
+ }
100
+ /**
101
+ * Update KPI target
102
+ */
103
+ export function updateTarget(kpi, target) {
104
+ return {
105
+ ...kpi,
106
+ target,
107
+ };
108
+ }
109
+ /**
110
+ * Get KPIs by category
111
+ */
112
+ export function getKPIsByCategory(kpis, category) {
113
+ return kpis.filter(k => k.category === category);
114
+ }
115
+ /**
116
+ * Get KPIs by frequency
117
+ */
118
+ export function getKPIsByFrequency(kpis, frequency) {
119
+ return kpis.filter(k => k.frequency === frequency);
120
+ }
121
+ /**
122
+ * Get KPIs that meet their targets
123
+ */
124
+ export function getKPIsOnTarget(kpis) {
125
+ return kpis.filter(meetsTarget);
126
+ }
127
+ /**
128
+ * Get KPIs that don't meet their targets
129
+ */
130
+ export function getKPIsOffTarget(kpis) {
131
+ return kpis.filter(kpi => !meetsTarget(kpi));
132
+ }
133
+ /**
134
+ * Calculate overall KPI health score (0-100)
135
+ */
136
+ export function calculateHealthScore(kpis) {
137
+ if (kpis.length === 0)
138
+ return 0;
139
+ const onTarget = getKPIsOnTarget(kpis).length;
140
+ return (onTarget / kpis.length) * 100;
141
+ }
142
+ /**
143
+ * Group KPIs by category
144
+ */
145
+ export function groupByCategory(kpis) {
146
+ const groups = new Map();
147
+ for (const kpi of kpis) {
148
+ const category = kpi.category || 'other';
149
+ const existing = groups.get(category) || [];
150
+ groups.set(category, [...existing, kpi]);
151
+ }
152
+ return groups;
153
+ }
154
+ /**
155
+ * Calculate variance from target
156
+ */
157
+ export function calculateVariance(kpi) {
158
+ if (kpi.target === undefined || kpi.current === undefined)
159
+ return 0;
160
+ return kpi.current - kpi.target;
161
+ }
162
+ /**
163
+ * Calculate variance percentage from target
164
+ */
165
+ export function calculateVariancePercentage(kpi) {
166
+ if (kpi.target === undefined || kpi.current === undefined)
167
+ return 0;
168
+ if (kpi.target === 0)
169
+ return 0;
170
+ return ((kpi.current - kpi.target) / kpi.target) * 100;
171
+ }
172
+ /**
173
+ * Format KPI value with unit
174
+ */
175
+ export function formatValue(kpi, value) {
176
+ const val = value ?? kpi.current;
177
+ if (val === undefined)
178
+ return 'N/A';
179
+ const formatted = val.toLocaleString(undefined, {
180
+ minimumFractionDigits: 0,
181
+ maximumFractionDigits: 2,
182
+ });
183
+ if (!kpi.unit)
184
+ return formatted;
185
+ switch (kpi.unit.toLowerCase()) {
186
+ case 'usd':
187
+ case 'eur':
188
+ case 'gbp':
189
+ return `$${formatted}`;
190
+ case 'percent':
191
+ case '%':
192
+ return `${formatted}%`;
193
+ default:
194
+ return `${formatted} ${kpi.unit}`;
195
+ }
196
+ }
197
+ /**
198
+ * Compare KPI performance over time
199
+ */
200
+ export function comparePerformance(current, previous) {
201
+ if (current.current === undefined || previous.current === undefined) {
202
+ return { change: 0, changePercent: 0, improved: false };
203
+ }
204
+ const change = current.current - previous.current;
205
+ const changePercent = previous.current !== 0 ? (change / previous.current) * 100 : 0;
206
+ // Determine if change is an improvement
207
+ const lowerIsBetter = ['churn', 'cost', 'time', 'error', 'downtime'].some(term => current.name.toLowerCase().includes(term));
208
+ const improved = lowerIsBetter ? change < 0 : change > 0;
209
+ return { change, changePercent, improved };
210
+ }
211
+ /**
212
+ * Validate KPI definitions
213
+ */
214
+ export function validateKPIs(kpis) {
215
+ const errors = [];
216
+ for (const kpi of kpis) {
217
+ if (!kpi.name) {
218
+ errors.push('KPI name is required');
219
+ }
220
+ if (kpi.target !== undefined && kpi.target < 0) {
221
+ errors.push(`KPI ${kpi.name} target cannot be negative`);
222
+ }
223
+ if (kpi.current !== undefined && kpi.current < 0) {
224
+ errors.push(`KPI ${kpi.name} current value cannot be negative`);
225
+ }
226
+ }
227
+ return {
228
+ valid: errors.length === 0,
229
+ errors,
230
+ };
231
+ }