ml-array-rescale
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -11,3 +11,6 @@ 'use strict'; | ||
throw new TypeError('input must be an array'); | ||
} else if (input.length === 0) { | ||
throw new TypeError('input must not be empty'); | ||
} | ||
let output; | ||
@@ -23,5 +26,12 @@ if (options.output !== undefined) { | ||
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'); | ||
} | ||
const { | ||
min: minValue = 0, | ||
max: maxValue = 1 | ||
min: minValue = options.autoMinMax ? currentMin : 0, | ||
max: maxValue = options.autoMinMax ? currentMax : 1 | ||
} = options; | ||
@@ -33,9 +43,2 @@ | ||
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'); | ||
} | ||
const factor = (maxValue - minValue) / (currentMax - currentMin); | ||
@@ -42,0 +45,0 @@ for (var i = 0; i < input.length; i++) { |
{ | ||
"name": "ml-array-rescale", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Rescale an array into a range", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -38,8 +38,14 @@ import rescale from '../index'; | ||
expect(() => rescale()).toThrow(/input must be an array/); | ||
expect(() => rescale([], {output: false})).toThrow(/output option must be an array if specified/); | ||
expect(() => rescale([], {min: 2})).toThrow(/min option must be smaller than max option/); | ||
expect(() => rescale([], {max: -1})).toThrow(/min option must be smaller than max option/); | ||
expect(() => rescale([], {min: 2, max: 0})).toThrow(/min option must be smaller than max option/); | ||
expect(() => rescale([], {min: 1, max: 1})).toThrow(/min option must be smaller than max option/); | ||
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]); | ||
}); | ||
}); |
@@ -7,3 +7,6 @@ import max from 'ml-array-max'; | ||
throw new TypeError('input must be an array'); | ||
} else if (input.length === 0) { | ||
throw new TypeError('input must not be empty'); | ||
} | ||
let output; | ||
@@ -19,5 +22,12 @@ if (options.output !== undefined) { | ||
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'); | ||
} | ||
const { | ||
min: minValue = 0, | ||
max: maxValue = 1 | ||
min: minValue = options.autoMinMax ? currentMin : 0, | ||
max: maxValue = options.autoMinMax ? currentMax : 1 | ||
} = options; | ||
@@ -29,9 +39,2 @@ | ||
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'); | ||
} | ||
const factor = (maxValue - minValue) / (currentMax - currentMin); | ||
@@ -38,0 +41,0 @@ for (var i = 0; i < input.length; i++) { |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7745
116
0