@anselan/maprange
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -1,2 +0,2 @@ | ||
declare const remap: (value: number, inputRange: number[], targetRange: number[], clamp?: boolean) => number; | ||
declare const remap: (value: number, inputRange: number[], targetRange: number[], clamp?: boolean, shouldRound?: boolean) => number; | ||
export default remap; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const remap = (value, inputRange, targetRange, clamp = false) => { | ||
const remap = (value, inputRange, targetRange, clamp = false, shouldRound = false) => { | ||
if (inputRange.length !== 2 || targetRange.length !== 2) { | ||
@@ -11,16 +11,11 @@ throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
if (outgoing > targetRange[1]) { | ||
return targetRange[1]; | ||
outgoing = targetRange[1]; | ||
} | ||
else if (outgoing < targetRange[0]) { | ||
return targetRange[0]; | ||
outgoing = targetRange[0]; | ||
} | ||
else { | ||
return outgoing; | ||
} | ||
} | ||
else { | ||
return outgoing; | ||
} | ||
return shouldRound ? Math.round(outgoing) : outgoing; | ||
}; | ||
exports.default = remap; | ||
//# sourceMappingURL=index.js.map |
@@ -32,3 +32,3 @@ "use strict"; | ||
}); | ||
test('two values, one complex', () => { | ||
test('two values, one with fractions in result', () => { | ||
const x = 0.5; | ||
@@ -41,2 +41,6 @@ const y = 0.8967; | ||
}); | ||
test('whole numbers only', () => { | ||
const y = 0.8967; | ||
expect(_1.default(y, [0, 1], [0, 1080], false, true)).toBe(968); | ||
}); | ||
}); | ||
@@ -43,0 +47,0 @@ describe('negative values', () => { |
{ | ||
"name": "@anselan/maprange", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Map values from one range to another", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -11,3 +11,3 @@ # Map Range in JS | ||
### Essentially: | ||
> **remap (value, inputRange, targetRange, clamp)** | ||
> **remap (value, inputRange, targetRange, clamp, shouldRound)** | ||
@@ -19,2 +19,3 @@ ...where the parameters are: | ||
* `clamp` (optional; default `false`): whether to constrain the result within the given target range | ||
* `shouldRound` (optional; default `false`): whether to round to nearest integer, i.e. only whole numbers | ||
@@ -45,2 +46,3 @@ ### Description | ||
### Float position to pixels | ||
Coordinates can often be normalised in the range [0,1]. Let's try convert to pixel values instead: | ||
``` | ||
@@ -56,2 +58,8 @@ // These could be coordinates in [0,1] range: | ||
``` | ||
Often you want whole numbers for things like pixels. Optional rounding is included for convenience: | ||
``` | ||
const py = remap(0.8967, [0,1], [0,1080], false, true); | ||
console.log(py); // "968" (rounded from 968.436) | ||
``` | ||
@@ -32,3 +32,3 @@ import remap from '.' | ||
}) | ||
test('two values, one complex', () => { | ||
test('two values, one with fractions in result', () => { | ||
const x = 0.5; | ||
@@ -43,2 +43,6 @@ const y = 0.8967; | ||
}) | ||
test('whole numbers only', () => { | ||
const y = 0.8967; | ||
expect(remap(y, [0,1], [0,1080], false, true)).toBe(968); | ||
}) | ||
}) | ||
@@ -45,0 +49,0 @@ |
@@ -1,2 +0,2 @@ | ||
const remap = (value: number, inputRange: number[], targetRange: number[], clamp: boolean = false): number => { | ||
const remap = (value: number, inputRange: number[], targetRange: number[], clamp: boolean = false, shouldRound: boolean = false): number => { | ||
if (inputRange.length !== 2 || targetRange.length !== 2) { | ||
@@ -9,13 +9,10 @@ throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
if (outgoing > targetRange[1]) { | ||
return targetRange[1]; | ||
outgoing = targetRange[1]; | ||
} else if (outgoing < targetRange[0]) { | ||
return targetRange[0] | ||
} else { | ||
return outgoing | ||
} | ||
} else { | ||
return outgoing; | ||
} | ||
outgoing = targetRange[0] | ||
} | ||
} | ||
return shouldRound ? Math.round(outgoing) : outgoing; | ||
} | ||
export default remap; |
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
13273
62