UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

26 lines (22 loc) 690 B
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * 获取 Node.js 的 util 模块 * 这样可以避免在浏览器环境中直接导入 util 模块导致的错误 * @returns util 模块 * @example * ```ts * const util = getUtil(); * console.log(util.format('%s + %s = %s', 1, 2, 3)); // '1 + 2 = 3' * * const promisify = util.promisify(fs.readFile); * const data = await promisify('./a.txt', 'utf-8'); * ``` */ function getUtil() { // 在浏览器环境中,这个函数不应该被调用 // 如果被调用,会抛出错误 // 在 Node.js 环境中,会正常返回 util 模块 return require('util'); } exports.getUtil = getUtil;