Skip to content

Commit a40858f

Browse files
author
Kubit
committed
Include translate utility
1 parent fad6e50 commit a40858f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { translateValue } from '../translateValue';
2+
3+
describe('translateValue', () => {
4+
it('should translate a value from one range to another', () => {
5+
const result = translateValue({
6+
inputMin: 0,
7+
inputMax: 100,
8+
outputMin: 0,
9+
outputMax: 1,
10+
value: 50,
11+
});
12+
expect(result).toBe(0.5);
13+
});
14+
15+
it('should translate a value from one range to another with different ranges', () => {
16+
const result = translateValue({
17+
inputMin: 0,
18+
inputMax: 200,
19+
outputMin: 0,
20+
outputMax: 2,
21+
value: 100,
22+
});
23+
expect(result).toBe(1);
24+
});
25+
26+
it('should constrain the output value within the output range', () => {
27+
const result = translateValue({
28+
inputMin: 0,
29+
inputMax: 100,
30+
outputMin: 0,
31+
outputMax: 1,
32+
value: 150,
33+
});
34+
expect(result).toBe(1);
35+
});
36+
37+
it('should handle negative input and output ranges', () => {
38+
const result = translateValue({
39+
inputMin: -100,
40+
inputMax: 100,
41+
outputMin: -1,
42+
outputMax: 1,
43+
value: 0,
44+
});
45+
expect(result).toBe(0);
46+
});
47+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Translates a value from one range to another.
3+
* This is useful in scenarios where you need to map a value from one scale
4+
* to another, such as converting a temperature from Celsius to Fahrenheit,
5+
* or mapping a slider value to a different range.
6+
*
7+
* @param {Object} params - The parameters for the translation.
8+
* @param {number} params.inputMin - The minimum value of the input range.
9+
* @param {number} params.inputMax - The maximum value of the input range.
10+
* @param {number} params.outputMin - The minimum value of the output range.
11+
* @param {number} params.outputMax - The maximum value of the output range.
12+
* @param {number} params.value - The value to be translated.
13+
* @returns {number} - The translated value, constrained within the output range.
14+
*
15+
* * @example
16+
* // Translates 50 from the range [0, 100] to the range [5, 10]
17+
* const output = translateValue({inputMin: 0, inputMax: 100, outputMin: 5, outputMax: 10, value: 50});
18+
* console.log(output); // 7.5
19+
*/
20+
export const translateValue = ({
21+
inputMin,
22+
inputMax,
23+
outputMin,
24+
outputMax,
25+
value,
26+
}: {
27+
inputMin: number;
28+
inputMax: number;
29+
outputMin: number;
30+
outputMax: number;
31+
value: number;
32+
}): number => {
33+
// Calculate Input Range
34+
const inputRange = inputMax - inputMin;
35+
// Calculate Output Range
36+
const outputRange = outputMax - outputMin;
37+
// Scale the Input Value
38+
const scaledInputValue = (value - inputMin) / inputRange;
39+
// Translate to Output Range
40+
let outputValue = outputMin + scaledInputValue * outputRange;
41+
// Constrain the Output Value
42+
outputValue = Math.min(outputMax, Math.max(outputMin, outputValue));
43+
44+
return outputValue;
45+
};

0 commit comments

Comments
 (0)