sdf-parser 7.0.2 → 7.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -214,12 +214,18 @@ class MolfileStream extends TransformStream {
214
214
  this.#buffer += chunk;
215
215
  let begin = 0;
216
216
  let index = 0;
217
- while ((index = this.#buffer.indexOf('\n$$$$', index)) !== -1) {
218
- controller.enqueue(this.#buffer.slice(begin, index));
219
- index += 5;
220
- if (this.#buffer[index] === '\r') {
221
- index++;
217
+ while ((index = this.#buffer.indexOf('$$$$', index)) !== -1) {
218
+ // we need to check if the delimiter '\n' is in the current buffer
219
+ // if it is not we need to wait for the next chunk
220
+ const endOfDelimiter = this.#buffer.indexOf('\n', index);
221
+ if (endOfDelimiter === -1) {
222
+ index = begin;
223
+ break;
222
224
  }
225
+ const eolLength = this.#buffer[endOfDelimiter - 1] === '\r' ? 2 : 1;
226
+ // need to remove the last eol because we will split on eol+'>' in getMolecule
227
+ controller.enqueue(this.#buffer.slice(begin, index - eolLength));
228
+ index = endOfDelimiter + eolLength;
223
229
  begin = index;
224
230
  }
225
231
  this.#buffer = this.#buffer.slice(begin);
@@ -234,7 +240,7 @@ class MolfileStream extends TransformStream {
234
240
  }
235
241
 
236
242
  /**
237
- * Parse a SDF file
243
+ * Parse a SDF file as an iterator
238
244
  * @param {ReadableStream} readStream - SDF file to parse
239
245
  * @param {object} [options={}] - iterator options
240
246
  * @param {Function} [options.filter] - Callback allowing to filter the molecules
@@ -245,10 +251,10 @@ class MolfileStream extends TransformStream {
245
251
  async function* iterator(readStream, options = {}) {
246
252
  const { eol = '\n', dynamicTyping = true } = options;
247
253
 
248
- const moleculeStream = readStream.pipeThrough(new MolfileStream());
249
- for await (const molfile of moleculeStream) {
250
- if (molfile.length < 20) continue;
251
- const molecule = getMolecule(molfile, {
254
+ const moleculeStream = readStream.pipeThrough(new MolfileStream({ eol }));
255
+ for await (const entry of moleculeStream) {
256
+ if (entry.length < 20) continue;
257
+ const molecule = getMolecule(entry, {
252
258
  eol,
253
259
  dynamicTyping,
254
260
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdf-parser",
3
- "version": "7.0.2",
3
+ "version": "7.0.3",
4
4
  "description": "SDF parser",
5
5
  "main": "lib/index.js",
6
6
  "module": "src/index.js",
@@ -7,12 +7,18 @@ export class MolfileStream extends TransformStream {
7
7
  this.#buffer += chunk;
8
8
  let begin = 0;
9
9
  let index = 0;
10
- while ((index = this.#buffer.indexOf('\n$$$$', index)) !== -1) {
11
- controller.enqueue(this.#buffer.slice(begin, index));
12
- index += 5;
13
- if (this.#buffer[index] === '\r') {
14
- index++;
10
+ while ((index = this.#buffer.indexOf('$$$$', index)) !== -1) {
11
+ // we need to check if the delimiter '\n' is in the current buffer
12
+ // if it is not we need to wait for the next chunk
13
+ const endOfDelimiter = this.#buffer.indexOf('\n', index);
14
+ if (endOfDelimiter === -1) {
15
+ index = begin;
16
+ break;
15
17
  }
18
+ const eolLength = this.#buffer[endOfDelimiter - 1] === '\r' ? 2 : 1;
19
+ // need to remove the last eol because we will split on eol+'>' in getMolecule
20
+ controller.enqueue(this.#buffer.slice(begin, index - eolLength));
21
+ index = endOfDelimiter + eolLength;
16
22
  begin = index;
17
23
  }
18
24
  this.#buffer = this.#buffer.slice(begin);
package/src/iterator.js CHANGED
@@ -3,7 +3,7 @@ import { parseString } from 'dynamic-typing';
3
3
  import { MolfileStream } from './MolfileStream.js';
4
4
 
5
5
  /**
6
- * Parse a SDF file
6
+ * Parse a SDF file as an iterator
7
7
  * @param {ReadableStream} readStream - SDF file to parse
8
8
  * @param {object} [options={}] - iterator options
9
9
  * @param {Function} [options.filter] - Callback allowing to filter the molecules
@@ -14,10 +14,10 @@ import { MolfileStream } from './MolfileStream.js';
14
14
  export async function* iterator(readStream, options = {}) {
15
15
  const { eol = '\n', dynamicTyping = true } = options;
16
16
 
17
- const moleculeStream = readStream.pipeThrough(new MolfileStream());
18
- for await (const molfile of moleculeStream) {
19
- if (molfile.length < 20) continue;
20
- const molecule = getMolecule(molfile, {
17
+ const moleculeStream = readStream.pipeThrough(new MolfileStream({ eol }));
18
+ for await (const entry of moleculeStream) {
19
+ if (entry.length < 20) continue;
20
+ const molecule = getMolecule(entry, {
21
21
  eol,
22
22
  dynamicTyping,
23
23
  });