Skip to content

Commit e922a16

Browse files
committedJan 5, 2024
refactor: move isPlainObject to _utils to allow testing
1 parent 8634955 commit e922a16

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed
 

Diff for: ‎src/_utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// From sindresorhus/is-plain-obj
2+
// MIT License
3+
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4+
export function isPlainObject(value: unknown): boolean {
5+
if (value === null || typeof value !== "object") {
6+
return false;
7+
}
8+
const prototype = Object.getPrototypeOf(value);
9+
return (
10+
(prototype === null ||
11+
prototype === Object.prototype ||
12+
Object.getPrototypeOf(prototype) === null) &&
13+
!(Symbol.toStringTag in value) &&
14+
!(Symbol.iterator in value)
15+
);
16+
}

Diff for: ‎src/defu.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isPlainObject } from "./_utils";
12
import type { Merger, DefuFn as DefuFunction, DefuInstance } from "./types";
23

34
// Base function to apply defaults
@@ -7,7 +8,7 @@ function _defu<T>(
78
namespace = ".",
89
merger?: Merger,
910
): T {
10-
if (!_isPlainObject(defaults)) {
11+
if (!isPlainObject(defaults)) {
1112
return _defu(baseObject, {}, namespace, merger);
1213
}
1314

@@ -30,7 +31,7 @@ function _defu<T>(
3031

3132
if (Array.isArray(value) && Array.isArray(object[key])) {
3233
object[key] = [...value, ...object[key]];
33-
} else if (_isPlainObject(value) && _isPlainObject(object[key])) {
34+
} else if (isPlainObject(value) && isPlainObject(object[key])) {
3435
object[key] = _defu(
3536
value,
3637
object[key],
@@ -45,23 +46,6 @@ function _defu<T>(
4546
return object;
4647
}
4748

48-
// From sindresorhus/is-plain-obj
49-
// MIT License
50-
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
51-
function _isPlainObject(value: unknown): boolean {
52-
if (value === null || typeof value !== "object") {
53-
return false;
54-
}
55-
const prototype = Object.getPrototypeOf(value);
56-
return (
57-
(prototype === null ||
58-
prototype === Object.prototype ||
59-
Object.getPrototypeOf(prototype) === null) &&
60-
!(Symbol.toStringTag in value) &&
61-
!(Symbol.iterator in value)
62-
);
63-
}
64-
6549
// Create defu wrapper with optional merger and multi arg support
6650
export function createDefu(merger?: Merger): DefuFunction {
6751
return (...arguments_) =>

0 commit comments

Comments
 (0)
Please sign in to comment.