Skip to content

Commit

Permalink
chore: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Oct 2, 2024
1 parent 2f6fc10 commit 2daa363
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 70 deletions.
1 change: 0 additions & 1 deletion .eslintrc.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .ncurc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dep: 'prod,dev'
reject:
# https://github.com/cheminfo/eslint-config/issues/49
- eslint
- react-plot
16 changes: 16 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import cheminfo from 'eslint-config-cheminfo-typescript';
import globals from 'globals';

export default [
...cheminfo,
{
languageOptions: {
globals: {
...globals.node,
},
},
rules: {
"unicorn/no-object-as-default-parameter": "off",
}
}
]
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build": "npm run tsc && cheminfo-build -r CommonSpectrum",
"check-types": "tsc --noEmit",
"clean": "rimraf lib lib-esm",
"eslint": "eslint src --ext ts --cache",
"eslint": "eslint src",
"eslint-fix": "npm run eslint -- --fix",
"prepack": "npm run tsc",
"prettier": "prettier --check src",
Expand Down Expand Up @@ -49,10 +49,10 @@
"@types/js-quantities": "^1.6.6",
"cheminfo-build": "^1.2.0",
"eslint": "^8.57.0",
"eslint-config-cheminfo-typescript": "^12.4.0",
"eslint-config-cheminfo-typescript": "^15.0.1",
"jest-matcher-deep-close-to": "^3.0.2",
"prettier": "^3.3.3",
"rimraf": "^5.0.5",
"rimraf": "^6.0.1",
"typescript": "^5.4.5"
},
"dependencies": {
Expand Down
11 changes: 7 additions & 4 deletions src/AnalysesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class AnalysesManager {
for (const spectrum of this.getSpectra()) {
if (spectrum.variables) {
for (const [, variable] of Object.entries(spectrum.variables)) {
appendDistinctValue(values, variable.label.replace(/\s+[[(].*$/, ''));
appendDistinctValue(values, variable.label.replace(/\s+[([].*$/, ''));
}
}
}
Expand Down Expand Up @@ -198,6 +198,7 @@ export class AnalysesManager {

/**
* Remove the analysis from the AnalysesManager for the specified id
* @param id
*/
public removeAnalysis(id: string) {
const index = this.getAnalysisIndex(id);
Expand All @@ -207,6 +208,7 @@ export class AnalysesManager {

/**
* Returns the index of the analysis in the analyses array
* @param id
*/
public getAnalysisIndex(id: string) {
if (!id) return undefined;
Expand All @@ -219,10 +221,11 @@ export class AnalysesManager {

/**
* Checks if the ID of an analysis exists in the AnalysesManager
* @param id
*/
public includes(id: string) {
const index = this.getAnalysisIndex(id);
return index === undefined ? false : !isNaN(index);
return index === undefined ? false : !Number.isNaN(index);
}
}

Expand All @@ -233,8 +236,8 @@ function normalizeLabelUnits(
if (!originalLabel) {
return { units: '', label: '' };
}
if (originalLabel.search(/[[(]]/) >= 0) {
const [units, label] = originalLabel.split(/\s*[[(]/);
if (originalLabel.search(/[([]]/) >= 0) {
const [units, label] = originalLabel.split(/\s*[([]/);
return { units: originalUnits || units, label };
}
return { label: originalLabel, units: originalUnits };
Expand Down
38 changes: 25 additions & 13 deletions src/Analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Analysis {
};

public constructor(options: AnalysisOptions = {}) {
this.id = options.id || Math.random().toString(36).substring(2, 10);
this.id = options.id || Math.random().toString(36).slice(2, 10);
this.label = options.label || this.id;
this.spectrumCallback = options.spectrumCallback;
this.spectra = [];
Expand Down Expand Up @@ -81,6 +81,8 @@ export class Analysis {

/**
* Add a spectrum in the internal spectra variable
* @param variables
* @param options
*/
public pushSpectrum(
variables: SpectrumVariables,
Expand All @@ -96,6 +98,7 @@ export class Analysis {

/**
* Retrieve a Spectrum based on x/y units
* @param selector
*/
public getXYSpectrum(selector: SpectrumSelector = {}) {
const id = JSON.stringify(selector);
Expand All @@ -107,6 +110,7 @@ export class Analysis {

/**
* Retrieve spectra matching selector
* @param selector
*/
public getXYSpectra(selector: SpectrumSelector = {}) {
const id = JSON.stringify(selector);
Expand All @@ -118,9 +122,10 @@ export class Analysis {

/**
* Retrieve a xy object
* @param selector.units Units separated by vs like for example "g vs °C"
* @param selector.xUnits if undefined takes the first variable
* @param selector.yUnits if undefined takes the second variable
* @param selector.units - Units separated by vs like for example "g vs °C"
* @param selector.xUnits - if undefined takes the first variable
* @param selector.yUnits - if undefined takes the second variable
* @param selector
*/
public getXY(selector: SpectrumSelector = {}) {
const spectrum = this.getXYSpectrum(selector);
Expand All @@ -134,8 +139,9 @@ export class Analysis {
/**
* Return the data object for specific x/y units with possibly some
* normalization options
* @param options.selector.xUnits // if undefined takes the first variable
* @param options.selector.yUnits // if undefined takes the second variable
* @param options.selector.xUnits - // if undefined takes the first variable
* @param options.selector.yUnits - // if undefined takes the second variable
* @param options
*/
public getNormalizedSpectrum(options: NormalizedOptions = {}) {
const { normalization, selector } = options;
Expand All @@ -145,6 +151,7 @@ export class Analysis {
}

/**
* @param options
*/
public getNormalizedSpectra(options: NormalizedOptions = {}): Spectrum[] {
const { normalization, selector } = options;
Expand All @@ -167,17 +174,19 @@ export class Analysis {

/**
* Returns the xLabel
* @param selector.xUnits // if undefined takes the first variable
* @param selector.yUnits // if undefined takes the second variable
* @param selector.xUnits - // if undefined takes the first variable
* @param selector.yUnits - // if undefined takes the second variable
* @param selector
*/
public getXLabel(selector: SpectrumSelector) {
return this.getXYSpectrum(selector)?.variables.x.label;
}

/**
* Returns the yLabel
* @param selector.xUnits // if undefined takes the first variable
* @param selector.yUnits // if undefined takes the second variable
* @param selector.xUnits - // if undefined takes the first variable
* @param selector.yUnits - // if undefined takes the second variable
* @param selector
*/
public getYLabel(selector: SpectrumSelector) {
return this.getXYSpectrum(selector)?.variables.y.label;
Expand All @@ -186,6 +195,9 @@ export class Analysis {

/**
* Internal function that ensure the order of x / y array
* @param variables
* @param options
* @param analysisOptions
*/
function standardizeData(
variables: SpectrumVariables,
Expand All @@ -207,14 +219,14 @@ function standardizeData(
const xVariable = variables.x;
const yVariable = variables.y;
if (!xVariable || !yVariable) {
throw Error('A spectrum must contain at least x and y variables');
throw new Error('A spectrum must contain at least x and y variables');
}
if (!isAnyArray(xVariable.data) || !isAnyArray(yVariable.data)) {
throw Error('x and y variables must contain an array data');
throw new Error('x and y variables must contain an array data');
}

const x = xVariable.data;
const reverse = x && x.length > 1 && x[0] > x[x.length - 1];
const reverse = x && x.length > 1 && x[0] > x.at(-1);

Check failure on line 229 in src/Analysis.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Object is possibly 'undefined'.

for (const [key, variable] of Object.entries(variables)) {
if (reverse) variable.data = variable.data.slice().reverse();
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/AnalysesManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('AnalysisManager', () => {
const analyses = analysesManager.getAnalyses();
expect(analyses).toHaveLength(3);
// we expect to have a total of 3 spectra without filtereing
expect(analyses.map((analysis) => analysis.spectra).flat()).toHaveLength(3);
expect(analyses.flatMap((analysis) => analysis.spectra)).toHaveLength(3);

// we select the first analysis, we expect to have 2 spectra
const filteredAnalyses = analysesManager.getAnalyses({ ids: ['abc'] });
Expand All @@ -83,7 +83,7 @@ describe('AnalysisManager', () => {
const analysis2 = new Analysis({ id: 'def' });
analysesManager.addAnalysis(analysis2);

const json = JSON.parse(JSON.stringify(analysesManager));
const json = structuredClone(analysesManager);
const data = json.analyses[0].spectra[0].variables.x.data;
expect(Array.isArray(data)).toBe(true);

Check failure on line 88 in src/__tests__/AnalysesManager.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (18)

src/__tests__/AnalysesManager.test.ts > AnalysisManager > check toJSON / fromJSON

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/__tests__/AnalysesManager.test.ts:88:33

Check failure on line 88 in src/__tests__/AnalysesManager.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (20)

src/__tests__/AnalysesManager.test.ts > AnalysisManager > check toJSON / fromJSON

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/__tests__/AnalysesManager.test.ts:88:33

Check failure on line 88 in src/__tests__/AnalysesManager.test.ts

View workflow job for this annotation

GitHub Actions / nodejs / test (22)

src/__tests__/AnalysesManager.test.ts > AnalysisManager > check toJSON / fromJSON

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/__tests__/AnalysesManager.test.ts:88:33
const spectraManager2 = AnalysesManager.fromJSON(json);
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/Analysis.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { describe, it, expect } from 'vitest';

Expand All @@ -9,7 +9,7 @@ describe('Analysis', () => {
it('getXY', () => {
const text = readFileSync(
join(__dirname, '../../testFiles/xps.jdx'),
'utf-8',
'utf8',
);

const analysis = fromJcamp(text);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/AnalysisManager.distinct.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { describe, it, expect } from 'vitest';

Expand Down
4 changes: 2 additions & 2 deletions src/from/__tests__/fromJcamp.callback.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { describe, it, expect } from 'vitest';

Expand Down
4 changes: 2 additions & 2 deletions src/from/__tests__/fromJcamp.link.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { test, expect } from 'vitest';

Expand Down
4 changes: 2 additions & 2 deletions src/from/__tests__/fromJcamp.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { describe, it, expect } from 'vitest';

Expand Down
4 changes: 2 additions & 2 deletions src/from/__tests__/fromText.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { test, expect } from 'vitest';

Expand Down
12 changes: 6 additions & 6 deletions src/from/fromJcamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { SpectrumVariable } from '../types/Cheminfo';

/**
* Creates a new Analysis from a JCAMP string
* @param {string} jcamp - String containing the JCAMP data
* @param {object} [options={}]
* @param {object} [options.id=Math.random()]
* @param {string} [options.label=options.id] human redeable label
* @param {string} [options.spectrumCallback] a callback to apply on variables when creating spectrum
* @return {Analysis} - New class element with the given data
* @param jcamp - String containing the JCAMP data
* @param [options={}]
* @param [options.id=Math.random()]
* @param [options.label=options.id] - human redeable label
* @param [options.spectrumCallback] - a callback to apply on variables when creating spectrum
* @returns - New class element with the given data
*/
export function fromJcamp(jcamp: string | ArrayBuffer, options = {}): Analysis {
const analysis = new Analysis(options);
Expand Down
3 changes: 2 additions & 1 deletion src/from/fromText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ interface FromTextOptions {

/**
* Convert strings into JCAMP and add extra information
* @param {string} data - values to add to the file, usually a csv or tsv values
* @param data - values to add to the file, usually a csv or tsv values
* @param options
*/

export function fromText(
Expand Down
4 changes: 2 additions & 2 deletions src/jsgraph/__tests__/getJSGraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { test, expect, describe, it } from 'vitest';

Expand Down
2 changes: 1 addition & 1 deletion src/jsgraph/addStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function addStyle(
let { color = '#A9A9A9' } = options;
const { opacity = 1, lineWidth = 1 } = options;

if (color.match(/#[0-9A-F]{6}$/i)) {
if (color.match(/#[\da-f]{6}$/i)) {
color = (color + ((opacity * 255) >> 0).toString(16)).toUpperCase();
} else {
color = color.replace(/rgb ?\((.*)\)/, `rgba($1,${opacity})`);
Expand Down
2 changes: 2 additions & 0 deletions src/jsgraph/getJSGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { COLORS } from './colors';

/**
* Generate a jsgraph chart format from an array of Analysis
* @param analyses
* @param options
*/
export function getJSGraph(analyses: Analysis[], options: JSGraphOptions = {}) {
const {
Expand Down
9 changes: 6 additions & 3 deletions src/jsgraph/getNormalizationAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/**
* Returns a JSGraph annotation that represents the normalization
* @param {object} [filter={}]
* @param {object} [filter.exclusions=[]] Array of exclusions zones
* @param {object} [boundary={y: {min:'0px', max:'2000px'}}] Height of the annotation
* @param {object} [filter.exclusions=[]] - Array of exclusions zones
* @param {object} [boundary={y: {min:'0px', max:'2000px'}}] - Height of the annotation
*/
interface AnnotationsFilter {
exclusions?: Array<{ ignore?: boolean; from: number; to: number }>;
from?: number;
to?: number;
}

interface Boundary { y: { min: string; max: string } }

export function getNormalizationAnnotations(
filter: AnnotationsFilter = {},
boundary = { y: { min: '0px', max: '2000px' } },
boundary: Boundary = { y: { min: '0px', max: '2000px' } },
) {
let { exclusions = [] } = filter;

Expand Down
3 changes: 3 additions & 0 deletions src/reactPlot/getReactPlotJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function getData(x: ListNumber, y: ListNumber) {

/**
* Generate a jsgraph chart format from an array of Analysis
* @param analyses
* @param query
* @param options
*/
export function getReactPlotJSON(
analyses: Analysis[],
Expand Down
6 changes: 4 additions & 2 deletions src/types/SpectrumSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export interface SpectrumSelector {
yLabel?: string | RegExp;
/** Allows to specify X and Y variables using a string like 'c vs d' */
variables?: string;
/** Select a specific X variable by one letter name
/**
* Select a specific X variable by one letter name
* @default 'x'
*/
xVariable?: OneLowerCase;
/** Select a specific Y variable by one letter name
/**
* Select a specific Y variable by one letter name
* @default 'y'
*/
yVariable?: OneLowerCase;
Expand Down
Loading

0 comments on commit 2daa363

Please sign in to comment.