unjs / defu Goto Github PK
View Code? Open in Web Editor NEWπ Assign default properties recursively
License: MIT License
π Assign default properties recursively
License: MIT License
defu
development environment
.
// 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
.
.
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?
When passing in a new object where some of the properties are null
, the defaults are used instead. I would expect this only be true for undefined
properties.
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: -
https://stackblitz.com/edit/github-bcxfrv?file=README.md
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 } }
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.
No response
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 } }
Node.js: v18.12.1
defu: 6.1.2
https://github.com/dword-design/demo-defu-array-merge
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.
No response
No response
this module is not es5
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
@types/node
, @vitest/coverage-v8
, eslint
, expect-type
, pnpm
, prettier
, typescript
, vitest
).github/workflows/ci.yml
actions/checkout v4
actions/setup-node v4
codecov/codecov-action v3
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
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.
I'd expect the array values from config to be appended to values from base/defaults. Currently it's the other way around.
No response
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.
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:
yarn add install @nuxtjs/axios
'@nuxtjs/axios'
to modules
in nuxt.config.jsyarn dev
or yarn build && yarn start
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).
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)
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
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
Rollback your 6.1.1 to 6.1.0 as it contains breaking changes and release it as a major release
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.
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
π
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 π¬
We can keep the defu's default behavior and introduce option to consider nullish value while merging like below:
defu(obj, defaults, { skipNullish: false })
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.
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
.
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"
},
I just ran a yarn upgrade
in our Nuxt 2 app the problem seem to come from rc9
which upgraded [email protected]
No response
No response
5MB (install size) vs < 1 kB (bundle size) make a huge difference π¦ π
@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'
}
Hi there i guess it's related to https://github.com/nuxt-community/axios-module/commit/a9e4867587cd60a25aca441159938495d41755c9
defu 6.1.3
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);
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.
No response
No response
node v18.17.1
defu v.6.1.2
nuxt 2.17.2
Any Nuxt2 project above version 2.17.1
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.
This seems not to happen however when I start the project in PS, but only through a cmd
No response
Would it be possible the change the arrow function into a simple function in order to avoid having to transpile this module ?
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.
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.