Skip to content

Commit 6085280

Browse files
aduh95BethGriggs
authored andcommittedSep 21, 2021
lib: avoid creating a throw away object in validateObject
PR-URL: #39807 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 9a67296 commit 6085280

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed
 

‎lib/internal/validators.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,21 @@ function validateBoolean(value, name) {
140140
throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
141141
}
142142

143+
/**
144+
* @param {unknown} value
145+
* @param {string} name
146+
* @param {{
147+
* allowArray?: boolean,
148+
* allowFunction?: boolean,
149+
* nullable?: boolean
150+
* }} [options]
151+
*/
143152
const validateObject = hideStackFrames(
144-
(value, name, {
145-
nullable = false,
146-
allowArray = false,
147-
allowFunction = false,
148-
} = {}) => {
153+
(value, name, options) => {
154+
const useDefaultOptions = options == null;
155+
const allowArray = useDefaultOptions ? false : options.allowArray;
156+
const allowFunction = useDefaultOptions ? false : options.allowFunction;
157+
const nullable = useDefaultOptions ? false : options.nullable;
149158
if ((!nullable && value === null) ||
150159
(!allowArray && ArrayIsArray(value)) ||
151160
(typeof value !== 'object' && (

0 commit comments

Comments
 (0)
Please sign in to comment.