Skip to content

Commit

Permalink
chore: update eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Nov 2, 2024
1 parent 6a53960 commit 005118b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 63 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cheminfo from 'eslint-config-cheminfo/base';
import cheminfo from 'eslint-config-cheminfo';
import globals from 'globals';

export default [
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/checkOptions.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'fs';
import fs from 'node:fs';

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

import { parse } from '..';

let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf-8');
let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf8');

describe('SDF Parser options', () => {
let result = parse(sdf, {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/checkUndefined.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'fs';
import fs from 'node:fs';

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

import { parse } from '..';

let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf-8');
let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf8');

describe('SDF Parser options and undefined', () => {
let result = parse(sdf, {
Expand Down
24 changes: 11 additions & 13 deletions src/__tests__/getEntriesBoundaries.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import fs from 'fs';
import fs from 'node:fs';

import { test, expect } from 'vitest';

import { getEntriesBoundaries } from '../getEntriesBoundaries';

let sdf0 = fs.readFileSync(`${__dirname}/test.sdf`, 'utf-8');
let sdf1 = fs.readFileSync(`${__dirname}/test1.sdf`, 'utf-8');
let sdf2 = fs.readFileSync(`${__dirname}/test2.sdf`, 'utf-8');
let sdf0 = fs.readFileSync(`${__dirname}/test.sdf`, 'utf8');
let sdf1 = fs.readFileSync(`${__dirname}/test1.sdf`, 'utf8');
let sdf2 = fs.readFileSync(`${__dirname}/test2.sdf`, 'utf8');

[sdf0, sdf1, sdf2].forEach((sdf) => {
for (const sdf of [sdf0, sdf1, sdf2]) {
let eol = '\n';
let header = sdf.substr(0, 1000);
if (header.indexOf('\r\n') > -1) {
let header = new Set(sdf.slice(0, 1000));
if (header.has('\r\n')) {
eol = '\r\n';
} else if (header.indexOf('\r') > -1) {
} else if (header.has('\r')) {
eol = '\r';
}

test('Split should match regex behavior', () => {
let sdfParts = sdf.split(new RegExp(`${eol}\\$\\$\\$\\$.*${eol}`));
expect(sdfParts).toStrictEqual(
getEntriesBoundaries(sdf, `${eol}$$$$`, eol).map((v) =>
sdf.substring(...v),
),
getEntriesBoundaries(sdf, `${eol}$$$$`, eol).map((v) => sdf.slice(...v)),
);
});
});
}

test('should parse sdf files without EOL in the EOF', () => {
const eol = '\n';
const sdf = fs.readFileSync(`${__dirname}/test4.sdf`, 'utf-8');
const sdf = fs.readFileSync(`${__dirname}/test4.sdf`, 'utf8');

expect(getEntriesBoundaries(sdf, `${eol}$$$$`, eol)).toMatchSnapshot();
});
6 changes: 3 additions & 3 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs';
import fs from 'node:fs';

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

import { parse } from '..';

let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf-8');
let sdf1 = fs.readFileSync(`${__dirname}/test1.sdf`, 'utf-8');
let sdf = fs.readFileSync(`${__dirname}/test.sdf`, 'utf8');
let sdf1 = fs.readFileSync(`${__dirname}/test1.sdf`, 'utf8');

describe('SDF Parser', () => {
let result = parse(sdf);
Expand Down
18 changes: 7 additions & 11 deletions src/__tests__/iterator.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { openAsBlob } from 'fs';
import { join } from 'path';
import { openAsBlob } from 'node:fs';
import { join } from 'node:path';

import { fileCollectionFromPath } from 'filelist-utils';
import { test, expect } from 'vitest';

import { iterator } from '../iterator';

test('iterator', async () => {
const files = (
await fileCollectionFromPath(join(__dirname, '.'))
).files.filter((file) => file.name === 'test.sdf');
const fileCollection = await fileCollectionFromPath(join(__dirname, '.'));
const file = fileCollection.files.find((f) => f.name === 'test.sdf');
const results = [];

const textDecoder = new TextDecoderStream();

Check failure on line 14 in src/__tests__/iterator.test.js

View workflow job for this annotation

GitHub Actions / nodejs / test (16)

src/__tests__/iterator.test.js > iterator

ReferenceError: TextDecoderStream is not defined ❯ src/__tests__/iterator.test.js:14:23
for await (const entry of iterator(
files[0].stream().pipeThrough(textDecoder),
)) {
for await (const entry of iterator(file.stream().pipeThrough(textDecoder))) {
results.push(entry);
}

Expand Down Expand Up @@ -134,9 +131,8 @@ test('iterator on stream', async () => {
});

test('iterator on fileCollection stream', async () => {
const file = (await fileCollectionFromPath(join(__dirname, '.'))).filter(
(file) => file.size === 32233,
).files[0];
const fileCollection = await fileCollectionFromPath(join(__dirname, '.'));
const file = fileCollection.filter((file) => file.size === 32233).files[0];
const results = [];

const textDecoder = new TextDecoderStream();

Check failure on line 138 in src/__tests__/iterator.test.js

View workflow job for this annotation

GitHub Actions / nodejs / test (16)

src/__tests__/iterator.test.js > iterator on fileCollection stream

ReferenceError: TextDecoderStream is not defined ❯ src/__tests__/iterator.test.js:138:23
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/notWellFormatted.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import fs from 'node:fs';

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

import { parse } from '..';

describe('SDF Parser of non well formatted file', () => {
let sdf = fs.readFileSync(`${__dirname}/test2.sdf`, 'utf-8');
sdf = sdf.replace(/\r/g, '');
let sdf = fs.readFileSync(`${__dirname}/test2.sdf`, 'utf8');
sdf = sdf.replaceAll('\r', '');
let result = parse(sdf, { mixedEOL: true });

it('Check molecules', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { parseString } from 'dynamic-typing';

/**

Check warning on line 3 in src/iterator.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @yields declaration
* Parse a SDF file
* @param {ReadableStream} readStream SDF file to parse
* @param {ReadableStream} readStream - SDF file to parse
* @param {object} [options={}]

Check warning on line 6 in src/iterator.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @param "options" description
* @param {Function} [options.filter] Callback allowing to filter the molecules
* @param {string} [options.eol='\n'] End of line character
* @param {boolean} [options.dynamicTyping] Dynamically type the data
* @param {Function} [options.filter] - Callback allowing to filter the molecules
* @param {string} [options.eol='\n'] - End of line character
* @param {boolean} [options.dynamicTyping] - Dynamically type the data
*/
export async function* iterator(readStream, options = {}) {
const { eol = '\n', dynamicTyping = true } = options;
Expand Down Expand Up @@ -49,7 +49,7 @@ function getMolecule(sdfPart, options) {
let lines = parts[j].split(eol);
let from = lines[0].indexOf('<');
let to = lines[0].indexOf('>');
let label = lines[0].substring(from + 1, to);
let label = lines[0].slice(from + 1, to);
for (let k = 1; k < lines.length - 1; k++) {
if (molecule[label]) {
molecule[label] += eol + lines[k];
Expand All @@ -73,7 +73,7 @@ function createLineStream() {
for (let i = 0; i < lines.length - 1; i++) {
controller.enqueue(lines[i]);
}
buffer = lines[lines.length - 1];
buffer = lines.at(-1);
},
flush(controller) {
if (buffer) controller.enqueue(buffer);
Expand Down
30 changes: 15 additions & 15 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { getEntriesBoundaries } from './getEntriesBoundaries';
import { getMolecule } from './util/getMolecule';
/**

Check warning on line 5 in src/parse.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @returns declaration
* Parse a SDF file
* @param {string|ArrayBuffer|Uint8Array} sdf SDF file to parse
* @param {string|ArrayBuffer|Uint8Array} sdf - SDF file to parse
* @param {object} [options={}]
* @param {string[]} [options.include] List of fields to include
* @param {string[]} [options.exclude] List of fields to exclude
* @param {Function} [options.filter] Callback allowing to filter the molecules
* @param {boolean} [options.dynamicTyping] Dynamically type the data
* @param {object} [options.modifiers] Object containing callbacks to apply on some specific fields
* @param {boolean} [options.mixedEOL=false] Set to true if you know there is a mixture between \r\n and \n
* @param {string} [options.eol] Specify the end of line character. Default will be the one found in the file
* @param {string[]} [options.include] - List of fields to include
* @param {string[]} [options.exclude] - List of fields to exclude
* @param {Function} [options.filter] - Callback allowing to filter the molecules
* @param {boolean} [options.dynamicTyping] - Dynamically type the data
* @param {object} [options.modifiers] - Object containing callbacks to apply on some specific fields
* @param {boolean} [options.mixedEOL=false] - Set to true if you know there is a mixture between \r\n and \n
* @param {string} [options.eol] - Specify the end of line character. Default will be the one found in the file
*/
export function parse(sdf, options = {}) {
options = { ...options };
Expand All @@ -28,14 +28,14 @@ export function parse(sdf, options = {}) {
if (options.eol === undefined) {
options.eol = '\n';
if (options.mixedEOL) {
sdf = sdf.replace(/\r\n/g, '\n');
sdf = sdf.replace(/\r/g, '\n');
sdf = sdf.replaceAll('\r\n', '\n');
sdf = sdf.replaceAll('\r', '\n');
} else {
// we will find the delimiter in order to be much faster and not use regular expression
let header = sdf.substr(0, 1000);
if (header.indexOf('\r\n') > -1) {
let header = new Set(sdf.slice(0, 1000));
if (header.has('\r\n')) {
options.eol = '\r\n';
} else if (header.indexOf('\r') > -1) {
} else if (header.has('\r')) {
options.eol = '\r';
}
}
Expand All @@ -52,7 +52,7 @@ export function parse(sdf, options = {}) {
let start = Date.now();

for (let i = 0; i < entriesBoundaries.length; i++) {
let sdfPart = sdf.substring(...entriesBoundaries[i]);
let sdfPart = sdf.slice(...entriesBoundaries[i]);

let currentLabels = [];
const molecule = getMolecule(sdfPart, labels, currentLabels, options);
Expand All @@ -73,7 +73,7 @@ export function parse(sdf, options = {}) {
currentLabel.maxValue = -Infinity;
for (let j = 0; j < molecules.length; j++) {
if (molecules[j][label]) {
let value = parseFloat(molecules[j][label]);
let value = Number.parseFloat(molecules[j][label]);
molecules[j][label] = value;
if (value > currentLabel.maxValue) {
currentLabel.maxValue = value;
Expand Down
15 changes: 8 additions & 7 deletions src/util/getMolecule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getMolecule(sdfPart, labels, currentLabels, options) {
let lines = parts[j].split(options.eol);
let from = lines[0].indexOf('<');
let to = lines[0].indexOf('>');
let label = lines[0].substring(from + 1, to);
let label = lines[0].slice(from + 1, to);
currentLabels.push(label);
if (!labels[label]) {
labels[label] = {
Expand All @@ -16,8 +16,8 @@ export function getMolecule(sdfPart, labels, currentLabels, options) {
keep: false,
};
if (
(!options.exclude || options.exclude.indexOf(label) === -1) &&
(!options.include || options.include.indexOf(label) > -1)
(!options.exclude || !options.exclude.includes(label)) &&
(!options.include || options.include.includes(label))
) {
labels[label].keep = true;
if (options.modifiers[label]) {
Expand All @@ -44,10 +44,11 @@ export function getMolecule(sdfPart, labels, currentLabels, options) {
molecule[label] = modifiedValue;
}
}
if (labels[label].isNumeric) {
if (!isFinite(molecule[label]) || molecule[label].match(/^0[0-9]/)) {
labels[label].isNumeric = false;
}
if (
labels[label].isNumeric &&
(!Number.isFinite(+molecule[label]) || molecule[label].match(/^0[0-9]/))
) {
labels[label].isNumeric = false;
}
}
}
Expand Down

0 comments on commit 005118b

Please sign in to comment.