All files / src/vendor/node util.ts

25.92% Statements 14/54
20.83% Branches 5/24
50% Functions 5/10
29.78% Lines 14/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107        64x 128x     128x     128x 128x                           64x 4x       4x 4x 4x 4x     4x                     64x                                           64x                                                                        
/**
 * Minimal implementation of Node.js util.inherits function.
 * Sets up prototype inheritance between constructor functions.
 */
export function inherits(ctor: any, superCtor: any): void {
  Iif (ctor === undefined || ctor === null) {
    throw new TypeError('The constructor to inherit from is not defined');
  }
  Iif (superCtor === undefined || superCtor === null) {
    throw new TypeError('The super constructor to inherit from is not defined');
  }
  ctor.super_ = superCtor;
  ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
      value: ctor,
      enumerable: false,
      writable: true,
      configurable: true,
    },
  });
}
 
/**
 * Minimal implementation of Node.js util.promisify function.
 * Converts callback-based functions to Promise-based functions.
 */
export function promisify(fn: Function): Function {
  Iif (typeof fn !== 'function') {
    throw new TypeError('The "original" argument must be of type function');
  }
 
  return function (...args: any[]): Promise<any> {
    return new Promise((resolve, reject) => {
      fn.call(this, ...args, (err: any, result: any) => {
        Iif (err) {
          reject(err);
        } else {
          resolve(result);
        }
      });
    });
  };
}
 
/**
 * Minimal implementation of Node.js util.inspect function.
 * Converts a value to a string representation for debugging.
 */
export function inspect(value: any): string {
  Iif (value === null) return 'null';
  Iif (value === undefined) return 'undefined';
  Iif (typeof value === 'string') return `'${value}'`;
  Iif (typeof value === 'number' || typeof value === 'boolean') return String(value);
  Iif (Array.isArray(value)) {
    const items = value.map(item => inspect(item)).join(', ');
    return `[ ${items} ]`;
  }
  Iif (typeof value === 'object') {
    const entries = Object.entries(value)
      .map(([key, val]) => `${key}: ${inspect(val)}`)
      .join(', ');
    return `{ ${entries} }`;
  }
  return String(value);
}
 
/**
 * Minimal implementation of Node.js util.format function.
 * Provides printf-style string formatting with basic placeholder support.
 */
export function format(template: string, ...args: any[]): string {
  Iif (args.length === 0) return template;
 
  let result = template;
  let argIndex = 0;
 
  // Replace %s (string), %d (number), %j (JSON) placeholders
  result = result.replace(/%[sdj%]/g, match => {
    Iif (argIndex >= args.length) return match;
 
    const arg = args[argIndex++];
    switch (match) {
      case '%s':
        return String(arg);
      case '%d':
        return Number(arg).toString();
      case '%j':
        try {
          return JSON.stringify(arg);
        } catch {
          return '[Circular]';
        }
      case '%%':
        return '%';
      default:
        return match;
    }
  });
 
  // Append remaining arguments
  while (argIndex < args.length) {
    result += ' ' + String(args[argIndex++]);
  }
 
  return result;
}