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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 76 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,59 +73,109 @@ const {

let internalDeepEqual;

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @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
* @param {any} arg
* @returns {arg is null}
*/
function isNull(arg) {
return arg === null;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @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
* @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
* @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
* @param {any} arg
* @returns {arg is undefined}
*/
function isUndefined(arg) {
return arg === undefined;
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @param {any} arg
* @returns {boolean}
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
*/
function isObject(arg) {
return arg !== null && typeof arg === 'object';
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @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
* @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
* @param {any} arg
* @returns {arg is (null | boolean | string | number | symbol)}
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
*/
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 +187,10 @@ 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
* @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 +207,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 +233,13 @@ function inherits(ctor, superCtor) {
ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
}

/**
rohit-gohri marked this conversation as resolved.
Show resolved Hide resolved
* @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 +263,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 +305,10 @@ function callbackify(original) {
return callbackified;
}

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