-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
58 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ import { parseString } from 'dynamic-typing'; | |
|
||
/** | ||
Check warning on line 3 in src/iterator.js GitHub Actions / nodejs / lint-eslint
|
||
* 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 GitHub Actions / nodejs / lint-eslint
|
||
* @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; | ||
|
@@ -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]; | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,15 @@ import { getEntriesBoundaries } from './getEntriesBoundaries'; | |
import { getMolecule } from './util/getMolecule'; | ||
/** | ||
Check warning on line 5 in src/parse.js GitHub Actions / nodejs / lint-eslint
|
||
* 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 }; | ||
|
@@ -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'; | ||
} | ||
} | ||
|
@@ -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); | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters