Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add JSDoc typings for util #38213

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 88 additions & 4 deletions lib/util.js
Expand Up @@ -73,59 +73,119 @@ const {

let internalDeepEqual;

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is boolean}
*/
function isBoolean(arg) {
return typeof arg === 'boolean';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is null}
*/
function isNull(arg) {
return arg === null;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is (null | undefined)}
*/
function isNullOrUndefined(arg) {
return arg === null || arg === undefined;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is number}
*/
function isNumber(arg) {
return typeof arg === 'number';
}

/**
* @param {any} arg
* @returns {arg is string}
*/
function isString(arg) {
return typeof arg === 'string';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is symbol}
*/
function isSymbol(arg) {
return typeof arg === 'symbol';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is undefined}
*/
function isUndefined(arg) {
return arg === undefined;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {a is NonNullable<object>}
*/
function isObject(arg) {
return arg !== null && typeof arg === 'object';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} e
* @returns {arg is Error}
*/
function isError(e) {
return ObjectPrototypeToString(e) === '[object Error]' || e instanceof Error;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is Function}
*/
function isFunction(arg) {
return typeof arg === 'function';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v4.0.0
* @param {any} arg
* @returns {arg is (boolean | null | number | string | symbol | undefined)}
*/
function isPrimitive(arg) {
return arg === null ||
(typeof arg !== 'object' && typeof arg !== 'function');
}

/**
* @param {number} n
* @returns {string}
*/
function pad(n) {
return StringPrototypePadStart(n.toString(), 2, '0');
}

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];

// 26 Feb 16:19:34
/**
* @returns {string} 26 Feb 16:19:34
*/
function timestamp() {
const d = new Date();
const t = ArrayPrototypeJoin([
Expand All @@ -137,7 +197,11 @@ function timestamp() {
}

let console;
// Log is just a thin wrapper to console.log that prepends a timestamp
/**
* Log is just a thin wrapper to console.log that prepends a timestamp
* @deprecated since v6.0.0
* @type {(...args: any[]) => void}
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
*/
function log(...args) {
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
if (!console) {
console = require('internal/console/global');
Expand All @@ -154,9 +218,9 @@ function log(...args) {
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* @param {Function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
* @param {Function} superCtor Constructor function to inherit prototype from.
* @throws {TypeError} Will error if either constructor is null, or if
* the super constructor lacks a prototype.
*/
Expand All @@ -180,6 +244,14 @@ function inherits(ctor, superCtor) {
ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @deprecated since v6.0.0
* @template T
* @template S
* @param {T} target
* @param {S} source
* @returns {S extends null ? T : (T & S)}
*/
function _extend(target, source) {
// Don't do anything if source isn't an object
if (source === null || typeof source !== 'object') return target;
Expand All @@ -203,6 +275,14 @@ const callbackifyOnRejected = hideStackFrames((reason, cb) => {
return cb(reason);
});

/**
* @template {(...args: any[]) => Promise<any>} T
* @param {T} original
* @returns {T extends (...args: infer TArgs) => Promise<infer TReturn> ?
* ((...params: [...TArgs, ((err: Error, ret: TReturn) => any)]) => void) :
* never
* }
*/
function callbackify(original) {
if (typeof original !== 'function') {
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
Expand Down Expand Up @@ -237,6 +317,10 @@ function callbackify(original) {
return callbackified;
}

/**
* @param {number} err
* @returns {string}
*/
function getSystemErrorName(err) {
validateNumber(err, 'err');
if (err >= 0 || !NumberIsSafeInteger(err)) {
Expand Down