Skip to content

Commit cc50b98

Browse files
committed
feat: add xyObjectNormedY
1 parent 295804c commit cc50b98

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ exports[`test existence of exported functions 1`] = `
134134
"xyObjectMinMaxValues",
135135
"xyObjectMinXPoint",
136136
"xyObjectMinYPoint",
137+
"xyObjectNormedY",
137138
"xyObjectSlotX",
138139
"xyObjectSortX",
139140
"xyObjectSumY",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { xyObjectNormedY } from '../xyObjectNormedY';
4+
5+
test('xyObjectNormedY', () => {
6+
const arrayXY = [
7+
{ x: 1, y: 1 },
8+
{ x: 2, y: 2 },
9+
{ x: 3, y: 3 },
10+
{ x: 4, y: 4 },
11+
];
12+
const result = xyObjectNormedY(arrayXY, { algorithm: 'max', value: 100 });
13+
expect(result).toStrictEqual([
14+
{ x: 1, y: 25 },
15+
{ x: 2, y: 50 },
16+
{ x: 3, y: 75 },
17+
{ x: 4, y: 100 },
18+
]);
19+
});

src/xyObject/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './xyObjectMaxYPoint';
66
export * from './xyObjectMinMaxValues';
77
export * from './xyObjectMinXPoint';
88
export * from './xyObjectMinYPoint';
9+
export * from './xyObjectNormedY';
910
export * from './xyObjectSlotX';
1011
export * from './xyObjectSortX';
1112
export * from './xyObjectSumY';

src/xyObject/xyObjectNormedY.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Point } from '../types';
2+
import { xNormed } from '../x/xNormed';
3+
import type { XNormedOptions } from '../x/xNormed';
4+
5+
/**
6+
* Resize the Y values of the points to be normalized.
7+
* @param points - array of points {x,y}
8+
* @param options
9+
* @returns - array of points {x,y} with normalized Y values
10+
*/
11+
export function xyObjectNormedY(
12+
points: Point[],
13+
options?: XNormedOptions,
14+
): Point[] {
15+
points = structuredClone(points);
16+
17+
const ys = points.map((point) => point.y);
18+
const normalizedYs = xNormed(ys, options);
19+
for (let i = 0; i < points.length; i++) {
20+
points[i].y = normalizedYs[i];
21+
}
22+
return points;
23+
}

0 commit comments

Comments
 (0)