You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
56 lines
1.5 KiB
|
4 months ago
|
'use strict';
|
||
|
|
|
||
|
|
var isAnyArray = require('is-any-array');
|
||
|
|
var max = require('ml-array-max');
|
||
|
|
var min = require('ml-array-min');
|
||
|
|
|
||
|
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
||
|
|
|
||
|
|
var max__default = /*#__PURE__*/_interopDefaultLegacy(max);
|
||
|
|
var min__default = /*#__PURE__*/_interopDefaultLegacy(min);
|
||
|
|
|
||
|
|
function rescale(input, options = {}) {
|
||
|
|
if (!isAnyArray.isAnyArray(input)) {
|
||
|
|
throw new TypeError('input must be an array');
|
||
|
|
} else if (input.length === 0) {
|
||
|
|
throw new TypeError('input must not be empty');
|
||
|
|
}
|
||
|
|
|
||
|
|
let output;
|
||
|
|
if (options.output !== undefined) {
|
||
|
|
if (!isAnyArray.isAnyArray(options.output)) {
|
||
|
|
throw new TypeError('output option must be an array if specified');
|
||
|
|
}
|
||
|
|
output = options.output;
|
||
|
|
} else {
|
||
|
|
output = new Array(input.length);
|
||
|
|
}
|
||
|
|
|
||
|
|
const currentMin = min__default['default'](input);
|
||
|
|
const currentMax = max__default['default'](input);
|
||
|
|
|
||
|
|
if (currentMin === currentMax) {
|
||
|
|
throw new RangeError(
|
||
|
|
'minimum and maximum input values are equal. Cannot rescale a constant array',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const {
|
||
|
|
min: minValue = options.autoMinMax ? currentMin : 0,
|
||
|
|
max: maxValue = options.autoMinMax ? currentMax : 1,
|
||
|
|
} = options;
|
||
|
|
|
||
|
|
if (minValue >= maxValue) {
|
||
|
|
throw new RangeError('min option must be smaller than max option');
|
||
|
|
}
|
||
|
|
|
||
|
|
const factor = (maxValue - minValue) / (currentMax - currentMin);
|
||
|
|
for (let i = 0; i < input.length; i++) {
|
||
|
|
output[i] = (input[i] - currentMin) * factor + minValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
return output;
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = rescale;
|