diff --git a/lib/internal/url.js b/lib/internal/url.js index 5855a47ea470b1..b874aceb1413a8 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -5,6 +5,7 @@ const { hexTable, isHexTable } = require('internal/querystring'); +const { getConstructorOf } = require('internal/util'); const binding = process.binding('url'); const context = Symbol('context'); const cannotBeBase = Symbol('cannot-be-base'); @@ -184,17 +185,6 @@ function onParseHashComplete(flags, protocol, username, password, } } -function getEligibleConstructor(obj) { - while (obj !== null) { - if (Object.prototype.hasOwnProperty.call(obj, 'constructor') && - typeof obj.constructor === 'function') { - return obj.constructor; - } - obj = Object.getPrototypeOf(obj); - } - return null; -} - class URL { constructor(input, base) { // toUSVString is not needed. @@ -225,7 +215,7 @@ class URL { if (typeof depth === 'number' && depth < 0) return opts.stylize('[Object]', 'special'); - const ctor = getEligibleConstructor(this); + const ctor = getConstructorOf(this); const obj = Object.create({ constructor: ctor === null ? URL : ctor diff --git a/lib/internal/util.js b/lib/internal/util.js index 0a5d4d1f18dac9..60b15b74797bda 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -187,3 +187,18 @@ exports.convertToValidSignal = function convertToValidSignal(signal) { throw new Error('Unknown signal: ' + signal); }; + +exports.getConstructorOf = function getConstructorOf(obj) { + while (obj) { + var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor'); + if (descriptor !== undefined && + typeof descriptor.value === 'function' && + descriptor.value.name !== '') { + return descriptor.value; + } + + obj = Object.getPrototypeOf(obj); + } + + return null; +}; diff --git a/lib/util.js b/lib/util.js index 2631f0a2c3e1a1..ddf43a760c2cc9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -278,22 +278,6 @@ function arrayToHash(array) { } -function getConstructorOf(obj) { - while (obj) { - var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor'); - if (descriptor !== undefined && - typeof descriptor.value === 'function' && - descriptor.value.name !== '') { - return descriptor.value; - } - - obj = Object.getPrototypeOf(obj); - } - - return null; -} - - function ensureDebugIsInitialized() { if (Debug === undefined) { const runInDebugContext = require('vm').runInDebugContext; @@ -410,7 +394,7 @@ function formatValue(ctx, value, recurseTimes) { }); } - var constructor = getConstructorOf(value); + var constructor = internalUtil.getConstructorOf(value); // Some type of object without properties can be shortcutted. if (keys.length === 0) {