@anselan/maprange
Advanced tools
Comparing version 0.9.2 to 0.10.0
@@ -0,4 +1,9 @@ | ||
/** Remap an input `value` which is currently in the range `inputRange` to a new range `outputRange` */ | ||
declare const remap: (value: number, inputRange: number[], targetRange: number[], clamp?: boolean, shouldRound?: boolean) => number; | ||
/** Remap all values in the array `values` from `inputRange` to `outputRange`; returns the remapped array */ | ||
declare const remapArray: (values: number[], inputRange: number[], targetRange: number[], clamp?: boolean, shouldRound?: boolean) => number[]; | ||
/** Works the same as remapArray, but conveniently allows you to simply specify the input and output | ||
* dimensions (width and height), and assumes that the min for each dimension in each range is zero. | ||
*/ | ||
declare const remapCoords: (inputCoords: number[], inputDimensions: number[], targetDimensions: number[], clamp?: boolean, shouldRound?: boolean) => number[]; | ||
export { remap, remapArray, remapCoords }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.remapCoords = exports.remapArray = exports.remap = void 0; | ||
const checkValidRanges = (arrays) => arrays.reduce((result, a) => a.length !== 2 || a[0] === a[1] ? false : result, true); | ||
const checkValidRanges = (arrays) => arrays.reduce((result, a) => a.length !== 2 ? false : result, true); | ||
/** Remap an input `value` which is currently in the range `inputRange` to a new range `outputRange` */ | ||
const remap = (value, inputRange, targetRange, clamp = false, shouldRound = false) => { | ||
@@ -33,2 +34,3 @@ if (!checkValidRanges([inputRange, targetRange])) { | ||
exports.remap = remap; | ||
/** Remap all values in the array `values` from `inputRange` to `outputRange`; returns the remapped array */ | ||
const remapArray = (values, inputRange, targetRange, clamp = false, shouldRound = false) => values.reduce((result, v) => [ | ||
@@ -39,2 +41,5 @@ ...result, | ||
exports.remapArray = remapArray; | ||
/** Works the same as remapArray, but conveniently allows you to simply specify the input and output | ||
* dimensions (width and height), and assumes that the min for each dimension in each range is zero. | ||
*/ | ||
const remapCoords = (inputCoords, inputDimensions, targetDimensions, clamp = false, shouldRound = false) => { | ||
@@ -41,0 +46,0 @@ if (inputCoords.length !== targetDimensions.length || inputCoords.length !== inputDimensions.length) { |
@@ -127,9 +127,18 @@ "use strict"; | ||
}); | ||
test('throw error if elements are equal in either range', () => { | ||
expect(() => { | ||
_1.remap(50, [100, 100], [0, 1]); | ||
}).toThrowError(); | ||
expect(() => { | ||
_1.remap(50, [0, 100], [1, 1]); | ||
}).toThrowError(); | ||
// test('throw error if elements are equal in either range', () => { | ||
// expect(() => { | ||
// remap(50, [100, 100], [0, 1]) | ||
// }).toThrowError() | ||
// expect(() => { | ||
// remap(50, [0, 100], [1, 1]) | ||
// }).toThrowError() | ||
// }) | ||
test("if elements are equal in range, simply return result unchanged", () => { | ||
const inputRange = [0, 1]; | ||
const targetRange = [510.4, 510.4]; | ||
expect(_1.remap(0, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(_1.remap(0.5, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(_1.remap(0.5, inputRange, targetRange)).toBe(targetRange[1]); | ||
expect(_1.remap(1, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(_1.remap(1, inputRange, targetRange)).toBe(targetRange[1]); | ||
}); | ||
@@ -136,0 +145,0 @@ }); |
{ | ||
"name": "@anselan/maprange", | ||
"version": "0.9.2", | ||
"version": "0.10.0", | ||
"description": "Map values from one range to another", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -155,9 +155,19 @@ import { remap, remapArray, remapCoords } from '.' | ||
test('throw error if elements are equal in either range', () => { | ||
expect(() => { | ||
remap(50, [100, 100], [0, 1]) | ||
}).toThrowError() | ||
expect(() => { | ||
remap(50, [0, 100], [1, 1]) | ||
}).toThrowError() | ||
// test('throw error if elements are equal in either range', () => { | ||
// expect(() => { | ||
// remap(50, [100, 100], [0, 1]) | ||
// }).toThrowError() | ||
// expect(() => { | ||
// remap(50, [0, 100], [1, 1]) | ||
// }).toThrowError() | ||
// }) | ||
test("if elements are equal in range, simply return result unchanged", () => { | ||
const inputRange = [0,1]; | ||
const targetRange = [510.4, 510.4]; | ||
expect(remap(0, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(remap(0.5, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(remap(0.5, inputRange, targetRange)).toBe(targetRange[1]); | ||
expect(remap(1, inputRange, targetRange)).toBe(targetRange[0]); | ||
expect(remap(1, inputRange, targetRange)).toBe(targetRange[1]); | ||
}) | ||
@@ -164,0 +174,0 @@ }) |
const checkValidRanges = (arrays: number[][]): boolean => | ||
arrays.reduce((result, a) => a.length !== 2 || a[0] === a[1] ? false : result, true) | ||
arrays.reduce((result, a) => a.length !== 2 ? false : result, true) | ||
/** Remap an input `value` which is currently in the range `inputRange` to a new range `outputRange` */ | ||
const remap = (value: number, inputRange: number[], targetRange: number[], clamp: boolean = false, shouldRound: boolean = false): number => { | ||
@@ -30,2 +31,3 @@ if (!checkValidRanges([inputRange, targetRange])) { | ||
/** Remap all values in the array `values` from `inputRange` to `outputRange`; returns the remapped array */ | ||
const remapArray = (values: number[], inputRange: number[], targetRange: number[], clamp: boolean = false, shouldRound: boolean = false): number[] => | ||
@@ -37,2 +39,5 @@ values.reduce((result, v) => [ | ||
/** Works the same as remapArray, but conveniently allows you to simply specify the input and output | ||
* dimensions (width and height), and assumes that the min for each dimension in each range is zero. | ||
*/ | ||
const remapCoords = (inputCoords: number[], inputDimensions: number[], targetDimensions: number[], clamp=false, shouldRound = false): number[] => { | ||
@@ -39,0 +44,0 @@ if (inputCoords.length !== targetDimensions.length || inputCoords.length !== inputDimensions.length) { |
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
38570
503