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

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

Open
Fdawgs opened this issue May 23, 2024 · 2 comments
Open

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

Fdawgs opened this issue May 23, 2024 · 2 comments

Comments

@Fdawgs
Copy link
Contributor

Fdawgs commented May 23, 2024

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'
}
 */
@Fdawgs Fdawgs changed the title Compromise-dates plugin mutates context object on function calls Compromise-dates plugin mutates context param obj on function calls May 23, 2024
@spencermountain
Copy link
Owner

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

@Fdawgs
Copy link
Contributor Author

Fdawgs commented Jun 5, 2024

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.

spencermountain added a commit that referenced this issue Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants