Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaceWith('') call with a capture group at beginning of match throws error #1150

Closed
Fdawgs opened this issue Oct 9, 2024 · 3 comments
Closed

Comments

@Fdawgs
Copy link
Contributor

Fdawgs commented Oct 9, 2024

Node version: 20.17.0
Compromise version: 14.14.1

From looking through the commits to 14.14.1 this may have been introduced with #1146?

When calling match().replaceWith('') after calling it once already with a capture group at the beginning causes it to throw:

TypeError: Cannot read properties of undefined (reading 'length')
    at T.replaceWith (/home/fsmith/application/node_modules/compromise/builds/three/compromise-three.cjs:1:11511)
    at Object.<anonymous> (/home/fsmith/application/tests/scratch/feat-18244.js:10:23)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49

Example:

'use strict'

/** @type {import('compromise').default} */
const nlp = require('compromise')

const text = 'to the window'

// Throws an error, capturing group at beginning, replacing with an empty string
try {
  const doc1 = nlp(text)
  doc1.match('[to] the window', 0).replaceWith('by')
  doc1.match('[by]', 0).replaceWith('')
} catch (e) {
  console.error('doc1', e)
}

// Works, capturing group at beginning, replacing with a non-empty string
const doc2 = nlp(text)
doc2.match('[to] the window', 0).replaceWith('near')
doc2.match('[near]', 0).replaceWith('by')
console.log('doc2', doc2.text())

// Works, capture group at end, replacing with an empty string
const doc3 = nlp(text)
doc3.match('to the [window]', 0).replaceWith('wall')
doc3.match('[wall]', 0).replaceWith('')
console.log('doc3', doc3.text())
@spencermountain
Copy link
Owner

spencermountain commented Oct 9, 2024 via email

@Fdawgs
Copy link
Contributor Author

Fdawgs commented Oct 9, 2024

Thanks @spencermountain! Have updated the example to make it easier to see.

spencermountain added a commit that referenced this issue Oct 10, 2024
@spencermountain spencermountain mentioned this issue Oct 10, 2024
@spencermountain
Copy link
Owner

got a fix for the runtime error released as 14.14.2
thanks for the heads-up, let me know if you see anything else
cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants