@reekon-tools/boldr-utils 1.3.5 → 1.3.7

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.
package/README.md CHANGED
@@ -10,3 +10,11 @@ BOLDR Utils is a collection of types and utility functions written to be used ac
10
10
  - Be consumable in Remix (Node/ESM, often using Vite or Webpack)
11
11
  - Be written in TypeScript
12
12
  - Be published cleanly to npm
13
+
14
+ ## Publishing
15
+
16
+ To publish the package to NPM, be sure to build it. Then, run
17
+
18
+ ```
19
+ npm publish --access public
20
+ ```
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { convertMicrometers } from './utils/micrometersToUnit.js';
3
3
  export { parseMeasurement } from './utils/parseMeasurement.js';
4
4
  export { useParseMeasurement } from './hooks/useParseMeasurement.js';
5
5
  export * from './types/firestore.js';
6
+ export { getToleranceColor, calculateDeviationPercentage, isWithinTolerance, generateToleranceGradient, createDefaultToleranceThresholds, DEFAULT_TOLERANCE_COLORS, type ToleranceThreshold, type ToleranceConfig, } from './utils/tolerance.js';
package/dist/index.js CHANGED
@@ -3,3 +3,4 @@ export { convertMicrometers } from './utils/micrometersToUnit.js';
3
3
  export { parseMeasurement } from './utils/parseMeasurement.js';
4
4
  export { useParseMeasurement } from './hooks/useParseMeasurement.js';
5
5
  export * from './types/firestore.js';
