diff --git a/src/index.ts b/src/index.ts index 28d974af..cf218631 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/x/__tests__/xMinMaxDelta.test.ts b/src/x/__tests__/xMinMaxDelta.test.ts new file mode 100644 index 00000000..6dc696f8 --- /dev/null +++ b/src/x/__tests__/xMinMaxDelta.test.ts @@ -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 }); +}); diff --git a/src/x/xMinMaxDelta.ts b/src/x/xMinMaxDelta.ts new file mode 100644 index 00000000..57d81559 --- /dev/null +++ b/src/x/xMinMaxDelta.ts @@ -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 }; +}