@reekon-tools/boldr-utils 1.4.3 → 1.4.5

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
+ };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { evaluateFormula, createFormulaScope, createEnhancedFormulaScope, clearFormulaCache, type EnhancedMapping, type FormulaDefinition, type FormulaEvaluationOptions, } from './formulas/evaluateFormula.js';
2
+ export { calculateFormula } from './formulas/calculateFormula.js';
2
3
  export { convertMicrometers } from './utils/micrometersToUnit.js';
3
4
  export { parseMeasurement } from './utils/parseMeasurement.js';
4
5
  export { useParseMeasurement } from './hooks/useParseMeasurement.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { evaluateFormula, createFormulaScope, createEnhancedFormulaScope, clearFormulaCache, } from './formulas/evaluateFormula.js';
2
+ export { calculateFormula } from './formulas/calculateFormula.js';
2
3
  export { convertMicrometers } from './utils/micrometersToUnit.js';
3
4
  export { parseMeasurement } from './utils/parseMeasurement.js';
4
5
  export { useParseMeasurement } from './hooks/useParseMeasurement.js';
@@ -156,6 +156,7 @@ export interface Formula extends FirestoreDoc, Timestamps {
156
156
  expression: string;
157
157
  name: string;
158
158
  varaibles: string[];
159
+ variableToColumnMap: Record<string, any>;
159
160
  }
160
161
  export interface ColumnConfig {
161
162
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reekon-tools/boldr-utils",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Shared utilities for formulas and measurement conversion used in Reekon apps",
5
5
  "author": "REEKON Tools",
6
6
  "license": "MIT",