Leave a spaced dash out of the text of a match that ends before it

This commit is contained in:
dualfroz committed 2026-10-03 01:05:43 +02:00
1 parent 1bb20a37d2
commit 5a4cddfb00
4 files changed
+21 -1

No files matched your search

+7
View File
@@ -167,3 +167,10 @@ test('a range does not stop other dates from splitting', (t) => {
t.equal(res.length, 3, 'three dates found')
t.end()
})
test('a trailing dash is not part of the date', (t) => {
const doc = nlp('2025-05-11 - They have taken the bridge and the Second Hall')
t.equal(doc.dates(context).text(), '2025-05-11', 'iso date before a dash')
t.equal(nlp('june 5 - june 7').dates(context).text(), 'june 5 - june 7', 'dash range')
t.end()
})
+4
View File
@@ -93,6 +93,10 @@ const textFromDoc = function (docs, opts) {
const last = docs.at(-1)
if (!last.at(-1).tags.has('Emoticon')) {
text = trimEnd(text)
// removing a spaced dash leaves its space behind
if (!opts.keepSpace) {
text = text.trimEnd()
}
}
// kill end quotations
if (text.endsWith(`'`) && !text.endsWith(`s'`)) {
+4 -1
View File
@@ -4,6 +4,8 @@ import fmts from './_fmts.js'
const isObject = val => {
return Object.prototype.toString.call(val) === '[object Object]'
}
// a dash between two words, like '2025-05-11 - they'
const spacedDash = /^\s+[-—]\s+$/
export default {
/** */
@@ -21,7 +23,8 @@ export default {
}
if (opts.keepEndPunct === undefined && this.pointer) {
const ptr = this.pointer[0]
if (ptr?.[1]) {
const lastTerm = this.docs.at(-1)?.at(-1)
if (ptr?.[1] || spacedDash.test(lastTerm?.post)) {
opts.keepEndPunct = false
} else {
opts.keepEndPunct = true
+6
View File
@@ -25,6 +25,12 @@ test('whitespace-out', function (t) {
t.end()
})
test('match before a spaced dash', function (t) {
t.equal(nlp('june 5 - they left').match('june 5').text(), 'june 5', here + 'first words')
t.equal(nlp('i saw john - he left').match('john').text(), 'john', here + 'middle word')
t.end()
})
test('pre/post concat', function (t) {
const doc = nlp(`Getting ready for whacking day? What's whacking day?`)
doc.post(' ', true)