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

defuFn return wrong expected value while there have multiple default #139

Open
byronogis opened this issue Dec 13, 2024 · 1 comment · May be fixed by #141
Open

defuFn return wrong expected value while there have multiple default #139

byronogis opened this issue Dec 13, 2024 · 1 comment · May be fixed by #141
Labels
bug Something isn't working

Comments

@byronogis
Copy link

Environment

node -v
v22.11.0

Reproduction

See Describe the bug

Describe the bug

import { defuFn } from 'defu'

const filterDist = (val: string[]) => val.filter(i => i !== 'dist')
const addTwenty = (val: number) => val + 20
const addTen = (val: number) => val + 10

const res = defuFn(
  {
    count: addTwenty,
    num: addTen,
    items: filterDist,
  },
  {
    count: 10,
    num: 5,
    items: ['node_modules', 'test'],
  },
  {
    count: 5,
    num: 3,
    items: ['temp', 'dist'],
  },
)

console.dir({ res }, { depth: 5 })

// expected: 
/*
{
  res: {
    count: 30,
    num: 15,
    items: [ "node_modules", "test", "temp"]
  }
}
*/

// received: 
/*
{
  res: {
    count: 30,
    num: 15,
    items: [ "node_modules", "test", "temp", "dist" ]
  }
}
*/

Additional context

When entering the second loop, the function has already been replaced with a value

arguments_.reduce((p, c) => _defu(p, c, "", merger), {} as any);

Logs

No response

@byronogis byronogis added the bug Something isn't working label Dec 13, 2024
@byronogis
Copy link
Author

byronogis commented Dec 13, 2024

Maybe this is a idea.

// createDefu
_defu(
  arguments_[0],
  arguments_.slice(1).reduce((p, c) => _defu(p, c, "", merger), {} as any),
    "",
  merger,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant