{"id":13457637,"url":"https://github.com/unjs/defu","last_synced_at":"2025-05-13T00:14:07.359Z","repository":{"id":38094706,"uuid":"169533180","full_name":"unjs/defu","owner":"unjs","description":"🌊 Assign default properties recursively","archived":false,"fork":false,"pushed_at":"2025-05-08T14:05:43.000Z","size":1077,"stargazers_count":1183,"open_issues_count":21,"forks_count":25,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-13T00:14:00.905Z","etag":null,"topics":["defaults","defaultsdeep","node","npm","yarn"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-02-07T07:16:32.000Z","updated_at":"2025-05-09T16:46:27.000Z","dependencies_parsed_at":"2023-12-20T07:24:45.480Z","dependency_job_id":"2540916d-3ddb-449a-a0c9-a896de6ad18e","html_url":"https://github.com/unjs/defu","commit_stats":{"total_commits":158,"total_committers":15,"mean_commits":"10.533333333333333","dds":0.7088607594936709,"last_synced_commit":"70cffe5bd32b6ef510ae129f9a1faa66df633b46"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fdefu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fdefu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fdefu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fdefu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unjs","download_url":"https://codeload.github.com/unjs/defu/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843225,"owners_count":21972874,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["defaults","defaultsdeep","node","npm","yarn"],"created_at":"2024-07-31T09:00:32.497Z","updated_at":"2025-05-13T00:14:07.336Z","avatar_url":"https://github.com/unjs.png","language":"TypeScript","readme":"# 🌊 defu\n\nAssign default properties, recursively. Lightweight and Fast.\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![Codecov][codecov-src]][codecov-href]\n[![License][license-src]][license-href]\n\n## Install\n\nInstall package:\n\n```bash\n# yarn\nyarn add defu\n# npm\nnpm install defu\n# pnpm\npnpm install defu\n```\n\n## Usage\n\n```js\nimport { defu } from \"defu\";\n\nconst options = defu(object, ...defaults);\n```\n\nLeftmost arguments have more priority when assigning defaults.\n\n### Arguments\n\n- **object (Object):** The destination object.\n- **source (Object):** The source object.\n\n```js\nimport { defu } from \"defu\";\n\nconsole.log(defu({ a: { b: 2 } }, { a: { b: 1, c: 3 } }));\n// =\u003e { a: { b: 2, c: 3 } }\n```\n\n### Using with CommonJS\n\n```js\nconst { defu } = require(\"defu\");\n```\n\n## Custom Merger\n\nSometimes default merging strategy is not desirable. Using `createDefu` we can create a custom instance with different merging strategy.\n\nThis function accepts `obj` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.\n\n**Example:** Sum numbers instead of overriding\n\n```js\nimport { createDefu } from \"defu\";\n\nconst ext = createDefu((obj, key, value) =\u003e {\n  if (typeof obj[key] === \"number\" \u0026\u0026 typeof value === \"number\") {\n    obj[key] += value;\n    return true;\n  }\n});\n\next({ cost: 15 }, { cost: 10 }); // { cost: 25 }\n```\n\n## Function Merger\n\nUsing `defuFn`, if user provided a function, it will be called with default value instead of merging.\n\nIt can be useful for default values manipulation.\n\n**Example:** Filter some items from defaults (array) and add 20 to the count default value.\n\n```js\nimport { defuFn } from \"defu\";\n\ndefuFn(\n  {\n    ignore: (val) =\u003e val.filter((item) =\u003e item !== \"dist\"),\n    count: (count) =\u003e count + 20,\n  },\n  {\n    ignore: [\"node_modules\", \"dist\"],\n    count: 10,\n  },\n);\n/*\n {\n    ignore: ['node_modules'],\n    count: 30\n  }\n  */\n```\n\n**Note:** if the default value is not defined, the function defined won't be called and kept as value.\n\n## Array Function Merger\n\n`defuArrayFn` is similar to `defuFn` but **only applies to array values defined in defaults**.\n\n**Example:** Filter some items from defaults (array) and add 20 to the count default value.\n\n```js\nimport { defuArrayFn } from 'defu'\n\ndefuArrayFn({\n  ignore: (val) =\u003e val.filter(i =\u003e i !== 'dist'),\n  count: () =\u003e 20\n }, {\n   ignore: [\n     'node_modules',\n     'dist'\n   ],\n   count: 10\n })\n /*\n  {\n    ignore: ['node_modules'],\n    count: () =\u003e 20\n  }\n  */\n```\n\n**Note:** the function is called only if the value defined in defaults is an aray.\n\n### Remarks\n\n- `object` and `defaults` are not modified\n- Nullish values ([`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) and [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)) are skipped. Please use [defaults-deep](https://www.npmjs.com/package/defaults-deep) or [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to preserve or different behavior.\n- Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.\n- Will concat `array` values (if default property is defined)\n\n```js\nconsole.log(defu({ array: [\"b\", \"c\"] }, { array: [\"a\"] }));\n// =\u003e { array: ['b', 'c', 'a'] }\n```\n\n## Type\n\nWe expose `Defu` as a type utility to return a merged type that follows the rules that defu follows.\n\n```js\nimport type { Defu } from 'defu'\n\ntype Options = Defu\u003c{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]\u003e\n// returns { foo: 'bar', bar: 'baz', 'something': 42 }\n```\n\n## License\n\nMIT. Made with 💖\n\n\u003c!-- Refs --\u003e\n\n[npm-version-src]: https://img.shields.io/npm/v/defu?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[npm-version-href]: https://npmjs.com/package/defu\n[npm-downloads-src]: https://img.shields.io/npm/dm/defu?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[npm-downloads-href]: https://npmjs.com/package/defu\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/defu/main?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[codecov-href]: https://codecov.io/gh/unjs/defu\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/defu?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[bundle-href]: https://bundlephobia.com/result?p=defu\n[license-src]: https://img.shields.io/github/license/unjs/defu.svg?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[license-href]: https://github.com/unjs/defu/blob/main/LICENSE\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funjs%2Fdefu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funjs%2Fdefu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funjs%2Fdefu/lists"}