Skip to content

Commit

Permalink
feat!: use named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Use named exports:
- `import defu from 'defu'` => `import { defu } from 'defu'`
- `defu.fn` => `import { defuFn }`
- `defu.arrayFn` => `import { defuArrayFn }`
  • Loading branch information
pi0 committed Mar 21, 2022
1 parent f6df314 commit 4a8fc52
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
25 changes: 17 additions & 8 deletions README.md
Expand Up @@ -18,15 +18,20 @@
Install package:

```bash
# yarn
yarn add defu
# or
# npm
npm install defu
# pnpm
pnpm install defu
```

## Usage

```js
const options = defu (object, ...defaults)
import { defu } from 'defu'

const options = defu(object, ...defaults)
```

Leftmost arguments have more priority when assigning defaults.
Expand All @@ -45,14 +50,16 @@ console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))

## Custom Merger

Sometimes default merging strategy is not desirable. Using `defu.extend` we can create a custom instance with different merging strategy.
Sometimes default merging strategy is not desirable. Using `createDefu` we can create a custom instance with different merging strategy.

This function accepts `obj` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.

**Example:** Sum numbers instead of overriding

```js
const ext = defu.extend((obj, key, value) => {
import { createDefu } from 'defu'

const ext = createDefu((obj, key, value) => {
if (typeof obj[key] === 'number' && typeof value === 'number') {
obj[key] += val
return true
Expand All @@ -64,15 +71,16 @@ ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }

## Function Merger

Using `defu.fn`, if user provided a function, it will be called with default value instead of merging.
Using `defuFn`, if user provided a function, it will be called with default value instead of merging.

I can be useful for default values manipulation.

**Example:** Filter some items from defaults (array) and add 20 to the count default value.

```js
import { defuFn } from 'defu'

defu.fn({
defuFn({
ignore: (val) => val.filter(item => item !== 'dist'),
count: (count) => count + 20
}, {
Expand All @@ -91,13 +99,14 @@ defu.fn({

## Array Function Merger

`defu.arrayFn` is similar to `defu.fn` but **only applies to array values defined in defaults**.
`defuArrayFn` is similar to `defuFn` but **only applies to array values defined in defaults**.

**Example:** Filter some items from defaults (array) and add 20 to the count default value.

```js
import { defuArrayFn } from 'defu'

defu.arrayFn({
defuArrayFn({
ignore(val) => val.filter(i => i !== 'dist'),
count: () => 20
}, {
Expand Down
4 changes: 0 additions & 4 deletions lib/defu.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions lib/defu.mjs

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -7,15 +7,14 @@
"exports": {
".": {
"require": "./dist/defu.cjs",
"import": "./lib/defu.mjs"
"import": "./dist/defu.mjs"
}
},
"main": "./dist/defu.cjs",
"module": "./lib/defu.mjs",
"types": "./lib/defu.d.ts",
"module": "./dist/defu.mjs",
"types": "./dist/defu.d.ts",
"files": [
"dist",
"lib"
"dist"
],
"scripts": {
"build": "unbuild",
Expand Down
16 changes: 6 additions & 10 deletions src/defu.ts
Expand Up @@ -40,30 +40,26 @@ function _defu<T> (baseObj: T, defaults: any, namespace: string = '.', merger?:
}

// Create defu wrapper with optional merger and multi arg support
function extend (merger?: Merger): DefuFn {
export function createDefu (merger?: Merger): DefuFn {
return (...args) => args.reduce((p, c) => _defu(p, c, '', merger), {} as any)
}

// Basic version
const defu = extend() as Defu
// Standard version
export const defu = createDefu() as Defu
export default defu

// Custom version with function merge support
defu.fn = extend((obj, key, currentValue, _namespace) => {
export const defuFn = createDefu((obj, key, currentValue, _namespace) => {
if (typeof obj[key] !== 'undefined' && typeof currentValue === 'function') {
obj[key] = currentValue(obj[key])
return true
}
})

// Custom version with function merge support only for defined arrays
defu.arrayFn = extend((obj, key, currentValue, _namespace) => {
export const defuArrayFn = createDefu((obj, key, currentValue, _namespace) => {
if (Array.isArray(obj[key]) && typeof currentValue === 'function') {
obj[key] = currentValue(obj[key])
return true
}
})

// Support user extending
defu.extend = extend

export default defu
14 changes: 7 additions & 7 deletions test/defu.test.ts
@@ -1,6 +1,6 @@
import { expectTypeOf } from 'expect-type'
import { it, describe, expect } from 'vitest'
import defu from '../src/defu'
import { defu, createDefu, defuFn, defuArrayFn } from '../src/defu'

// Part of tests brought from jonschlinkert/defaults-deep (MIT)

Expand Down Expand Up @@ -100,7 +100,7 @@ describe('defu', () => {
})

it('custom merger', () => {
const ext = defu.extend((obj, key, val) => {
const ext = createDefu((obj, key, val) => {
if (typeof val === 'number') {
;(obj as any)[key] += val
return true
Expand All @@ -109,10 +109,10 @@ describe('defu', () => {
expect(ext({ cost: 15 }, { cost: 10 })).toEqual({ cost: 25 })
})

it('defu.fn()', () => {
it('defuFn()', () => {
const num = () => 20
expect(
defu.fn(
defuFn(
{
ignore: val => val.filter(i => i !== 'dist'),
num,
Expand All @@ -130,9 +130,9 @@ describe('defu', () => {
})
})

it('defu.arrayFn()', () => {
it('defuArrayFn()', () => {
const num = () => 20
expect(defu.arrayFn({
expect(defuArrayFn({
arr: () => ['c'],
num
}, {
Expand All @@ -145,7 +145,7 @@ describe('defu', () => {
})

it('custom merger with namespace', () => {
const ext = defu.extend((obj, key, val, namespace) => {
const ext = createDefu((obj, key, val, namespace) => {
// console.log({ obj, key, val, namespace })
if (key === 'modules') {
// TODO: It is not possible to override types with extend()
Expand Down

0 comments on commit 4a8fc52

Please sign in to comment.