Skip to content

Commit

Permalink
refactor: move isPlainObject to _utils to allow testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 5, 2024
1 parent 8634955 commit e922a16
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
16 changes: 16 additions & 0 deletions src/_utils.ts
@@ -0,0 +1,16 @@
// From sindresorhus/is-plain-obj
// MIT License
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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)
);
}
22 changes: 3 additions & 19 deletions 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
Expand All @@ -7,7 +8,7 @@ function _defu<T>(
namespace = ".",
merger?: Merger,
): T {
if (!_isPlainObject(defaults)) {
if (!isPlainObject(defaults)) {
return _defu(baseObject, {}, namespace, merger);
}

Expand All @@ -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],
Expand All @@ -45,23 +46,6 @@ function _defu<T>(
return object;
}

// From sindresorhus/is-plain-obj
// MIT License
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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_) =>
Expand Down

0 comments on commit e922a16

Please sign in to comment.