business-as-code 2.1.1 → 2.3.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.
Files changed (212) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +2 -0
  3. package/package.json +7 -4
  4. package/src/dollar.ts +5 -2
  5. package/src/entities/organization.ts +31 -18
  6. package/src/goals.ts +78 -12
  7. package/src/index.ts +48 -18
  8. package/src/kpis.ts +62 -8
  9. package/src/metrics.ts +92 -79
  10. package/src/okrs.ts +120 -20
  11. package/src/organization.ts +12 -15
  12. package/src/process.ts +11 -12
  13. package/src/product.ts +8 -9
  14. package/src/queries.ts +238 -75
  15. package/src/roles.ts +62 -61
  16. package/src/workflow.ts +22 -15
  17. package/test/business.test.ts +282 -0
  18. package/test/dollar.test.ts +270 -0
  19. package/test/entities.test.ts +628 -0
  20. package/test/financials.test.ts +539 -0
  21. package/test/goals.test.ts +451 -0
  22. package/{src → test}/index.test.ts +1 -1
  23. package/test/kpis.test.ts +440 -0
  24. package/test/metrics.test.ts +744 -0
  25. package/test/okrs.test.ts +741 -0
  26. package/test/organization.test.ts +548 -0
  27. package/test/process.test.ts +503 -0
  28. package/test/product.test.ts +430 -0
  29. package/test/queries.test.ts +556 -0
  30. package/test/roles.test.ts +546 -0
  31. package/test/service.test.ts +450 -0
  32. package/test/types.test.ts +1141 -0
  33. package/test/vision.test.ts +214 -0
  34. package/test/workflow.test.ts +501 -0
  35. package/vitest.config.ts +47 -0
  36. package/.turbo/turbo-build.log +0 -5
  37. package/dist/business.d.ts +0 -62
  38. package/dist/business.d.ts.map +0 -1
  39. package/dist/business.js +0 -109
  40. package/dist/business.js.map +0 -1
  41. package/dist/dollar.d.ts +0 -60
  42. package/dist/dollar.d.ts.map +0 -1
  43. package/dist/dollar.js +0 -107
  44. package/dist/dollar.js.map +0 -1
  45. package/dist/entities/assets.d.ts +0 -21
  46. package/dist/entities/assets.d.ts.map +0 -1
  47. package/dist/entities/assets.js +0 -323
  48. package/dist/entities/assets.js.map +0 -1
  49. package/dist/entities/business.d.ts +0 -36
  50. package/dist/entities/business.d.ts.map +0 -1
  51. package/dist/entities/business.js +0 -370
  52. package/dist/entities/business.js.map +0 -1
  53. package/dist/entities/communication.d.ts +0 -21
  54. package/dist/entities/communication.d.ts.map +0 -1
  55. package/dist/entities/communication.js +0 -255
  56. package/dist/entities/communication.js.map +0 -1
  57. package/dist/entities/customers.d.ts +0 -58
  58. package/dist/entities/customers.d.ts.map +0 -1
  59. package/dist/entities/customers.js +0 -989
  60. package/dist/entities/customers.js.map +0 -1
  61. package/dist/entities/financials.d.ts +0 -59
  62. package/dist/entities/financials.d.ts.map +0 -1
  63. package/dist/entities/financials.js +0 -932
  64. package/dist/entities/financials.js.map +0 -1
  65. package/dist/entities/goals.d.ts +0 -58
  66. package/dist/entities/goals.d.ts.map +0 -1
  67. package/dist/entities/goals.js +0 -800
  68. package/dist/entities/goals.js.map +0 -1
  69. package/dist/entities/index.d.ts +0 -299
  70. package/dist/entities/index.d.ts.map +0 -1
  71. package/dist/entities/index.js +0 -198
  72. package/dist/entities/index.js.map +0 -1
  73. package/dist/entities/legal.d.ts +0 -21
  74. package/dist/entities/legal.d.ts.map +0 -1
  75. package/dist/entities/legal.js +0 -301
  76. package/dist/entities/legal.js.map +0 -1
  77. package/dist/entities/market.d.ts +0 -21
  78. package/dist/entities/market.d.ts.map +0 -1
  79. package/dist/entities/market.js +0 -301
  80. package/dist/entities/market.js.map +0 -1
  81. package/dist/entities/marketing.d.ts +0 -67
  82. package/dist/entities/marketing.d.ts.map +0 -1
  83. package/dist/entities/marketing.js +0 -1157
  84. package/dist/entities/marketing.js.map +0 -1
  85. package/dist/entities/offerings.d.ts +0 -51
  86. package/dist/entities/offerings.d.ts.map +0 -1
  87. package/dist/entities/offerings.js +0 -727
  88. package/dist/entities/offerings.js.map +0 -1
  89. package/dist/entities/operations.d.ts +0 -58
  90. package/dist/entities/operations.d.ts.map +0 -1
  91. package/dist/entities/operations.js +0 -787
  92. package/dist/entities/operations.js.map +0 -1
  93. package/dist/entities/organization.d.ts +0 -57
  94. package/dist/entities/organization.d.ts.map +0 -1
  95. package/dist/entities/organization.js +0 -807
  96. package/dist/entities/organization.js.map +0 -1
  97. package/dist/entities/partnerships.d.ts +0 -21
  98. package/dist/entities/partnerships.d.ts.map +0 -1
  99. package/dist/entities/partnerships.js +0 -300
  100. package/dist/entities/partnerships.js.map +0 -1
  101. package/dist/entities/planning.d.ts +0 -87
  102. package/dist/entities/planning.d.ts.map +0 -1
  103. package/dist/entities/planning.js +0 -271
  104. package/dist/entities/planning.js.map +0 -1
  105. package/dist/entities/projects.d.ts +0 -25
  106. package/dist/entities/projects.d.ts.map +0 -1
  107. package/dist/entities/projects.js +0 -349
  108. package/dist/entities/projects.js.map +0 -1
  109. package/dist/entities/risk.d.ts +0 -21
  110. package/dist/entities/risk.d.ts.map +0 -1
  111. package/dist/entities/risk.js +0 -293
  112. package/dist/entities/risk.js.map +0 -1
  113. package/dist/entities/sales.d.ts +0 -72
  114. package/dist/entities/sales.d.ts.map +0 -1
  115. package/dist/entities/sales.js +0 -1248
  116. package/dist/entities/sales.js.map +0 -1
  117. package/dist/financials.d.ts +0 -130
  118. package/dist/financials.d.ts.map +0 -1
  119. package/dist/financials.js +0 -297
  120. package/dist/financials.js.map +0 -1
  121. package/dist/goals.d.ts +0 -87
  122. package/dist/goals.d.ts.map +0 -1
  123. package/dist/goals.js +0 -215
  124. package/dist/goals.js.map +0 -1
  125. package/dist/index.d.ts +0 -97
  126. package/dist/index.d.ts.map +0 -1
  127. package/dist/index.js +0 -132
  128. package/dist/index.js.map +0 -1
  129. package/dist/kpis.d.ts +0 -118
  130. package/dist/kpis.d.ts.map +0 -1
  131. package/dist/kpis.js +0 -232
  132. package/dist/kpis.js.map +0 -1
  133. package/dist/metrics.d.ts +0 -448
  134. package/dist/metrics.d.ts.map +0 -1
  135. package/dist/metrics.js +0 -325
  136. package/dist/metrics.js.map +0 -1
  137. package/dist/okrs.d.ts +0 -123
  138. package/dist/okrs.d.ts.map +0 -1
  139. package/dist/okrs.js +0 -269
  140. package/dist/okrs.js.map +0 -1
  141. package/dist/organization.d.ts +0 -585
  142. package/dist/organization.d.ts.map +0 -1
  143. package/dist/organization.js +0 -173
  144. package/dist/organization.js.map +0 -1
  145. package/dist/process.d.ts +0 -112
  146. package/dist/process.d.ts.map +0 -1
  147. package/dist/process.js +0 -241
  148. package/dist/process.js.map +0 -1
  149. package/dist/product.d.ts +0 -85
  150. package/dist/product.d.ts.map +0 -1
  151. package/dist/product.js +0 -145
  152. package/dist/product.js.map +0 -1
  153. package/dist/queries.d.ts +0 -304
  154. package/dist/queries.d.ts.map +0 -1
  155. package/dist/queries.js +0 -415
  156. package/dist/queries.js.map +0 -1
  157. package/dist/roles.d.ts +0 -340
  158. package/dist/roles.d.ts.map +0 -1
  159. package/dist/roles.js +0 -255
  160. package/dist/roles.js.map +0 -1
  161. package/dist/service.d.ts +0 -61
  162. package/dist/service.d.ts.map +0 -1
  163. package/dist/service.js +0 -140
  164. package/dist/service.js.map +0 -1
  165. package/dist/types.d.ts +0 -459
  166. package/dist/types.d.ts.map +0 -1
  167. package/dist/types.js +0 -5
  168. package/dist/types.js.map +0 -1
  169. package/dist/vision.d.ts +0 -38
  170. package/dist/vision.d.ts.map +0 -1
  171. package/dist/vision.js +0 -68
  172. package/dist/vision.js.map +0 -1
  173. package/dist/workflow.d.ts +0 -115
  174. package/dist/workflow.d.ts.map +0 -1
  175. package/dist/workflow.js +0 -247
  176. package/dist/workflow.js.map +0 -1
  177. package/src/business.js +0 -108
  178. package/src/dollar.js +0 -106
  179. package/src/entities/assets.js +0 -322
  180. package/src/entities/business.js +0 -369
  181. package/src/entities/communication.js +0 -254
  182. package/src/entities/customers.js +0 -988
  183. package/src/entities/financials.js +0 -931
  184. package/src/entities/goals.js +0 -799
  185. package/src/entities/index.js +0 -197
  186. package/src/entities/legal.js +0 -300
  187. package/src/entities/market.js +0 -300
  188. package/src/entities/marketing.js +0 -1156
  189. package/src/entities/offerings.js +0 -726
  190. package/src/entities/operations.js +0 -786
  191. package/src/entities/organization.js +0 -806
  192. package/src/entities/partnerships.js +0 -299
  193. package/src/entities/planning.js +0 -270
  194. package/src/entities/projects.js +0 -348
  195. package/src/entities/risk.js +0 -292
  196. package/src/entities/sales.js +0 -1247
  197. package/src/financials.js +0 -296
  198. package/src/goals.js +0 -214
  199. package/src/index.js +0 -131
  200. package/src/index.test.js +0 -274
  201. package/src/kpis.js +0 -231
  202. package/src/metrics.js +0 -324
  203. package/src/okrs.js +0 -268
  204. package/src/organization.js +0 -172
  205. package/src/process.js +0 -240
  206. package/src/product.js +0 -144
  207. package/src/queries.js +0 -414
  208. package/src/roles.js +0 -254
  209. package/src/service.js +0 -139
  210. package/src/types.js +0 -4
  211. package/src/vision.js +0 -67
  212. package/src/workflow.js +0 -246
package/src/index.test.js DELETED
@@ -1,274 +0,0 @@
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 DELETED
@@ -1,231 +0,0 @@
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
- }