diff --git a/plugins/dates/tests/regressions.test.js b/plugins/dates/tests/regressions.test.js index 13fec18..4ab32b1 100644 --- a/plugins/dates/tests/regressions.test.js +++ b/plugins/dates/tests/regressions.test.js @@ -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() +}) diff --git a/src/1-one/output/api/_text.js b/src/1-one/output/api/_text.js index ac1c47b..e2b1e4d 100644 --- a/src/1-one/output/api/_text.js +++ b/src/1-one/output/api/_text.js @@ -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'`)) { diff --git a/src/1-one/output/api/text.js b/src/1-one/output/api/text.js index dc39338..1b53b1f 100644 --- a/src/1-one/output/api/text.js +++ b/src/1-one/output/api/text.js @@ -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 diff --git a/tests/one/change/whitespace.test.js b/tests/one/change/whitespace.test.js index e9dd214..5d9956e 100644 --- a/tests/one/change/whitespace.test.js +++ b/tests/one/change/whitespace.test.js @@ -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)