@anselan/maprange
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -7,3 +7,3 @@ "use strict"; | ||
if (!checkValidRanges([inputRange, targetRange])) { | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements, and first element much have smaller value than second'); | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
} | ||
@@ -13,7 +13,17 @@ // let outgoing = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2; | ||
if (clamp === true) { | ||
if (outgoing > targetRange[1]) { | ||
outgoing = targetRange[1]; | ||
if (targetRange[0] < targetRange[1]) { // normal output range | ||
if (outgoing > targetRange[1]) { | ||
outgoing = targetRange[1]; | ||
} | ||
else if (outgoing < targetRange[0]) { | ||
outgoing = targetRange[0]; | ||
} | ||
} | ||
else if (outgoing < targetRange[0]) { | ||
outgoing = targetRange[0]; | ||
else { // inverse output range | ||
if (outgoing < targetRange[1]) { | ||
outgoing = targetRange[1]; | ||
} | ||
else if (outgoing > targetRange[0]) { | ||
outgoing = targetRange[0]; | ||
} | ||
} | ||
@@ -20,0 +30,0 @@ } |
@@ -101,2 +101,28 @@ "use strict"; | ||
}); | ||
test('inverse output ranges with offset input ranges', () => { | ||
const value = 0.75; // halfway in | ||
expect(_1.remap(value, [0.5, 1.0], [0, 1])).toBe(0.5); // sanity, !inverse | ||
expect(_1.remap(value, [0.5, 1.0], [1, 0])).toBe(0.5); | ||
const value2 = 0.5 + 0.125; // a quarter way in | ||
expect(_1.remap(value2, [0.5, 1.0], [0, 1])).toBe(0.25); // sanity, !inverse | ||
expect(_1.remap(value2, [0.5, 1.0], [1, 0])).toBe(0.75); | ||
const value3 = 1.5; // whole step overshoot | ||
expect(_1.remap(value3, [0.5, 1.0], [0, 1])).toBe(2.0); // sanity, !inverse | ||
expect(_1.remap(value3, [0.5, 1.0], [1, 0])).toBe(-1.0); | ||
const value4 = 1.0; // at the end | ||
expect(_1.remap(value4, [0.5, 1.0], [1, 0])).toBe(0); | ||
const value5 = 0.5; // at the beginning | ||
expect(_1.remap(value5, [0.5, 1.0], [1, 0])).toBe(1); | ||
}); | ||
test('inverse output ranges clamped results', () => { | ||
const value = 0.75; // halfway in | ||
expect(_1.remap(value, [0.5, 1.0], [0, 1], true)).toBe(0.5); // sanity, !inverse | ||
expect(_1.remap(value, [0.5, 1.0], [1, 0], true)).toBe(0.5); | ||
const value2 = 0.5 + 0.125; // a quarter way in | ||
expect(_1.remap(value2, [0.5, 1.0], [0, 1], true)).toBe(0.25); // sanity, !inverse | ||
expect(_1.remap(value2, [0.5, 1.0], [1, 0], true)).toBe(0.75); | ||
const value3 = 1.5; // whole step overshoot | ||
expect(_1.remap(value3, [0.5, 1.0], [0, 1], true)).toBe(1.0); // sanity, !inverse | ||
expect(_1.remap(value3, [0.5, 1.0], [1, 0], true)).toBe(0); | ||
}); | ||
test('throw error if elements are equal in either range', () => { | ||
@@ -103,0 +129,0 @@ expect(() => { |
{ | ||
"name": "@anselan/maprange", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "Map values from one range to another", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -116,4 +116,41 @@ import { remap, remapArray, remapCoords } from '.' | ||
}) | ||
}); | ||
test('inverse output ranges with offset input ranges', () => { | ||
const value = 0.75; // halfway in | ||
expect(remap(value, [0.5, 1.0], [0, 1])).toBe(0.5); // sanity, !inverse | ||
expect(remap(value, [0.5, 1.0], [1, 0])).toBe(0.5); | ||
const value2 = 0.5 + 0.125; // a quarter way in | ||
expect(remap(value2, [0.5, 1.0], [0, 1])).toBe(0.25); // sanity, !inverse | ||
expect(remap(value2, [0.5, 1.0], [1, 0])).toBe(0.75); | ||
const value3 = 1.5; // whole step overshoot | ||
expect(remap(value3, [0.5, 1.0], [0, 1])).toBe(2.0); // sanity, !inverse | ||
expect(remap(value3, [0.5, 1.0], [1, 0])).toBe(-1.0); | ||
const value4 = 1.0; // at the end | ||
expect(remap(value4, [0.5, 1.0], [1, 0])).toBe(0); | ||
const value5 = 0.5; // at the beginning | ||
expect(remap(value5, [0.5, 1.0], [1, 0])).toBe(1); | ||
}); | ||
test('inverse output ranges clamped results', () => { | ||
const value = 0.75; // halfway in | ||
expect(remap(value, [0.5, 1.0], [0, 1], true)).toBe(0.5); // sanity, !inverse | ||
expect(remap(value, [0.5, 1.0], [1, 0], true)).toBe(0.5); | ||
const value2 = 0.5 + 0.125; // a quarter way in | ||
expect(remap(value2, [0.5, 1.0], [0, 1], true)).toBe(0.25); // sanity, !inverse | ||
expect(remap(value2, [0.5, 1.0], [1, 0], true)).toBe(0.75); | ||
const value3 = 1.5; // whole step overshoot | ||
expect(remap(value3, [0.5, 1.0], [0, 1], true)).toBe(1.0); // sanity, !inverse | ||
expect(remap(value3, [0.5, 1.0], [1, 0], true)).toBe(0); | ||
}); | ||
test('throw error if elements are equal in either range', () => { | ||
@@ -120,0 +157,0 @@ expect(() => { |
@@ -6,3 +6,3 @@ const checkValidRanges = (arrays: number[][]): boolean => | ||
if (!checkValidRanges([inputRange, targetRange])) { | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements, and first element much have smaller value than second'); | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
} | ||
@@ -12,2 +12,3 @@ // let outgoing = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2; | ||
if (clamp === true) { | ||
if (targetRange[0] < targetRange[1]) { // normal output range | ||
if (outgoing > targetRange[1]) { | ||
@@ -18,2 +19,10 @@ outgoing = targetRange[1]; | ||
} | ||
} else { // inverse output range | ||
if (outgoing < targetRange[1]) { | ||
outgoing = targetRange[1]; | ||
} else if (outgoing > targetRange[0]) { | ||
outgoing = targetRange[0] | ||
} | ||
} | ||
} | ||
@@ -20,0 +29,0 @@ return shouldRound ? Math.round(outgoing) : outgoing; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34189
448