Git Product home page Git Product logo

defu's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

defu's Issues

merge date bug

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

.

bug: unexpected type when defu two arrays

When I use:

const a = defu([1, 2], [])

The type of a will be object not array. That cause I cannot use map or any other array's function to a
Is that a feature or a bug?

[Bug] Objects exported with '* as' export aren't merged recursively

Environment

Operating System: Linux
Node Version: v18.18.0
Nuxt Version: 3.8.2
CLI Version: 3.10.0
Nitro Version: 2.8.1
Package Manager: [email protected]
Builder: -
User Config: devtools, extends
Runtime Modules: -
Build Modules: -

Reproduction

https://stackblitz.com/edit/github-bcxfrv?file=README.md

Describe the bug

merge app.config from different layers using namespace imports to combine different parts of the config, e.g:

import * as pages from './config/pages'

export default defineAppConfig({
  pages,
})

Check the console:

Expected:

{ nuxt: { buildId: 'dev' }, pages: { foo: { nested: 1 }, bar: 2 } }

Actual:

{ nuxt: { buildId: 'dev' }, pages: { bar: 2 } }

Additional context

This is a regression that was introduced with 6.1.3 release, specifically with this PR #111
@pi0 I'm quite dissapointed with the overall workflow this change was merged with. Test coverage was very small for such fundamental change, but even those tests that were written were skipped, there was no code review. Was it some kind of pre-holiday accident? πŸ₯²
IMO, any changes like this deserves at least minor (not a patch) and better - major version bump because it's a fundamental change to how objects are merged and this is a main goal of this lib.

Logs

No response

request: schema support

By default, giving a schema,
If the incoming new data is the same key, append the new data to the values of the schema keys.
delete keys that are not compatible with the schema.

Example

const schema = {
    isPro: false,
    darkMode: false,
    pages: {
        home: false,
        settings: false,
    },
}

const result = defu(schema, { isPro: 'bbb', d: 'c', pages: { home: true } }, {schema: true})

console.log(result) // {isPro: 'bbb', darkMode: false, pages: { home: true, settings: false } }

Array merge order is unintuitive

Environment

Node.js: v18.12.1
defu: 6.1.2

Reproduction

https://github.com/dword-design/demo-defu-array-merge

Describe the bug

defu({ plugins: ['local 1', 'local 2'] }, { plugins: ['inherited 1', 'inherited 2'] })

Expected: { plugins: ['inherited 1', 'inherited 2', 'local 1', 'local 2'] }.
Actual: { plugins: ['local 1', 'local 2', 'inherited 1', 'inherited 2'] }.

I would expect the order in the array to always be parent first and then child. Has direct implications if you have a plugin or module system where the order is important.

Additional context

No response

Logs

No response

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v3
npm
package.json
  • @types/node ^20.10.6
  • @vitest/coverage-v8 ^1.1.3
  • changelogen ^0.5.5
  • eslint ^8.56.0
  • eslint-config-unjs ^0.2.1
  • expect-type ^0.17.3
  • prettier ^3.1.1
  • typescript ^5.3.3
  • unbuild ^2.0.0
  • vitest ^1.1.3
  • pnpm 8.10.2

  • Check this box to trigger a request for Renovate to run again on this repository

The array values from config should be appended to array values from base/defaults

Environment

  • Version: 6.1.4
  • Node: 18

Reproduction

https://stackblitz.com/edit/typescript-1ggsvw

In the console you can see the ["c", "d", "a", "b"] instead of ["a", "b", "c", "d"], where ["a", "b"] are base values/defaults and ["c", "d"] are config values.

Describe the bug

I'd expect the array values from config to be appended to values from base/defaults. Currently it's the other way around.

Additional context

nuxt/nuxt#25719

Logs

No response

Cannot assign to read only property 'exports' of object '#<Object>'

Hi can you tell me please how to fix this error:

Cannot assign to read only property 'exports' of object '#'

This library is used by some third party lib but as you can see the application build is not able to work with this type of module export. Its a Nuxt application but this error start to appear as I install some Babel packages. Here is detailed description: https://stackoverflow.com/questions/70785689/nuxt-with-babel-throws-uncaught-typeerror-cannot-assign-to-read-only-property

Thanks.

4.0.1 is not compatible with IE11

I'm not sure whether this bug report belongs here, or in the Nuxt repo, or in the @nuxtjs/axios repo...

With the latest version of Nuxt (2.15.5) and @nuxtjs/axios (5.13.1), an error is thrown when visiting a page in IE11, because defu is included in _nuxt/vendors/app.js, with the following code present (from node_modules/defu/dist/defu.mjs):

function _defu(baseObj, defaults, namespace = ".", merger) {
...

To reproduce:

Couldn't transpile this module

This module uses ES6 code which is problematic if the target is IE11, we can transpile it, however at the end of the lib file the module is exported as modules.export instead of default export which is problematic if we transpile it with Babel.

It should be default export to be consistent with the rest of the code (ES6).

Allow two way function merger

Currently function merger is only appied only if (defaults) has the function merger functionality however it is sometimes usable to also support merger for the input config. (nuxt/nuxt#21462 (comment))

Note: This behavior was for performance reasons. We might find a workaround directly from function merger custom export (or a new export to avoid breaking changes)

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: rules

Defu is not a function

You should roll back to the old version (6.1.0) if the fix will take time.
You have released breaking changes as a minor version update (6.1.0 to 6.1.1)
Our production build has been stopped for the last 6 hrs
there is a huge revenue impact because of this

Your other module rc9 uses defu and it is breaking our nuxt build as rc9 is used during the build process

Please reach out if you need any help

defu is not a function

Rollback your 6.1.1 to 6.1.0 as it contains breaking changes and release it as a major release

clone support

Currently, we only create new object/array when both defaults and current value have a value while if only there is value here we set it.

I am thinking to introduce a new option to createDefu(merger, { clone: true } to enable cloning behavior.

An alternative would be using structuredClone however i think it also copies the values (non object/array) and taking more memory also not available in (at least Node) workers.

✨ Bring back nullish values support via option

Describe the feature

Hi πŸ‘‹πŸ»

I'm author of Anu UI lib. I'm using defu and it's doing an amazing job until I found that it skips nullish values undefined & null 😞

Why?

If we want to allow explicitly set null or undefined for property it's not possible ATM.

I'm trying to implement global prop defaults via vue plugin options for my lib. If user wants to disable some prop, the user can pass undefined in the plugin option but unfortunately defu skips that and uses default from component props definition 😬

Proposed Solution

We can keep the defu's default behavior and introduce option to consider nullish value while merging like below:

defu(obj, defaults, { skipNullish: false })

Additional context

I guess this was already implemented before and we removed this in this PR.

I can use mentioned alternatives but those aren't from unjs family and are outdated (might be vulnerable) which makes defu one and only choice.

Additional information

  • Would you be willing to help implement this feature?

Impossible to `import` in TypeScript project

The package is difficult to import from within TypeScript project.

import defu from 'defu'

Throws:

TypeError: (0 , defu_1.default) is not a function

import * as defu from 'defu'
import defu = require('defu')

Works, but fails TypeScript check:

TS2349: This expression is not callable.
Type 'typeof import("X:/my-project/node_modules/.pnpm/[email protected]/node_modules/defu/dist/defu")' has no call signatures.

const defu = require('defu')

Works, no types, using require.

[Nuxt 2] defu__WEBPACK_IMPORTED_MODULE_3__ is not a function

Environment

Node: v16.19.1

    "dependencies": {
        "@nuxtjs/apollo": "^4.0.1-rc.5",
        "@nuxtjs/auth-next": "^5.0.0-1667386184.dfbbb54",
        "@nuxtjs/axios": "^5.13.6",
        "@nuxtjs/gtm": "^2.4.0",
        "@nuxtjs/sentry": "^5.1.7",
        "@nuxtjs/svg": "^0.4.0",
        "@sentry/tracing": "^6.17.7",
        "body-scroll-lock": "^4.0.0-beta.0",
        "cookie-universal-nuxt": "^2.2.2",
        "elasticsearch": "^16.7.3",
        "elasticsearch-client-retry": "^0.1.0",
        "focus-trap": "^7.2.0",
        "foundation-sites": "^6.7.5",
        "graphql-tag": "^2.12.6",
        "jquery": "^3.6.1",
        "lodash": "^4.17.21",
        "maska": "^2.1.6",
        "md5": "^2.3.0",
        "moment-duration-format": "^2.3.2",
        "nuxt": "^2.15.8",
        "nuxt-jsonld": "v1",
        "nuxt-mq": "^2.0.2",
        "portal-vue": "2.1.7",
        "stopwords-fr": "^0.3.0",
        "uuid": "^9.0.0",
        "v-click-outside": "^3.2.0",
        "vue-adblock-detector": "^1.2.4",
        "vue-infinite-loading": "^2.4.5",
        "vue-priority-plus-navigation": "nguyenj/vue-priority-plus-navigation#master",
        "vue-scrollto": "^2.20.0",
        "vue-slick": "^1.2.0",
        "vue-sticky-directive": "^0.0.10",
        "vue-transition-expand": "^0.1.0",
        "what-input": "^5.2.10"
    },

Reproduction

Capture d’écran, le 2023-03-07 aΜ€ 10 20 18

Capture d’écran, le 2023-03-07 aΜ€ 10 20 08

yarn.lock.txt

Describe the bug

I just ran a yarn upgrade in our Nuxt 2 app the problem seem to come from rc9 which upgraded [email protected]

Additional context

No response

Logs

No response

Export problem

@pi0 : importing into a 'module' type app (in package.json, type:'module') triggers an error

node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ...\node_modules\defu\package.json imported from app.js
    at new NodeError (node:internal/errors:363:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:321:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:510:7)
    at packageResolve (node:internal/modules/esm/resolve:687:14)
    at moduleResolve (node:internal/modules/esm/resolve:739:18)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)
    at link (node:internal/modules/esm/module_job:72:36) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

When a source and a target share a property with different types, it creates union types instead of overwrites with target's type

Environment

defu 6.1.3

Reproduction

live code https://stackblitz.com/edit/typescript-ztqwq7?file=index.ts

import defu from 'defu';

type Source = {
  a: string;
  some: string;
};

type Target = {
  a: number;
};

const source: Source = {
  a: '1',
  some: 'props',
};

const target: Target = {
  a: parseFloat(source.a),
};

// type of merged['a'] is string | number instead of number
const merged = defu(target, source);
console.log(merged);

Describe the bug

I'm not sure if it's a bug or a feature but when both object share the same property, defu merges types instead of overwriting it.

Additional context

No response

Logs

No response

Backward compatible CJS default export

Environment

node v18.17.1
defu v.6.1.2
nuxt 2.17.2

Reproduction

Any Nuxt2 project above version 2.17.1

Describe the bug

With Nuxt v2.17.1 the package webpack-dev-middleware has been pinned to "5.0.0". This has a peer dependency of webpack ^5.0.0.

With webpack5 comes named imports. I can see in version 6.0.0 that you switched to named imports. Apparantly there is still an issue in this package with named imports.

This is the error message I get in the console, when I start a nuxt2 project: defu_WEBPACK_IMPORTED_MODULE_3__ is not a function.

Which means there is still an import type error in the newer version.

Additional context

This seems not to happen however when I start the project in PS, but only through a cmd

Logs

No response

ES5 compliant

Would it be possible the change the arrow function into a simple function in order to avoid having to transpile this module ?

two subset defaults.

interface UploadOptions<T extends StorageList> {
  storeKey?: keyof T
  id?: string
}

interface UrlUploadOptions {
  lazy?: boolean
}

async uploadByUrl(url: string, options?: UploadOptions<typeof this.storages> & UrlUploadOptions): Promise<Image> {
  const opts = defu(options ?? {}, this.defaultUploadOptions, this.defaultUrlUploadOptions)
 // ...
}

Type Error:

TS2559: Type 'UrlUploadOptions' has no properties in common with type 'UploadOptions '.

It's because of the type

<Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]): MergeObjects<Source, Defaults>;

Could we change it to Partial<Defaults>?

Currently I can merge two defaults into one to apply.

const opts = defu(options ?? {}, { ...this.defaultUploadOptions, ...this.defaultUrlUploadOptions })

but, it needs merging before applying. or call defu twice... sounds stupid.

Defu is not a function

The nuxt build keeps failing when rc9 is upgraded to 1.2.3, with error Defu is not a function.

Should I use "rc9": 1.2.2 to resolve the issue?
Since 1.2.2 depends on "defu": 6.1.0 instead of "defu": 6.1.1 that causing the build failure?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.