Skip to content

Commit

Permalink
feat: add xMinMaxDelta
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Feb 14, 2023
1 parent 231ca46 commit b142519
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from './x/xMedian';
export * from './x/xMedianAbsoluteDeviation';
export * from './x/xMinIndex';
export * from './x/xMinMaxValues';
export * from './x/xMinMaxDelta';
export * from './x/xMinValue';
export * from './x/xMode';
export * from './x/xMultiply';
Expand Down
13 changes: 13 additions & 0 deletions src/x/__tests__/xMinMaxDelta.test.ts
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 });
});
27 changes: 27 additions & 0 deletions src/x/xMinMaxDelta.ts
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 };
}

0 comments on commit b142519

Please sign in to comment.