unjs / defu

🌊 Assign default properties recursively
MIT License
1.06k stars 22 forks source link

merge date bug #100

Closed 171h closed 1 year ago

171h commented 1 year ago

Environment

defu development environment

Reproduction

.

Describe the bug

// this repo/test/defu.test.ts

  it('merge Date', () => {
    const defaultObj = { date: new Date(), date2: new Date() }
    const obj2 = { date: new Date('2021-01-01'), date2: new Date('2021-01-02') }
    const obj3 = { date: '2021-01-01', date2: '2021-01-02' }
    console.log(obj2)
    const newObj2 = defu(obj2,  defaultObj)
    const newObj3 = defu(obj3,  defaultObj)
    console.log('results', { defaultObj, obj2, obj3, newObj2, newObj3 })
  })

// output

RERUN  test/defu.test.ts x35

stdout | test/defu.test.ts > defu > merge Date
results {
  defaultObj: { date: 2023-06-29T03:44:08.346Z, date2: 2023-06-29T03:44:08.346Z },
  obj2: { date: 2021-01-01T00:00:00.000Z, date2: 2021-01-02T00:00:00.000Z },
  obj3: { date: '2021-01-01', date2: '2021-01-02' },
  newObj2: { date: {}, date2: {} },
  newObj3: { date: '2021-01-01', date2: '2021-01-02' }
}

results.newObj2 should toBe obj2

Additional context

.

Logs

.
darvids0n commented 1 year ago

Probably the correct way to use objects that you don't want defu to try and deep-merge, is to convert them toString() before setting into your input or defaults objects. Defu is a minimal implementation and this would be the only special case, which seems wrong.