-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move
isPlainObject
to _utils
to allow testing
- Loading branch information
Showing
2 changed files
with
19 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// From sindresorhus/is-plain-obj | ||
// MIT License | ||
// Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com) | ||
export function isPlainObject(value: unknown): boolean { | ||
if (value === null || typeof value !== "object") { | ||
return false; | ||
} | ||
const prototype = Object.getPrototypeOf(value); | ||
return ( | ||
(prototype === null || | ||
prototype === Object.prototype || | ||
Object.getPrototypeOf(prototype) === null) && | ||
!(Symbol.toStringTag in value) && | ||
!(Symbol.iterator in value) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { isPlainObject } from "./_utils"; | ||
import type { Merger, DefuFn as DefuFunction, DefuInstance } from "./types"; | ||
|
||
// Base function to apply defaults | ||
|
@@ -7,7 +8,7 @@ function _defu<T>( | |
namespace = ".", | ||
merger?: Merger, | ||
): T { | ||
if (!_isPlainObject(defaults)) { | ||
if (!isPlainObject(defaults)) { | ||
return _defu(baseObject, {}, namespace, merger); | ||
} | ||
|
||
|
@@ -30,7 +31,7 @@ function _defu<T>( | |
|
||
if (Array.isArray(value) && Array.isArray(object[key])) { | ||
object[key] = [...value, ...object[key]]; | ||
} else if (_isPlainObject(value) && _isPlainObject(object[key])) { | ||
} else if (isPlainObject(value) && isPlainObject(object[key])) { | ||
object[key] = _defu( | ||
value, | ||
object[key], | ||
|
@@ -45,23 +46,6 @@ function _defu<T>( | |
return object; | ||
} | ||
|
||
// From sindresorhus/is-plain-obj | ||
// MIT License | ||
// Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com) | ||
function _isPlainObject(value: unknown): boolean { | ||
if (value === null || typeof value !== "object") { | ||
return false; | ||
} | ||
const prototype = Object.getPrototypeOf(value); | ||
return ( | ||
(prototype === null || | ||
prototype === Object.prototype || | ||
Object.getPrototypeOf(prototype) === null) && | ||
!(Symbol.toStringTag in value) && | ||
!(Symbol.iterator in value) | ||
); | ||
} | ||
|
||
// Create defu wrapper with optional merger and multi arg support | ||
export function createDefu(merger?: Merger): DefuFunction { | ||
return (...arguments_) => | ||
|