Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/xy/xyEnsureGrowingX.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DataXY } from 'cheminfo-types';
import type { DataXY, NumberArray } from 'cheminfo-types';

import { xIsMonotonic } from '../x/index.ts';

Expand All @@ -8,7 +8,9 @@ import { xyCheck } from './xyCheck.ts';
* Filters x,y values to allow strictly growing values in x-axis.
* @param data - Object that contains property x (an ordered increasing array) and y (an array).
*/
export function xyEnsureGrowingX(data: DataXY): DataXY {
export function xyEnsureGrowingX<DataType extends NumberArray = NumberArray>(
data: DataXY<DataType>,
): DataXY<DataType | number[]> {
xyCheck(data);
if (xIsMonotonic(data.x) === 1) return data;
const x = Array.from(data.x);
Expand Down
10 changes: 6 additions & 4 deletions src/xy/xyGrowingX.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { DataXY } from 'cheminfo-types';
import type { DataXY, NumberArray } from 'cheminfo-types';

/**
* Order object of array, x has to be monotone. Ensure x is growing
* @param data - Object of kind {x:[], y:[]}.
*/
export function xyGrowingX(data: DataXY): DataXY {
export function xyGrowingX<DataType extends NumberArray = NumberArray>(
data: DataXY<DataType>,
): DataXY<DataType> {
const { x, y } = data;

if (x.length !== y.length) {
Expand All @@ -14,7 +16,7 @@ export function xyGrowingX(data: DataXY): DataXY {
if (x.length < 2 || x[0] < (x.at(-1) as number)) return data;

return {
x: x.toReversed(),
y: y.toReversed(),
x: x.toReversed() as DataType,
y: y.toReversed() as DataType,
};
}
Loading