UNPKG

@nestjs-kitchen/connextion-presto

Version:

A flexible module to provide presto-client interface in NextJS.

73 lines (72 loc) 2.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withResolvers = exports.noop = exports.buildDataRows = exports.createDebugLogger = exports.getCurrentDateStr = exports.printTable = exports.truncateString = void 0; const dayjs_1 = __importDefault(require("dayjs")); const constants_1 = require("./constants"); const truncateString = (str, maxLength) => { if (str.length <= maxLength) { return str; } const truncatedLength = maxLength - 3; if (truncatedLength <= 0) { return '...'; } return str.slice(0, truncatedLength) + '...'; }; exports.truncateString = truncateString; const printTable = (logData) => { const maxKeyLength = constants_1.MAX_LENGTH / 3; const maxValueLength = constants_1.MAX_LENGTH; const separatorTop = '─'.repeat(maxKeyLength + 2) + '┬' + '─'.repeat(maxValueLength + 2); const separatorBottom = '─'.repeat(maxKeyLength + 2) + '┴' + '─'.repeat(maxValueLength + 2); const lines = [ `┌${separatorTop}┐`, ...Object.entries(logData) .filter(([_, value]) => value) .map(([key, value]) => `│ ${key.padEnd(maxKeyLength)}${(0, exports.truncateString)(String(value), constants_1.MAX_LENGTH).padEnd(maxValueLength)} │`), `└${separatorBottom}┘` ]; return lines.join('\n'); }; exports.printTable = printTable; const getCurrentDateStr = () => { return (0, dayjs_1.default)().format(constants_1.DATE_FORMAT); }; exports.getCurrentDateStr = getCurrentDateStr; const createDebugLogger = (defaultLogger, customFormater) => { if (typeof customFormater === 'function') { return (data) => { defaultLogger(customFormater(data)); }; } return (data) => { const firstLine = `Executing Presto command\n`; defaultLogger(firstLine + (0, exports.printTable)(data)); }; }; exports.createDebugLogger = createDebugLogger; const buildDataRows = (columns, data) => { return data.map((item) => { const row = {}; for (const [index, column] of columns.entries()) { row[column.name] = item[index]; } return row; }); }; exports.buildDataRows = buildDataRows; const noop = (..._) => { }; exports.noop = noop; const withResolvers = () => { let resolve; let reject; const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); return { promise, resolve: resolve, reject: reject }; }; exports.withResolvers = withResolvers;