@reekon-tools/boldr-utils 1.4.2 → 1.4.4

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.
@@ -0,0 +1,2 @@
1
+ import { Formula, Measurement, Units } from '../types/firestore.js';
2
+ export declare const calculateFormula: (formula: Formula, formulas: Formula[], columns: Record<string, any>, measurements: Measurement[], unit: Units) => string | number | null;
@@ -0,0 +1,81 @@
1
+ import { evaluateFormula } from './evaluateFormula.js';
2
+ export const calculateFormula = (formula, formulas, columns, measurements, unit) => {
3
+ // Convert Firestore Map to plain object if necessary
4
+ let variableToColumnMap = {};
5
+ if (formula.variableToColumnMap) {
6
+ if (formula.variableToColumnMap instanceof Map) {
7
+ // Convert Firestore Map to plain object
8
+ variableToColumnMap = Object.fromEntries(formula.variableToColumnMap);
9
+ }
10
+ else if (typeof formula.variableToColumnMap === 'object') {
11
+ // Already a plain object
12
+ variableToColumnMap = formula.variableToColumnMap;
13
+ }
14
+ }
15
+ if (Object.keys(variableToColumnMap).length === 0) {
16
+ return null;
17
+ }
18
+ // Build valueMap from group.columns and measurementMap
19
+ const valueMap = {};
20
+ // Map column IDs to their actual measurement values
21
+ for (const [columnId, measurementId] of columns.entries()) {
22
+ const measurement = measurements.find((m) => m.id === measurementId);
23
+ if (measurement?.value !== undefined) {
24
+ valueMap[columnId] = measurement.value;
25
+ }
26
+ else {
27
+ const numericValue = typeof measurementId === 'number'
28
+ ? measurementId
29
+ : parseFloat(measurementId || '0');
30
+ valueMap[columnId] = isNaN(numericValue) ? 0 : numericValue;
31
+ }
32
+ }
33
+ // Convert formulas to the expected FormulaDefinition format
34
+ const formulaDefinitions = formulas
35
+ .filter((f) => f.variableToColumnMap) // Only include formulas with new schema
36
+ .map((f) => {
37
+ const mappings = {};
38
+ // Convert Firestore Map to plain object if necessary
39
+ let fVariableToColumnMap = {};
40
+ if (f.variableToColumnMap instanceof Map) {
41
+ fVariableToColumnMap = Object.fromEntries(f.variableToColumnMap);
42
+ }
43
+ else if (typeof f.variableToColumnMap === 'object') {
44
+ fVariableToColumnMap = f.variableToColumnMap;
45
+ }
46
+ for (const [variable, mapping] of Object.entries(fVariableToColumnMap)) {
47
+ if (typeof mapping === 'string') {
48
+ // Handle transition period - old format in new structure
49
+ mappings[variable] = { id: mapping, type: 'column' };
50
+ }
51
+ else {
52
+ // New format: { id: string; type: 'column' | 'formula' }
53
+ mappings[variable] = mapping;
54
+ }
55
+ }
56
+ return {
57
+ id: f.id,
58
+ name: f.name,
59
+ expression: f.expression,
60
+ variableToColumnMap: mappings,
61
+ };
62
+ });
63
+ // Convert the current formula's mappings
64
+ const currentMappings = {};
65
+ for (const [variable, mapping] of Object.entries(variableToColumnMap)) {
66
+ if (typeof mapping === 'string') {
67
+ currentMappings[variable] = { id: mapping, type: 'column' };
68
+ }
69
+ else {
70
+ currentMappings[variable] = mapping;
71
+ }
72
+ }
73
+ const result = evaluateFormula({
74
+ expression: formula.expression,
75
+ mappings: currentMappings,
76
+ valueMap,
77
+ unit: unit,
78
+ formulas: formulaDefinitions,
79
+ });
80
+ return result;
81
+ };
@@ -137,6 +137,9 @@ export interface Group extends FirestoreDoc, Timestamps {
137
137
  };
138
138
  };
139
139
  diagramFileId?: string;
140
+ formula?: any;
141
+ isCompleted?: boolean;
142
+ type?: string;
140
143
  }
141
144
  export declare enum ColumnType {
142
145
  Text = "text",
@@ -153,6 +156,7 @@ export interface Formula extends FirestoreDoc, Timestamps {
153
156
  expression: string;
154
157
  name: string;
155
158
  varaibles: string[];
159
+ variableToColumnMap: Record<string, any>;
156
160
  }
157
161
  export interface ColumnConfig {
158
162
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reekon-tools/boldr-utils",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Shared utilities for formulas and measurement conversion used in Reekon apps",
5
5
  "author": "REEKON Tools",
6
6
  "license": "MIT",