Skip to content

Commit

Permalink
feat: add xy2ToXY
Browse files Browse the repository at this point in the history
* feat: xy array of arrays to XY Object

* feat: create xy2ToXY

* docs: add README for xy2

* docs: add README for xy2

---------

Co-authored-by: Luc Patiny <luc@patiny.com>
  • Loading branch information
RicardoSilvestr and lpatiny authored Feb 7, 2023
1 parent 68eb144 commit 6bf49a5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ They are sorted in various categories:
- matrix: functions on ml-matrix instances (or array or array)
- x: functions that apply on a 1D array
- xy: functions that apply on an object `{x: [], y:[]}`
- xy2: functions that apply on an array of array of 2 numbers `[[x,y], [x,y]]`
- xyArray: functions that apply on an array of objects `{x: [], y:[]}`
- xyObject: functions that apply on an array of point `[{x,y}]`
- xreim: functions that apply on an object `{x: [], re:[], im:[]}`
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export * from './xy/xyToXYObject';
export * from './xy/xyUniqueX';
export * from './xy/xyWeightedMerge';

export * from './xy2/xy2ToXY';

export * from './xreim/xreimZeroFilling';
export * from './xreim/xreimSortX';

Expand Down
18 changes: 18 additions & 0 deletions src/xy2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# xy2

This folder deals with data structure like:

`[number,number][]`

It allows for example to convert an array of array containing 2 values to an object {x: [], y:[]}

```js
const result = xy2ToXY([
[1, 2],
[2, 4],
[5, 4],
[8, 5],
]);
console.log(result);
// { x: [1, 2, 5, 8], y: [2, 4, 4, 5] }
```
12 changes: 12 additions & 0 deletions src/xy2/__tests__/xy2ToXY.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { xy2ToXY } from '../xy2ToXY';

test('xy2ToXY', () => {
expect(
xy2ToXY([
[1, 2],
[2, 4],
[5, 4],
[8, 5],
]),
).toStrictEqual({ x: [1, 2, 5, 8], y: [2, 4, 4, 5] });
});
16 changes: 16 additions & 0 deletions src/xy2/xy2ToXY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DataXY } from 'cheminfo-types';

/**
* Convert an array of XY arrays to a DataXY object containing x,y arrays
*
* @param data - array of arrays [[x,y],[x,y],...]
*/
export function xy2ToXY(data: [number, number][]): DataXY<number[]> {
const xy2: DataXY<number[]> = { x: [], y: [] };

for (let xyValue of data) {
xy2.x.push(xyValue[0]);
xy2.y.push(xyValue[1]);
}
return xy2;
}

0 comments on commit 6bf49a5

Please sign in to comment.