-
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.
feat: rename xyClosestX to xyFindClosestPoint and only allow sorted a…
…rray
- Loading branch information
Showing
5 changed files
with
83 additions
and
133 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 was deleted.
Oops, something went wrong.
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,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, | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,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], | ||
}; | ||
} |