util.js 3.4 KB

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