-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { xMinMaxDelta } from '../xMinMaxDelta'; | ||
|
||
test('xMinMaxDelta', () => { | ||
let typedArray = new Uint16Array(6); | ||
typedArray[0] = 1; | ||
typedArray[1] = 2; | ||
typedArray[2] = 3; | ||
typedArray[3] = 4; | ||
typedArray[4] = 6; | ||
typedArray[5] = 7; | ||
|
||
expect(xMinMaxDelta(typedArray)).toStrictEqual({ min: 1, max: 2 }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NumberArray } from 'cheminfo-types'; | ||
|
||
import { xCheck } from './xCheck'; | ||
|
||
/** | ||
* Return min and max values of an array | ||
* | ||
* @param array - array of number | ||
* @returns - Object with 2 properties, min and max | ||
*/ | ||
export function xMinMaxDelta(array: NumberArray): { | ||
min: number; | ||
max: number; | ||
} { | ||
xCheck(array, { minLength: 2 }); | ||
|
||
let minDelta = array[1] - array[0]; | ||
let maxDelta = minDelta; | ||
|
||
for (let i = 0; i < array.length - 1; i++) { | ||
let delta = array[i + 1] - array[i]; | ||
if (delta < minDelta) minDelta = delta; | ||
if (delta > maxDelta) maxDelta = delta; | ||
} | ||
|
||
return { min: minDelta, max: maxDelta }; | ||
} |