@anselan/maprange
Advanced tools
Comparing version 0.4.0 to 0.5.1
declare const remap: (value: number, inputRange: number[], targetRange: number[], clamp?: boolean, shouldRound?: boolean) => number; | ||
export default remap; | ||
export = remap; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const checkValidRanges = (arrays) => arrays.reduce((result, a) => a.length !== 2 || a[0] >= a[1] ? false : result, true); | ||
const remap = (value, inputRange, targetRange, clamp = false, shouldRound = false) => { | ||
if (inputRange.length !== 2 || targetRange.length !== 2) { | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
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'); | ||
} | ||
@@ -19,3 +19,3 @@ // let outgoing = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2; | ||
}; | ||
exports.default = remap; | ||
module.exports = remap; | ||
//# sourceMappingURL=index.js.map |
@@ -69,2 +69,36 @@ "use strict"; | ||
}); | ||
describe('check range validity', () => { | ||
test('throw error if less than 2 elements in either range', () => { | ||
expect(() => { | ||
_1.default(0.5, [0], [0, 100]); | ||
}).toThrowError(); | ||
expect(() => { | ||
_1.default(0.5, [0, 1], [100]); | ||
}).toThrowError(); | ||
}); | ||
test('throw error if more than 2 elements in either range', () => { | ||
expect(() => { | ||
_1.default(0.1, [0, 1, 2], [0, 100]); | ||
}).toThrowError(); | ||
expect(() => { | ||
_1.default(0.1, [0, 1], [0, 100, 1000]); | ||
}).toThrowError(); | ||
}); | ||
test('throw error if elements out or order in either range', () => { | ||
expect(() => { | ||
_1.default(-1, [1, -1], [0, 100]); | ||
}).toThrowError(); | ||
expect(() => { | ||
_1.default(-1, [-1, 1], [10000, 100]); | ||
}).toThrowError(); | ||
}); | ||
test('throw error if elements are equal in either range', () => { | ||
expect(() => { | ||
_1.default(50, [100, 100], [0, 1]); | ||
}).toThrowError(); | ||
expect(() => { | ||
_1.default(50, [0, 100], [1, 1]); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@anselan/maprange", | ||
"version": "0.4.0", | ||
"version": "0.5.1", | ||
"description": "Map values from one range to another", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -30,7 +30,7 @@ # Map Range in JS | ||
``` | ||
import remap from 'remap` | ||
import remap from '@anselan/remap` | ||
``` | ||
...or: | ||
``` | ||
const remap = require('remap`) | ||
const remap = require('@anselan/remap`) | ||
``` | ||
@@ -37,0 +37,0 @@ |
@@ -73,1 +73,40 @@ import remap from '.' | ||
}) | ||
describe('check range validity', () => { | ||
test('throw error if less than 2 elements in either range', () => { | ||
expect(() => { | ||
remap(0.5, [0], [0,100]); | ||
}).toThrowError(); | ||
expect(() => { | ||
remap(0.5, [0,1], [100]); | ||
}).toThrowError(); | ||
}) | ||
test('throw error if more than 2 elements in either range', () => { | ||
expect(() => { | ||
remap(0.1, [0,1,2], [0,100]) | ||
}).toThrowError(); | ||
expect(() => { | ||
remap(0.1, [0,1], [0,100,1000]) | ||
}).toThrowError(); | ||
}) | ||
test('throw error if elements out or order in either range', () => { | ||
expect(() => { | ||
remap(-1, [1,-1], [0,100]) | ||
}).toThrowError(); | ||
expect(() => { | ||
remap(-1, [-1, 1], [10000,100]) | ||
}).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() | ||
}) | ||
}) |
@@ -0,4 +1,7 @@ | ||
const checkValidRanges = (arrays: number[][]): boolean => | ||
arrays.reduce((result, a) => a.length !== 2 || a[0] >= a[1] ? false : result, true) | ||
const remap = (value: number, inputRange: number[], targetRange: number[], clamp: boolean = false, shouldRound: boolean = false): number => { | ||
if (inputRange.length !== 2 || targetRange.length !== 2) { | ||
throw Error('inputRange and targetRange must be number arrays with exactly 2 elements'); | ||
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'); | ||
} | ||
@@ -17,2 +20,9 @@ // let outgoing = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2; | ||
export default remap; | ||
// const remapArray = (values: number[], inputRange: number[], targetRange: number[]): number[] => { | ||
// } | ||
// export { | ||
// remap, | ||
// } | ||
export = remap |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17724
14
266