Skip to content

Commit

Permalink
feat: rename xyClosestX to xyFindClosestPoint and only allow sorted a…
Browse files Browse the repository at this point in the history
…rray
  • Loading branch information
lpatiny committed Mar 7, 2022
1 parent a2b1c43 commit 2b439f8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export * from './x/utils/getOutputArray';

export * from './xy/xyAlign';
export * from './xy/xyCheck';
export * from './xy/xyClosestX';
export * from './xy/xyFindClosestPoint';
export * from './xy/xyCovariance';
export * from './xy/xyCumulativeDistributionStatistics';
export * from './xy/xyEnsureGrowingX';
Expand Down
71 changes: 0 additions & 71 deletions src/xy/__tests__/xyClosestX.test.ts

This file was deleted.

57 changes: 57 additions & 0 deletions src/xy/__tests__/xyFindClosestPoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { xyFindClosestPoint } from '../xyFindClosestPoint';

describe('closestX', () => {
it('should yield the correct result with even element array', () => {
const x = [-1, 0, 1, 2, 3, 4, 5, 6];
const y = [10, 11, 12, 13, 14, 15, 16, 17];
expect(xyFindClosestPoint({ x, y }, -2)).toStrictEqual({
x: -1,
y: 10,
});
expect(xyFindClosestPoint({ x, y }, 0.6)).toStrictEqual({
x: 1,
y: 12,
});
expect(xyFindClosestPoint({ x, y }, 4.3)).toStrictEqual({
x: 4,
y: 15,
});
expect(xyFindClosestPoint({ x, y }, 6)).toStrictEqual({
x: 6,
y: 17,
});
expect(xyFindClosestPoint({ x, y }, 7)).toStrictEqual({
x: 6,
y: 17,
});
});

it('should yield the correct result with odd element array', () => {
const x = [-1, 0, 1, 2, 3, 4, 5, 6, 7];
const y = [10, 11, 12, 13, 14, 15, 16, 17, 18];
expect(xyFindClosestPoint({ x, y }, -2)).toStrictEqual({
x: -1,
y: 10,
});
expect(xyFindClosestPoint({ x, y }, 0.6)).toStrictEqual({
x: 1,
y: 12,
});
expect(xyFindClosestPoint({ x, y }, 4.3)).toStrictEqual({
x: 4,
y: 15,
});
expect(xyFindClosestPoint({ x, y }, 6)).toStrictEqual({
x: 6,
y: 17,
});
expect(xyFindClosestPoint({ x, y }, 7)).toStrictEqual({
x: 7,
y: 18,
});
expect(xyFindClosestPoint({ x, y }, 8)).toStrictEqual({
x: 7,
y: 18,
});
});
});
61 changes: 0 additions & 61 deletions src/xy/xyClosestX.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/xy/xyFindClosestPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import binarySearch from 'binary-search';
import { DataXY, PointXY } from 'cheminfo-types';

import { xFindClosestIndex } from '../x/xFindClosestIndex';

/**
* Finds the closest point
*
* @param data - x array should be sorted and ascending
* @param target - target to search
* @returns - closest point
*/
export function xyFindClosestPoint(
/** points */
data: DataXY,
target: number,
): PointXY {
const { x, y } = data;

const index = xFindClosestIndex(x, target);
return {
x: x[index],
y: y[index],
};
}

0 comments on commit 2b439f8

Please sign in to comment.