6
+ export { getToleranceColor, calculateDeviationPercentage, isWithinTolerance, generateToleranceGradient, createDefaultToleranceThresholds, DEFAULT_TOLERANCE_COLORS, } from './utils/tolerance.js';
@@ -0,0 +1,62 @@
1
+ export interface ToleranceThreshold {
2
+ id: string;
3
+ percentage: number;
4
+ color: string;
5
+ }
6
+ export interface ToleranceConfig {
7
+ thresholds: ToleranceThreshold[];
8
+ updatedAt?: Date | {
9
+ toDate(): Date;
10
+ };
11
+ }
12
+ /**
13
+ * Default colors for tolerance visualization
14
+ */
15
+ export declare const DEFAULT_TOLERANCE_COLORS: {
16
+ readonly IN_TOLERANCE: "#22c55e";
17
+ readonly OUT_OF_TOLERANCE: "#ef4444";
18
+ };
19
+ /**
20
+ * Calculates the appropriate color for a measurement based on tolerance thresholds
21
+ *
22
+ * @param actualValue - The measured value
23
+ * @param target - The target/nominal value
24
+ * @param min - The minimum acceptable value (used as negative tolerance from target)
25
+ * @param max - The maximum acceptable value (used as positive tolerance from target)
26
+ * @param toleranceConfig - Optional tolerance configuration with custom thresholds
27
+ * @returns The color hex string to use for the measurement
28
+ */
29
+ export declare function getToleranceColor(actualValue: number, target: number, min: number, max: number, toleranceConfig?: ToleranceConfig | null): string;
30
+ /**
31
+ * Calculates the deviation percentage of a measurement from its target
32
+ *
33
+ * @param actualValue - The measured value
34
+ * @param target - The target/nominal value
35
+ * @param min - The minimum acceptable value
36
+ * @param max - The maximum acceptable value
37
+ * @returns The deviation as a percentage of the tolerance range
38
+ */
39
+ export declare function calculateDeviationPercentage(actualValue: number, target: number, min: number, max: number): number;
40
+ /**
41
+ * Checks if a measurement is within tolerance bounds
42
+ *
43
+ * @param actualValue - The measured value
44
+ * @param target - The target/nominal value
45
+ * @param min - The minimum acceptable value
46
+ * @param max - The maximum acceptable value
47
+ * @returns True if the measurement is within bounds
48
+ */
49
+ export declare function isWithinTolerance(actualValue: number, target: number, min: number, max: number): boolean;
50
+ /**
51
+ * Generates a CSS gradient string for visualizing tolerance thresholds
52
+ *
53
+ * @param thresholds - Array of tolerance thresholds
54
+ * @returns CSS linear-gradient string
55
+ */
56
+ export declare function generateToleranceGradient(thresholds: ToleranceThreshold[]): string;
57
+ /**
58
+ * Creates default tolerance thresholds
59
+ *
60
+ * @returns Array of default tolerance thresholds
61
+ */
62
+ export declare function createDefaultToleranceThresholds(): ToleranceThreshold[];
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Default colors for tolerance visualization
3
+ */
4
+ export const DEFAULT_TOLERANCE_COLORS = {
5
+ IN_TOLERANCE: '#22c55e', // Green
6
+ OUT_OF_TOLERANCE: '#ef4444', // Red
7
+ };
8
+ /**
9
+ * Calculates the appropriate color for a measurement based on tolerance thresholds
10
+ *
11
+ * @param actualValue - The measured value
12
+ * @param target - The target/nominal value
13
+ * @param min - The minimum acceptable value (used as negative tolerance from target)
14
+ * @param max - The maximum acceptable value (used as positive tolerance from target)
15
+ * @param toleranceConfig - Optional tolerance configuration with custom thresholds
16
+ * @returns The color hex string to use for the measurement
17
+ */
18
+ export function getToleranceColor(actualValue, target, min, max, toleranceConfig) {
19
+ // If no tolerance config provided, use simple green/red logic
20
+ if (!toleranceConfig?.thresholds || toleranceConfig.thresholds.length === 0) {
21
+ const isWithinTolerance = actualValue >= target - min && actualValue <= target + max;
22
+ return isWithinTolerance
23
+ ? DEFAULT_TOLERANCE_COLORS.IN_TOLERANCE
24
+ : DEFAULT_TOLERANCE_COLORS.OUT_OF_TOLERANCE;
25
+ }
26
+ // Calculate deviation percentage
27
+ const deviation = Math.abs(actualValue - target);
28
+ const toleranceRange = max + min; // Fixed: total tolerance range
29
+ const deviationPercentage = toleranceRange > 0 ? (deviation / toleranceRange) * 100 : 0;
30
+ // Sort thresholds by percentage to ensure correct evaluation order
31
+ const sortedThresholds = toleranceConfig.thresholds.sort((a, b) => a.percentage - b.percentage);
32
+ // If deviation is below the first threshold, it's in tolerance (green)
33
+ if (deviationPercentage < sortedThresholds[0].percentage) {
34
+ return DEFAULT_TOLERANCE_COLORS.IN_TOLERANCE;
35
+ }
36
+ // Find the highest threshold that the deviation exceeds
37
+ let applicableColor = sortedThresholds[0].color;
38
+ for (const threshold of sortedThresholds) {
39
+ if (deviationPercentage >= threshold.percentage) {
40
+ applicableColor = threshold.color;
41
+ }
42
+ }
43
+ return applicableColor;
44
+ }
45
+ /**
46
+ * Calculates the deviation percentage of a measurement from its target
47
+ *
48
+ * @param actualValue - The measured value
49
+ * @param target - The target/nominal value
50
+ * @param min - The minimum acceptable value
51
+ * @param max - The maximum acceptable value
52
+ * @returns The deviation as a percentage of the tolerance range
53
+ */
54
+ export function calculateDeviationPercentage(actualValue, target, min, max) {
55
+ const deviation = Math.abs(actualValue - target);
56
+ const toleranceRange = max + min; // Fixed: total tolerance range
57
+ return toleranceRange > 0 ? (deviation / toleranceRange) * 100 : 0;
58
+ }
59
+ /**
60
+ * Checks if a measurement is within tolerance bounds
61
+ *
62
+ * @param actualValue - The measured value
63
+ * @param target - The target/nominal value
64
+ * @param min - The minimum acceptable value
65
+ * @param max - The maximum acceptable value
66
+ * @returns True if the measurement is within bounds
67
+ */
68
+ export function isWithinTolerance(actualValue, target, min, max) {
69
+ return actualValue >= target - min && actualValue <= target + max;
70
+ }
71
+ /**
72
+ * Generates a CSS gradient string for visualizing tolerance thresholds
73
+ *
74
+ * @param thresholds - Array of tolerance thresholds
75
+ * @returns CSS linear-gradient string
76
+ */
77
+ export function generateToleranceGradient(thresholds) {
78
+ const sortedThresholds = [...thresholds].sort((a, b) => a.percentage - b.percentage);
79
+ if (sortedThresholds.length === 0) {
80
+ return `linear-gradient(to right, ${DEFAULT_TOLERANCE_COLORS.IN_TOLERANCE} 100%)`;
81
+ }
82
+ const gradientStops = [];
83
+ // Green from 0% to first threshold
84
+ const firstThreshold = sortedThresholds[0];
85
+ if (firstThreshold.percentage > 0) {
86
+ gradientStops.push(`${DEFAULT_TOLERANCE_COLORS.IN_TOLERANCE} 0%`);
87
+ gradientStops.push(`${DEFAULT_TOLERANCE_COLORS.IN_TOLERANCE} ${firstThreshold.percentage}%`);
88
+ }
89
+ // Add each threshold color
90
+ sortedThresholds.forEach((threshold, index) => {
91
+ gradientStops.push(`${threshold.color} ${threshold.percentage}%`);
92
+ // If this isn't the last threshold, add the color up to the next threshold
93
+ const nextThreshold = sortedThresholds[index + 1];
94
+ if (nextThreshold) {
95
+ gradientStops.push(`${threshold.color} ${nextThreshold.percentage}%`);
96
+ }
97
+ else {
98
+ // Last threshold extends to 100%
99
+ gradientStops.push(`${threshold.color} 100%`);
100
+ }
101
+ });
102
+ return `linear-gradient(to right, ${gradientStops.join(', ')})`;
103
+ }
104
+ /**
105
+ * Creates default tolerance thresholds
106
+ *
107
+ * @returns Array of default tolerance thresholds
108
+ */
109
+ export function createDefaultToleranceThresholds() {
110
+ return [
111
+ { id: '1', percentage: 25, color: '#ef4444' }, // Red at 25%
112
+ ];
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reekon-tools/boldr-utils",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "Shared utilities for formulas and measurement conversion used in Reekon apps",
5
5
  "author": "REEKON Tools",
6
6
  "license": "MIT",