Skip to content

Commit

Permalink
fix!: improve types in xreim 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 24a6285 commit b6257b5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ exports[`test existence of exported functions 1`] = `
"xyUniqueX",
"xyWeightedMerge",
"xy2ToXY",
"xreimZeroFilling",
"xreimSortX",
"xreimZeroFilling",
"xyArrayAlign",
"xyArrayMerge",
"xyArrayWeightedMerge",
Expand Down
8 changes: 4 additions & 4 deletions src/types/DataXReIm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DoubleArray } from 'cheminfo-types';

export interface DataXReIm {
export interface DataXReIm<ArrayType extends DoubleArray = DoubleArray> {
/** Array of x values */
x: DoubleArray;
x: ArrayType;
/** Array of re values */
re: DoubleArray;
re: ArrayType;
/** Array of im values */
im: DoubleArray;
im: ArrayType;
}
42 changes: 20 additions & 22 deletions src/xreim/__tests__/xreimSortX.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { xreimSortX } from '../../index';
import { xreimSortX } from '../xreimSortX';

describe('xreimSortX', () => {
it('test xreimSortX do nothing', () => {
const x = [0, 1, 2, 3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimSortX({ x, re, im });
test('test xreimSortX do nothing', () => {
const x = [0, 1, 2, 3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimSortX({ x, re, im });

expect(result).toStrictEqual({
x: [0, 1, 2, 3],
re: [0, 1, 2, 3],
im: [4, 5, 6, 7],
});
expect(result).toStrictEqual({
x: [0, 1, 2, 3],
re: [0, 1, 2, 3],
im: [4, 5, 6, 7],
});
});

it('test xreimSortX reverse', () => {
const x = [3, 2, 1, 0];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimSortX({ x, re, im });
test('test xreimSortX reverse', () => {
const x = [3, 2, 1, 0];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimSortX({ x, re, im });

expect(result).toStrictEqual({
x: [0, 1, 2, 3],
re: [3, 2, 1, 0],
im: [7, 6, 5, 4],
});
expect(result).toStrictEqual({
x: [0, 1, 2, 3],
re: [3, 2, 1, 0],
im: [7, 6, 5, 4],
});
});
82 changes: 39 additions & 43 deletions src/xreim/__tests__/xreimZeroFilling.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
import { xreimZeroFilling } from '../../index';
import { xreimZeroFilling } from '../xreimZeroFilling';

describe('xreimZeroFilling', () => {
it('test xreimZeroFilling over', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 6);
const newX = Array.from(result.x).map(
(value) => Math.round(value * 10) / 10,
);
test('test xreimZeroFilling over', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 6);
const newX = Array.from(result.x).map((value) => Math.round(value * 10) / 10);

const newRe = Array.from(result.re);
const newIm = Array.from(result.im);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);

expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1, 0.2, 0.3, 0.4, 0.5],
re: [0, 1, 2, 3, 0, 0],
im: [4, 5, 6, 7, 0, 0],
});
expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1, 0.2, 0.3, 0.4, 0.5],
re: [0, 1, 2, 3, 0, 0],
im: [4, 5, 6, 7, 0, 0],
});
});

it('test xreimZeroFilling equal', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 4);
const newX = Array.from(result.x);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);
test('test xreimZeroFilling equal', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 4);
const newX = Array.from(result.x);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);

expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1, 0.2, 0.3],
re: [0, 1, 2, 3],
im: [4, 5, 6, 7],
});
expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1, 0.2, 0.3],
re: [0, 1, 2, 3],
im: [4, 5, 6, 7],
});
});

it('test xreimZeroFilling under', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 2);
const newX = Array.from(result.x);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);
test('test xreimZeroFilling under', () => {
const x = [0, 0.1, 0.2, 0.3];
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const result = xreimZeroFilling({ x, re, im }, 2);
const newX = Array.from(result.x);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);

expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1],
re: [0, 1],
im: [4, 5],
});
expect({ x: newX, re: newRe, im: newIm }).toStrictEqual({
x: [0, 0.1],
re: [0, 1],
im: [4, 5],
});
});
2 changes: 1 addition & 1 deletion src/xreim/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './xreimZeroFilling';
export * from './xreimSortX';
export * from './xreimZeroFilling';
13 changes: 9 additions & 4 deletions src/xreim/xreimSortX.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { DoubleArray } from 'cheminfo-types';

import { DataXReIm } from '../types';

/**
* Sort object of array, x has to be monotone.
*
* @param data - object of kind {x:[], re:[], im:[]}
* @returns - sorted array
*/
export function xreimSortX(data: DataXReIm): DataXReIm {
export function xreimSortX<ArrayType extends DoubleArray = DoubleArray>(
data: DataXReIm<ArrayType>,
): DataXReIm<ArrayType> {
const { x, re, im } = data;

if (x.length !== re.length || x.length !== im.length) {
Expand All @@ -15,8 +20,8 @@ export function xreimSortX(data: DataXReIm): DataXReIm {
if (x.length < 2 || x[0] < x[1]) return data;

return {
x: x.slice(0).reverse(),
re: re.slice(0).reverse(),
im: im.slice(0).reverse(),
x: x.slice(0).reverse() as ArrayType,
re: re.slice(0).reverse() as ArrayType,
im: im.slice(0).reverse() as ArrayType,
};
}
1 change: 1 addition & 0 deletions src/xreim/xreimZeroFilling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataXReIm } from '../types';

/**
* This function make a zero filling to re and im part.
*
Expand Down

0 comments on commit b6257b5

Please sign in to comment.