spencermountain / compromise

modest natural-language processing
http://compromise.cool
MIT License
11.31k stars 645 forks source link

Compromise-dates plugin mutates context param obj on function calls #1109

Open Fdawgs opened 1 month ago

Fdawgs commented 1 month ago

Node version: 20.13.1 Compromise version: 14.13.0 Compromise-dates version: 3.5.0

Using the compromise-dates plugin, any call to the .dates() functions (.json(), .get(), .format() etc.) mutates the today value in the context param obj to a SpaceTime object, which is an unexpected side-effect:

'use strict'

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

const doc = nlp("I'll deal with that bug tomorrow")

const context = {
  timezone: 'Canada/Eastern',
  today: '2020-02-20',
  punt: { weeks: 2 },
  dayStart: '8:00am',
  dayEnd: '5:30pm',
}

const datesViews = doc.dates(context)
// context obj is still the same at this point
console.log(context)

const dateList = datesViews.json()

console.log(context)
/* context obj is now:
{
  timezone: 'Canada/Eastern',
  today: SpaceTime {
    epoch: 1582174800000,
    tz: 'canada/eastern',
    silent: true,
    british: undefined,
    _weekStart: 1,
    _today: {}
  },
  punt: { weeks: 2 },
  dayStart: '8:00am',
  dayEnd: '5:30pm'
}
 */
spencermountain commented 1 month ago

Hey! Good catch - .json should definitely never mutate the object - getting swamped at work, will look at a fix hopefully this weekend Cheers

Fdawgs commented 1 month ago

Thanks @spencermountain, no worries! Just to reiterate, it seems to be all/most of the .dates() functions mutates it, not just .json(). I.e. doc.dates(context).format('{month} {date-ordinal}') also does it.