business-as-code 2.1.3 → 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 (260) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -0
  3. package/package.json +16 -13
  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/LICENSE +0 -21
  38. package/dist/business.d.ts +0 -62
  39. package/dist/business.d.ts.map +0 -1
  40. package/dist/business.js +0 -109
  41. package/dist/business.js.map +0 -1
  42. package/dist/canvas/activities.d.ts +0 -19
  43. package/dist/canvas/activities.d.ts.map +0 -1
  44. package/dist/canvas/activities.js +0 -20
  45. package/dist/canvas/activities.js.map +0 -1
  46. package/dist/canvas/channels.d.ts +0 -20
  47. package/dist/canvas/channels.d.ts.map +0 -1
  48. package/dist/canvas/channels.js +0 -21
  49. package/dist/canvas/channels.js.map +0 -1
  50. package/dist/canvas/relationships.d.ts +0 -20
  51. package/dist/canvas/relationships.d.ts.map +0 -1
  52. package/dist/canvas/relationships.js +0 -21
  53. package/dist/canvas/relationships.js.map +0 -1
  54. package/dist/canvas/resources.d.ts +0 -20
  55. package/dist/canvas/resources.d.ts.map +0 -1
  56. package/dist/canvas/resources.js +0 -30
  57. package/dist/canvas/resources.js.map +0 -1
  58. package/dist/canvas/revenue.d.ts +0 -22
  59. package/dist/canvas/revenue.d.ts.map +0 -1
  60. package/dist/canvas/revenue.js +0 -30
  61. package/dist/canvas/revenue.js.map +0 -1
  62. package/dist/canvas/segments.d.ts +0 -20
  63. package/dist/canvas/segments.d.ts.map +0 -1
  64. package/dist/canvas/segments.js +0 -28
  65. package/dist/canvas/segments.js.map +0 -1
  66. package/dist/canvas/types.d.ts +0 -232
  67. package/dist/canvas/types.d.ts.map +0 -1
  68. package/dist/canvas/types.js +0 -8
  69. package/dist/canvas/types.js.map +0 -1
  70. package/dist/canvas/value.d.ts +0 -20
  71. package/dist/canvas/value.d.ts.map +0 -1
  72. package/dist/canvas/value.js +0 -21
  73. package/dist/canvas/value.js.map +0 -1
  74. package/dist/dollar.d.ts +0 -60
  75. package/dist/dollar.d.ts.map +0 -1
  76. package/dist/dollar.js +0 -107
  77. package/dist/dollar.js.map +0 -1
  78. package/dist/entities/assets.d.ts +0 -21
  79. package/dist/entities/assets.d.ts.map +0 -1
  80. package/dist/entities/assets.js +0 -323
  81. package/dist/entities/assets.js.map +0 -1
  82. package/dist/entities/business.d.ts +0 -36
  83. package/dist/entities/business.d.ts.map +0 -1
  84. package/dist/entities/business.js +0 -370
  85. package/dist/entities/business.js.map +0 -1
  86. package/dist/entities/communication.d.ts +0 -21
  87. package/dist/entities/communication.d.ts.map +0 -1
  88. package/dist/entities/communication.js +0 -255
  89. package/dist/entities/communication.js.map +0 -1
  90. package/dist/entities/customers.d.ts +0 -58
  91. package/dist/entities/customers.d.ts.map +0 -1
  92. package/dist/entities/customers.js +0 -989
  93. package/dist/entities/customers.js.map +0 -1
  94. package/dist/entities/financials.d.ts +0 -59
  95. package/dist/entities/financials.d.ts.map +0 -1
  96. package/dist/entities/financials.js +0 -932
  97. package/dist/entities/financials.js.map +0 -1
  98. package/dist/entities/goals.d.ts +0 -58
  99. package/dist/entities/goals.d.ts.map +0 -1
  100. package/dist/entities/goals.js +0 -800
  101. package/dist/entities/goals.js.map +0 -1
  102. package/dist/entities/index.d.ts +0 -299
  103. package/dist/entities/index.d.ts.map +0 -1
  104. package/dist/entities/index.js +0 -198
  105. package/dist/entities/index.js.map +0 -1
  106. package/dist/entities/legal.d.ts +0 -21
  107. package/dist/entities/legal.d.ts.map +0 -1
  108. package/dist/entities/legal.js +0 -301
  109. package/dist/entities/legal.js.map +0 -1
  110. package/dist/entities/market.d.ts +0 -21
  111. package/dist/entities/market.d.ts.map +0 -1
  112. package/dist/entities/market.js +0 -301
  113. package/dist/entities/market.js.map +0 -1
  114. package/dist/entities/marketing.d.ts +0 -67
  115. package/dist/entities/marketing.d.ts.map +0 -1
  116. package/dist/entities/marketing.js +0 -1157
  117. package/dist/entities/marketing.js.map +0 -1
  118. package/dist/entities/offerings.d.ts +0 -51
  119. package/dist/entities/offerings.d.ts.map +0 -1
  120. package/dist/entities/offerings.js +0 -727
  121. package/dist/entities/offerings.js.map +0 -1
  122. package/dist/entities/operations.d.ts +0 -58
  123. package/dist/entities/operations.d.ts.map +0 -1
  124. package/dist/entities/operations.js +0 -787
  125. package/dist/entities/operations.js.map +0 -1
  126. package/dist/entities/organization.d.ts +0 -57
  127. package/dist/entities/organization.d.ts.map +0 -1
  128. package/dist/entities/organization.js +0 -807
  129. package/dist/entities/organization.js.map +0 -1
  130. package/dist/entities/partnerships.d.ts +0 -21
  131. package/dist/entities/partnerships.d.ts.map +0 -1
  132. package/dist/entities/partnerships.js +0 -300
  133. package/dist/entities/partnerships.js.map +0 -1
  134. package/dist/entities/planning.d.ts +0 -0
  135. package/dist/entities/planning.d.ts.map +0 -1
  136. package/dist/entities/planning.js +0 -271
  137. package/dist/entities/planning.js.map +0 -1
  138. package/dist/entities/projects.d.ts +0 -25
  139. package/dist/entities/projects.d.ts.map +0 -1
  140. package/dist/entities/projects.js +0 -349
  141. package/dist/entities/projects.js.map +0 -1
  142. package/dist/entities/risk.d.ts +0 -21
  143. package/dist/entities/risk.d.ts.map +0 -1
  144. package/dist/entities/risk.js +0 -293
  145. package/dist/entities/risk.js.map +0 -1
  146. package/dist/entities/sales.d.ts +0 -72
  147. package/dist/entities/sales.d.ts.map +0 -1
  148. package/dist/entities/sales.js +0 -1248
  149. package/dist/entities/sales.js.map +0 -1
  150. package/dist/financials.d.ts +0 -130
  151. package/dist/financials.d.ts.map +0 -1
  152. package/dist/financials.js +0 -297
  153. package/dist/financials.js.map +0 -1
  154. package/dist/goals.d.ts +0 -87
  155. package/dist/goals.d.ts.map +0 -1
  156. package/dist/goals.js +0 -215
  157. package/dist/goals.js.map +0 -1
  158. package/dist/index.d.ts +0 -97
  159. package/dist/index.d.ts.map +0 -1
  160. package/dist/index.js +0 -132
  161. package/dist/index.js.map +0 -1
  162. package/dist/kpis.d.ts +0 -118
  163. package/dist/kpis.d.ts.map +0 -1
  164. package/dist/kpis.js +0 -232
  165. package/dist/kpis.js.map +0 -1
  166. package/dist/metrics.d.ts +0 -448
  167. package/dist/metrics.d.ts.map +0 -1
  168. package/dist/metrics.js +0 -325
  169. package/dist/metrics.js.map +0 -1
  170. package/dist/okrs.d.ts +0 -123
  171. package/dist/okrs.d.ts.map +0 -1
  172. package/dist/okrs.js +0 -269
  173. package/dist/okrs.js.map +0 -1
  174. package/dist/organization.d.ts +0 -585
  175. package/dist/organization.d.ts.map +0 -1
  176. package/dist/organization.js +0 -173
  177. package/dist/organization.js.map +0 -1
  178. package/dist/process.d.ts +0 -112
  179. package/dist/process.d.ts.map +0 -1
  180. package/dist/process.js +0 -241
  181. package/dist/process.js.map +0 -1
  182. package/dist/product.d.ts +0 -85
  183. package/dist/product.d.ts.map +0 -1
  184. package/dist/product.js +0 -145
  185. package/dist/product.js.map +0 -1
  186. package/dist/queries.d.ts +0 -304
  187. package/dist/queries.d.ts.map +0 -1
  188. package/dist/queries.js +0 -415
  189. package/dist/queries.js.map +0 -1
  190. package/dist/roles.d.ts +0 -340
  191. package/dist/roles.d.ts.map +0 -1
  192. package/dist/roles.js +0 -255
  193. package/dist/roles.js.map +0 -1
  194. package/dist/service.d.ts +0 -61
  195. package/dist/service.d.ts.map +0 -1
  196. package/dist/service.js +0 -140
  197. package/dist/service.js.map +0 -1
  198. package/dist/types.d.ts +0 -459
  199. package/dist/types.d.ts.map +0 -1
  200. package/dist/types.js +0 -5
  201. package/dist/types.js.map +0 -1
  202. package/dist/vision.d.ts +0 -38
  203. package/dist/vision.d.ts.map +0 -1
  204. package/dist/vision.js +0 -68
  205. package/dist/vision.js.map +0 -1
  206. package/dist/workflow.d.ts +0 -115
  207. package/dist/workflow.d.ts.map +0 -1
  208. package/dist/workflow.js +0 -247
  209. package/dist/workflow.js.map +0 -1
  210. package/src/business.js +0 -108
  211. package/src/canvas/activities.ts +0 -32
  212. package/src/canvas/canvas.ts +0 -482
  213. package/src/canvas/channels.ts +0 -34
  214. package/src/canvas/costs.ts +0 -43
  215. package/src/canvas/economics.ts +0 -99
  216. package/src/canvas/index.ts +0 -206
  217. package/src/canvas/partnerships.ts +0 -34
  218. package/src/canvas/projections.ts +0 -141
  219. package/src/canvas/relationships.ts +0 -34
  220. package/src/canvas/resources.ts +0 -43
  221. package/src/canvas/revenue.ts +0 -56
  222. package/src/canvas/segments.ts +0 -42
  223. package/src/canvas/types.ts +0 -363
  224. package/src/canvas/value.ts +0 -34
  225. package/src/dollar.js +0 -106
  226. package/src/entities/assets.js +0 -322
  227. package/src/entities/business.js +0 -369
  228. package/src/entities/communication.js +0 -254
  229. package/src/entities/customers.js +0 -988
  230. package/src/entities/financials.js +0 -931
  231. package/src/entities/goals.js +0 -799
  232. package/src/entities/index.js +0 -197
  233. package/src/entities/legal.js +0 -300
  234. package/src/entities/market.js +0 -300
  235. package/src/entities/marketing.js +0 -1156
  236. package/src/entities/offerings.js +0 -726
  237. package/src/entities/operations.js +0 -786
  238. package/src/entities/organization.js +0 -806
  239. package/src/entities/partnerships.js +0 -299
  240. package/src/entities/planning.js +0 -270
  241. package/src/entities/projects.js +0 -348
  242. package/src/entities/risk.js +0 -292
  243. package/src/entities/sales.js +0 -1247
  244. package/src/financials.js +0 -296
  245. package/src/goals.js +0 -214
  246. package/src/index.js +0 -131
  247. package/src/index.test.js +0 -274
  248. package/src/kpis.js +0 -231
  249. package/src/metrics.js +0 -324
  250. package/src/okrs.js +0 -268
  251. package/src/organization.js +0 -172
  252. package/src/process.js +0 -240
  253. package/src/product.js +0 -144
  254. package/src/queries.js +0 -414
  255. package/src/roles.js +0 -254
  256. package/src/service.js +0 -139
  257. package/src/types.js +0 -4
  258. package/src/vision.js +0 -67
  259. package/src/workflow.js +0 -246
  260. package/tests/canvas.test.ts +0 -842
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
- }