Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-array-rescale

Package Overview
Dependencies
Maintainers
5
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-array-rescale - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

CHANGELOG.md

60

lib/index.js

@@ -9,42 +9,42 @@ 'use strict';

function rescale(input, options = {}) {
if (!Array.isArray(input)) {
throw new TypeError('input must be an array');
} else if (input.length === 0) {
throw new TypeError('input must not be empty');
}
if (!Array.isArray(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 (!Array.isArray(options.output)) {
throw new TypeError('output option must be an array if specified');
}
output = options.output;
} else {
output = new Array(input.length);
let output;
if (options.output !== undefined) {
if (!Array.isArray(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(input);
const currentMax = max(input);
const currentMin = min(input);
const currentMax = max(input);
if (currentMin === currentMax) {
throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array');
}
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;
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');
}
if (minValue >= maxValue) {
throw new RangeError('min option must be smaller than max option');
}
const factor = (maxValue - minValue) / (currentMax - currentMin);
for (var i = 0; i < input.length; i++) {
output[i] = (input[i] - currentMin) * factor + minValue;
}
const factor = (maxValue - minValue) / (currentMax - currentMin);
for (var i = 0; i < input.length; i++) {
output[i] = (input[i] - currentMin) * factor + minValue;
}
return output;
return output;
}
module.exports = rescale;
{
"name": "ml-array-rescale",
"version": "1.1.0",
"version": "1.1.1",
"description": "Rescale an array into a range",

@@ -23,5 +23,5 @@ "main": "lib/index.js",

"dependencies": {
"ml-array-max": "^1.0.1",
"ml-array-min": "^1.0.1"
"ml-array-max": "^1.0.2",
"ml-array-min": "^1.0.2"
}
}
import rescale from '../index';
describe('rescale', () => {
it('should return a rescaled array', () => {
expect(rescale([0, 2])).toEqual([0, 1]);
expect(rescale([0, 1])).toEqual([0, 1]);
expect(rescale([0, 1, 2])).toEqual([0, 0.5, 1]);
});
it('should return a rescaled array', () => {
expect(rescale([0, 2])).toEqual([0, 1]);
expect(rescale([0, 1])).toEqual([0, 1]);
expect(rescale([0, 1, 2])).toEqual([0, 0.5, 1]);
});
it('should throw min == max', () => {
expect(() => rescale([1, 1])).toThrow(/minimum and maximum input values are equal\. Cannot rescale a constant array/);
});
it('should throw min == max', () => {
expect(() => rescale([1, 1])).toThrow(/minimum and maximum input values are equal\. Cannot rescale a constant array/);
});
it('should fill the provided output', () => {
const array = [0, 1, 2, 3, 4];
const output = new Array(5);
rescale(array, {output});
expect(output).toEqual([0, 0.25, 0.5, 0.75, 1]);
expect(array).toEqual([0, 1, 2, 3, 4]);
});
it('should fill the provided output', () => {
const array = [0, 1, 2, 3, 4];
const output = new Array(5);
rescale(array, { output });
expect(output).toEqual([0, 0.25, 0.5, 0.75, 1]);
expect(array).toEqual([0, 1, 2, 3, 4]);
});
it('should work in-place', () => {
const array = [0, 1, 2, 3, 4];
rescale(array, {output: array});
expect(array).toEqual([0, 0.25, 0.5, 0.75, 1]);
});
it('should work in-place', () => {
const array = [0, 1, 2, 3, 4];
rescale(array, { output: array });
expect(array).toEqual([0, 0.25, 0.5, 0.75, 1]);
});
it('should work with custom min/max', () => {
expect(rescale([0, 1, 2], {min: -1, max: 1})).toEqual([-1, 0, 1]);
expect(rescale([0, 1, 2], {min: 0.5})).toEqual([0.5, 0.75, 1]);
expect(rescale([0, 1, 2], {max: 0.5})).toEqual([0, 0.25, 0.5]);
expect(rescale([0, 1, 2], {min: 50, max: 100})).toEqual([50, 75, 100]);
expect(rescale([-25, 0, 25, 50, 75], {min: -50, max: 0})).toEqual([-50, -37.5, -25, -12.5, 0]);
});
it('should work with custom min/max', () => {
expect(rescale([0, 1, 2], { min: -1, max: 1 })).toEqual([-1, 0, 1]);
expect(rescale([0, 1, 2], { min: 0.5 })).toEqual([0.5, 0.75, 1]);
expect(rescale([0, 1, 2], { max: 0.5 })).toEqual([0, 0.25, 0.5]);
expect(rescale([0, 1, 2], { min: 50, max: 100 })).toEqual([50, 75, 100]);
expect(rescale([-25, 0, 25, 50, 75], { min: -50, max: 0 })).toEqual([-50, -37.5, -25, -12.5, 0]);
});
it('should throw on bad inputs', () => {
expect(() => rescale()).toThrow(/input must be an array/);
expect(() => rescale([0, 1, 2], {output: false})).toThrow(/output option must be an array if specified/);
expect(() => rescale([0, 1, 2], {min: 2})).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], {max: -1})).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], {min: 2, max: 0})).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], {min: 1, max: 1})).toThrow(/min option must be smaller than max option/);
expect(() => rescale([], {min: 0, max: 1})).toThrow(/input must not be empty/);
});
it('should throw on bad inputs', () => {
expect(() => rescale()).toThrow(/input must be an array/);
expect(() => rescale([0, 1, 2], { output: false })).toThrow(/output option must be an array if specified/);
expect(() => rescale([0, 1, 2], { min: 2 })).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], { max: -1 })).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], { min: 2, max: 0 })).toThrow(/min option must be smaller than max option/);
expect(() => rescale([0, 1, 2], { min: 1, max: 1 })).toThrow(/min option must be smaller than max option/);
expect(() => rescale([], { min: 0, max: 1 })).toThrow(/input must not be empty/);
});
it('should work with current min/max', () => {
expect(rescale([0, 1, 2], {min: 1, autoMinMax: true})).toEqual([1, 1.5, 2]);
expect(rescale([0, 1, 2], {max: 3, autoMinMax: true})).toEqual([0, 1.5, 3]);
});
it('should work with current min/max', () => {
expect(rescale([0, 1, 2], { min: 1, autoMinMax: true })).toEqual([1, 1.5, 2]);
expect(rescale([0, 1, 2], { max: 3, autoMinMax: true })).toEqual([0, 1.5, 3]);
});
});

@@ -5,40 +5,40 @@ import max from 'ml-array-max';

export default function rescale(input, options = {}) {
if (!Array.isArray(input)) {
throw new TypeError('input must be an array');
} else if (input.length === 0) {
throw new TypeError('input must not be empty');
}
if (!Array.isArray(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 (!Array.isArray(options.output)) {
throw new TypeError('output option must be an array if specified');
}
output = options.output;
} else {
output = new Array(input.length);
let output;
if (options.output !== undefined) {
if (!Array.isArray(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(input);
const currentMax = max(input);
const currentMin = min(input);
const currentMax = max(input);
if (currentMin === currentMax) {
throw new RangeError('minimum and maximum input values are equal. Cannot rescale a constant array');
}
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;
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');
}
if (minValue >= maxValue) {
throw new RangeError('min option must be smaller than max option');
}
const factor = (maxValue - minValue) / (currentMax - currentMin);
for (var i = 0; i < input.length; i++) {
output[i] = (input[i] - currentMin) * factor + minValue;
}
const factor = (maxValue - minValue) / (currentMax - currentMin);
for (var i = 0; i < input.length; i++) {
output[i] = (input[i] - currentMin) * factor + minValue;
}
return output;
return output;
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc