Skip to content

Commit

Permalink
fix!: improve types in xyObject functions
Browse files Browse the repository at this point in the history
  • Loading branch information
targos authored and lpatiny committed Feb 26, 2024
1 parent 018b825 commit 5f0f88b
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 101 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ exports[`test existence of exported functions 1`] = `
"xyArrayMerge",
"xyArrayWeightedMerge",
"xyObjectBestPoints",
"xyObjectCheck",
"xyObjectJoinX",
"xyObjectMaxXPoint",
"xyObjectMinXPoint",
"xyObjectMaxYPoint",
"xyObjectMinXPoint",
"xyObjectMinYPoint",
"xyObjectSlotX",
"xyObjectSortX",
Expand Down
77 changes: 38 additions & 39 deletions src/xyObject/__tests__/xyObjectBestPoints.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
import { xyObjectBestPoints } from '../../index';
import { xyObjectBestPoints } from '../xyObjectBestPoints';

describe('test xyObjectBestPeaks', () => {
const points = [
{ x: 1, y: 1 },
{ x: 2, y: 4 },
{ x: 3, y: 2 },
{ x: 4, y: 5 },
{ x: 5, y: 3 },
];
it('default options', () => {
const result = xyObjectBestPoints(points);
expect(result).toStrictEqual([
{ x: 1, y: 1, close: false },
{ x: 2, y: 4, close: false },
{ x: 3, y: 2, close: false },
{ x: 4, y: 5, close: false },
{ x: 5, y: 3, close: false },
]);
});
const points = [
{ x: 1, y: 1 },
{ x: 2, y: 4 },
{ x: 3, y: 2 },
{ x: 4, y: 5 },
{ x: 5, y: 3 },
];

it('custom options', () => {
const result = xyObjectBestPoints(points, {
numberSlots: 3,
numberCloseSlots: 6,
});
expect(result).toStrictEqual([
{ close: true, x: 1, y: 1 },
{ close: false, x: 2, y: 4 },
{ close: true, x: 3, y: 2 },
{ close: false, x: 4, y: 5 },
{ close: true, x: 5, y: 3 },
]);
});
test('default options', () => {
const result = xyObjectBestPoints(points);
expect(result).toStrictEqual([
{ x: 1, y: 1, close: false },
{ x: 2, y: 4, close: false },
{ x: 3, y: 2, close: false },
{ x: 4, y: 5, close: false },
{ x: 5, y: 3, close: false },
]);
});

it('custom options threshold', () => {
const result = xyObjectBestPoints(points, { threshold: 0.5 });
expect(result).toStrictEqual([
{ close: false, x: 2, y: 4 },
{ close: false, x: 4, y: 5 },
{ close: false, x: 5, y: 3 },
]);
test('custom options', () => {
const result = xyObjectBestPoints(points, {
numberSlots: 3,
numberCloseSlots: 6,
});
expect(result).toStrictEqual([
{ close: true, x: 1, y: 1 },
{ close: false, x: 2, y: 4 },
{ close: true, x: 3, y: 2 },
{ close: false, x: 4, y: 5 },
{ close: true, x: 5, y: 3 },
]);
});

test('custom options threshold', () => {
const result = xyObjectBestPoints(points, { threshold: 0.5 });
expect(result).toStrictEqual([
{ close: false, x: 2, y: 4 },
{ close: false, x: 4, y: 5 },
{ close: false, x: 5, y: 3 },
]);
});
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectJoinX.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectJoinX } from '../../index';
import { xyObjectJoinX } from '../xyObjectJoinX';

test('xyObjectJoinX', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectMaxXPoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectMaxYPoint } from '../../index';
import { xyObjectMaxYPoint } from '../xyObjectMaxYPoint';

test('xyObjectMaxYPoint', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectMaxYPoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectMaxXPoint } from '../../index';
import { xyObjectMaxXPoint } from '../xyObjectMaxXPoint';

test('xyObjectMaxXPoint', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectMinXPoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectMinXPoint } from '../../index';
import { xyObjectMinXPoint } from '../xyObjectMinXPoint';

test('xyObjectMinXPoint', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectMinYPoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectMinYPoint } from '../../index';
import { xyObjectMinYPoint } from '../xyObjectMinYPoint';

test('xyObjectMinYPoint', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectSlotX.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectSlotX } from '../../index';
import { xyObjectSlotX } from '../xyObjectSlotX';

test('xyObjectSlotX', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectSortX.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectSortX } from '../../index';
import { xyObjectSortX } from '../xyObjectSortX';

test('xyObjectSortX', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectSumY.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectSumY } from '../../index';
import { xyObjectSumY } from '../xyObjectSumY';

test('xyObjectSumY', () => {
const arrayXY = [
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/__tests__/xyObjectToXY.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xyObjectToXY } from '../../index';
import { xyObjectToXY } from '../xyObjectToXY';

test('xyObjectToXY', () => {
const arrayXY = [
Expand Down
3 changes: 2 additions & 1 deletion src/xyObject/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from './xyObjectBestPoints';
export * from './xyObjectCheck';
export * from './xyObjectJoinX';
export * from './xyObjectMaxXPoint';
export * from './xyObjectMinXPoint';
export * from './xyObjectMaxYPoint';
export * from './xyObjectMinXPoint';
export * from './xyObjectMinYPoint';
export * from './xyObjectSlotX';
export * from './xyObjectSortX';
Expand Down
59 changes: 31 additions & 28 deletions src/xyObject/xyObjectBestPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@ import { Point } from '../types';
import { xyObjectMaxXPoint } from './xyObjectMaxXPoint';
import { xyObjectMinXPoint } from './xyObjectMinXPoint';

export interface XYObjectBestPointsOptions {
/**
* min X value of the window to consider
*/
from?: number;
/**
* max X value of the window to consider
*/
to?: number;
/**
* max number of points
* @default 20
* */
limit?: number;
/**
* minimal intensity compare to more intense
* @default 0.01
* */
threshold?: number;
/**
* number of slots
* @default 50 */
numberCloseSlots?: number;
/**
* define the number of slots and indirectly the slot width
* @default 10
* */
numberSlots?: number;
}

/**
* Filter the array by taking the higher points (max y value) and only.
* Keep one per slot. There are 2 different slots, the smallest one will have the
Expand All @@ -14,34 +44,7 @@ import { xyObjectMinXPoint } from './xyObjectMinXPoint';
*/
export function xyObjectBestPoints(
points: Point[],
options: {
/**
* min X value of the window to consider
*/
from?: number;
/**
* max X value of the window to consider
*/
to?: number;
/**
* max number of points
* @default 20
* */
limit?: number;
/**
* minimal intensity compare to more intense
* @default 0.01
* */
threshold?: number;
/** number of slots
* @default 50 */
numberCloseSlots?: number;
/**
* define the number of slots and indirectly the slot width
* @default 10
* */
numberSlots?: number;
} = {},
options: XYObjectBestPointsOptions = {},
): Point[] {
const {
from = xyObjectMinXPoint(points).x,
Expand Down
12 changes: 6 additions & 6 deletions src/xyObject/xyObjectCheck.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Point } from '../types';

/**
* Throw an error in no an object of x,y arrays
* Throws an error in not an array of x,y objects.
*
* @param points - list of points
* @param points - List of points.
*/
export function xyObjectCheck(points: Point[] = []) {
export function xyObjectCheck(points?: Point[]): asserts points is Point[] {
if (!Array.isArray(points)) {
throw new Error('ArrayPoints must be an array of {x,y} object');
throw new Error('points must be an array of {x,y} objects');
}
if (
points.length > 0 &&
(typeof points[0].x === 'undefined' || typeof points[0].y === 'undefined')
(typeof points[0].x !== 'number' || typeof points[0].y !== 'number')
) {
throw new Error('ArrayPoints must be an array of {x,y} object');
throw new Error('points must be an array of {x,y} objects');
}
}
16 changes: 9 additions & 7 deletions src/xyObject/xyObjectJoinX.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Point } from '../types';

export interface XYObjectJoinXOptions {
/**
* Limit to join the data.
* @default Number.EPSILON
* */
xError?: number;
}

/**
* xyObjectJoinX.
*
Expand All @@ -8,13 +16,7 @@ import { Point } from '../types';
*/
export function xyObjectJoinX(
points: Point[],
options: {
/**
* Limit to join the data.
* @default Number.EPSILON
* */
xError?: number;
} = {},
options: XYObjectJoinXOptions = {},
): Point[] {
const { xError = Number.EPSILON } = options;

Expand Down
16 changes: 9 additions & 7 deletions src/xyObject/xyObjectSlotX.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Point } from '../types';

export interface XYObjectSlotXOptions {
/**
* Limit to join the dataPoints[].
* @default 1
* */
slotWidth?: number;
}

/**
* xyObjectSlotX
*
Expand All @@ -8,13 +16,7 @@ import { Point } from '../types';
*/
export function xyObjectSlotX(
points: Point[],
options: {
/**
* Limit to join the dataPoints[].
* @default 1
* */
slotWidth?: number;
} = {},
options: XYObjectSlotXOptions = {},
): Point[] {
const { slotWidth = 1 } = options;
const halfSlot = slotWidth / 2;
Expand Down
3 changes: 2 additions & 1 deletion src/xyObject/xyObjectSortX.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Point } from '../types';

/**
* Sorts an array of points
* Sorts an array of points in-place.
*
* @param points - array of points {x,y}
* @returns - sorted array of points {x,y}
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/xyObjectSumY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Point } from '../types';
import { xyObjectCheck } from './xyObjectCheck';

/**
* Calculate the sum of Y values
* Calculate the sum of Y values.
*
* @param points - Object that contains property x and y (an array)
*/
Expand Down
2 changes: 1 addition & 1 deletion src/xyObject/xyObjectToXY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Point } from '../types';
*
* @param points - Array of points {x,y}.
*/
export function xyObjectToXY(points: Point[]): DataXY {
export function xyObjectToXY(points: Point[]): DataXY<number[]> {
return {
x: points.map((entry) => entry.x),
y: points.map((entry) => entry.y),
Expand Down

0 comments on commit 5f0f88b

Please sign in to comment.