Find payloads by term id so they survive a .remove()

This commit is contained in:
dualfroz committed 2026-10-03 03:19:54 +02:00
1 parent 21ceba9e20
commit 2bef4a0319
2 files changed
+77 -30

No files matched your search

+23
View File
@@ -57,3 +57,26 @@ test('payload-fn', function (t) {
t.equal(doc.getPayloads().length, 5, '5-now')
t.end()
})
test('payload-after-remove', function (t) {
let doc = nlp('one apple. two pears. three plums.')
doc.match('(apple|plums)').addPayload(m => ({ fruit: m.text('normal') }))
t.equal(doc.getPayloads().length, 2, 'before-remove')
doc.remove('two pears')
let fruit = doc.getPayloads().map(p => p.val.fruit)
t.deepEqual(fruit, ['apple', 'plums'], 'after-remove')
t.equal(doc.match('plums').getPayloads().length, 1, 'plums-still-found')
doc.match('plums').clearPayloads()
fruit = doc.getPayloads().map(p => p.val.fruit)
t.deepEqual(fruit, ['apple'], 'clear-after-remove')
// removing the payload's own words drops it
doc = nlp('one apple. two pears. three plums.')
doc.match('(apple|pears|plums)').addPayload(m => ({ fruit: m.text('normal') }))
doc.remove('two pears')
fruit = doc.getPayloads().map(p => p.val.fruit)
t.deepEqual(fruit, ['apple', 'plums'], 'removed-payload-is-gone')
t.end()
})