diff --git a/src/_utils.ts b/src/_utils.ts new file mode 100644 index 0000000..52ac3ed --- /dev/null +++ b/src/_utils.ts @@ -0,0 +1,16 @@ +// From sindresorhus/is-plain-obj +// MIT License +// Copyright (c) Sindre Sorhus (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) + ); +} diff --git a/src/defu.ts b/src/defu.ts index e942aec..271c023 100644 --- a/src/defu.ts +++ b/src/defu.ts @@ -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( namespace = ".", merger?: Merger, ): T { - if (!_isPlainObject(defaults)) { + if (!isPlainObject(defaults)) { return _defu(baseObject, {}, namespace, merger); } @@ -30,7 +31,7 @@ function _defu( 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( return object; } -// From sindresorhus/is-plain-obj -// MIT License -// Copyright (c) Sindre Sorhus (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_) =>