ml-array-rescale
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -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; | ||
} |
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
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
7827
7
1
Updatedml-array-max@^1.0.2
Updatedml-array-min@^1.0.2