From 7659e891176d274cd9a5e3f0dcf158704c228016 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Fri, 22 Jan 2021 17:41:52 +0800 Subject: [PATCH] Export Inspect --- Inspect.ts | 203 ++++++++++++++++++++++++++--------------------------- 1 file changed, 101 insertions(+), 102 deletions(-) diff --git a/Inspect.ts b/Inspect.ts index 4a0e810..5f8b2f9 100644 --- a/Inspect.ts +++ b/Inspect.ts @@ -1,126 +1,125 @@ import { ensureDirSync } from "https://deno.land/std@0.84.0/fs/mod.ts"; import * as path from "https://deno.land/std@0.84.0/path/mod.ts"; - - class Inspect { + +export class Inspect { static vars(obj: { [id: string]: any }) { - let inspectVarsPath = Deno.env.get('INSPECT_VARS'); - if (!inspectVarsPath || obj == null) { - return; - } - - let json = JSON.stringify(obj); - let varsPath = inspectVarsPath.replaceAll('\\', '/'); - if (varsPath.indexOf('/') >= 0) { - let dir = path.dirname(varsPath); - ensureDirSync(dir); - } - Deno.writeTextFileSync(varsPath, json); + let inspectVarsPath = Deno.env.get('INSPECT_VARS'); + if (!inspectVarsPath || obj == null) { + return; + } + + let json = JSON.stringify(obj); + let varsPath = inspectVarsPath.replaceAll('\\', '/'); + if (varsPath.indexOf('/') >= 0) { + let dir = path.dirname(varsPath); + ensureDirSync(dir); + } + Deno.writeTextFileSync(varsPath, json); } - + static dump(obj: any) { - let to = JSON.stringify(obj, null, 4); - return to.replace(/"/g, ''); + let to = JSON.stringify(obj, null, 4); + return to.replace(/"/g, ''); } - + static printDump(obj: any) { - console.log(Inspect.dump(obj)); + console.log(Inspect.dump(obj)); } - + static dumpTable(rows: any[]): String { - let mapRows = rows; - let keys = Inspect._allKeys(mapRows); - let colSizes:{[id:string]:number} = {}; - - keys.forEach(k => { - let max = k.length; - mapRows.forEach(row => { - let col = row[k]; - if (col != null) { - let valSize = `${col}`.length; - if (valSize > max) { - max = valSize; - } - } - }); - colSizes[k] = max; - }); - - let colSizesLength = Object.keys(colSizes).length; - // sum + ' padding ' + | - let rowWidth = Object.values(colSizes).reduce((p:number, c:number) => p + c, 0) + - (colSizesLength * 2) + - (colSizesLength + 1); - let dashes = '-'.repeat(rowWidth - 2); - let sb:string[] = []; - sb.push(`+${dashes}+`); - let head = '|'; - keys.forEach((k) => { - head += Inspect._alignCenter(k, colSizes[k]) + '|'; - }); - sb.push(head); - sb.push(`|${dashes}|`); - - mapRows.forEach(row => { - let to = '|'; + let mapRows = rows; + let keys = Inspect._allKeys(mapRows); + let colSizes: { [id: string]: number } = {}; + keys.forEach(k => { - to += '' + Inspect._alignAuto(row[k], colSizes[k]) + '|'; + let max = k.length; + mapRows.forEach(row => { + let col = row[k]; + if (col != null) { + let valSize = `${col}`.length; + if (valSize > max) { + max = valSize; + } + } + }); + colSizes[k] = max; + }); + + let colSizesLength = Object.keys(colSizes).length; + // sum + ' padding ' + | + let rowWidth = Object.values(colSizes).reduce((p: number, c: number) => p + c, 0) + + (colSizesLength * 2) + + (colSizesLength + 1); + let dashes = '-'.repeat(rowWidth - 2); + let sb: string[] = []; + sb.push(`+${dashes}+`); + let head = '|'; + keys.forEach((k) => { + head += Inspect._alignCenter(k, colSizes[k]) + '|'; }); - sb.push(to); - }); - - sb.push(`+${dashes}+`); - - return sb.join('\n'); + sb.push(head); + sb.push(`|${dashes}|`); + + mapRows.forEach(row => { + let to = '|'; + keys.forEach(k => { + to += '' + Inspect._alignAuto(row[k], colSizes[k]) + '|'; + }); + sb.push(to); + }); + + sb.push(`+${dashes}+`); + + return sb.join('\n'); } - + static printDumpTable(rows: any[]) { - console.log(Inspect.dumpTable(rows)); + console.log(Inspect.dumpTable(rows)); } - + static _allKeys(rows: any[]): string[] { - let to: string[] = []; - rows.forEach(o => Object.keys(o).forEach((key:any) => { - let k = `${key}`; - if (to.indexOf(k) === -1) { - to.push(k); - } - })); - return to; + let to: string[] = []; + rows.forEach(o => Object.keys(o).forEach((key: any) => { + let k = `${key}`; + if (to.indexOf(k) === -1) { + to.push(k); + } + })); + return to; } - - static _alignLeft(str: string, len: number, pad:string = ' '): string { - if (len < 0) return ''; - let aLen = len + 1 - str.length; - if (aLen <= 0) return str; - return pad + str + pad.repeat(len + 1 - str.length); + + static _alignLeft(str: string, len: number, pad: string = ' '): string { + if (len < 0) return ''; + let aLen = len + 1 - str.length; + if (aLen <= 0) return str; + return pad + str + pad.repeat(len + 1 - str.length); } - + static _alignCenter(str: string, len: number, pad: string = ' '): string { - if (len < 0) return ''; - str ??= ''; - let nLen = str.length; - let half = Math.floor(len / 2 - nLen / 2); - let odds = Math.abs((nLen % 2) - (len % 2)); - len = str.length; - return pad.repeat(half + 1) + str + pad.repeat(half + 1 + odds); + if (len < 0) return ''; + str ??= ''; + let nLen = str.length; + let half = Math.floor(len / 2 - nLen / 2); + let odds = Math.abs((nLen % 2) - (len % 2)); + len = str.length; + return pad.repeat(half + 1) + str + pad.repeat(half + 1 + odds); } - + static _alignRight(str: string, len: number, pad: string = ' '): string { - if (len < 0) return ''; - let aLen = len + 1 - str.length; - if (aLen <= 0) return str; - return pad.repeat(len + 1 - str.length) + str + pad; + if (len < 0) return ''; + let aLen = len + 1 - str.length; + if (aLen <= 0) return str; + return pad.repeat(len + 1 - str.length) + str + pad; } - + static _alignAuto(obj: any, len: number, pad: string = ' '): string { - let str = `${obj}`; - if (str.length <= len) { - if (typeof obj == 'number') { - return Inspect._alignRight(str, len, pad); + let str = `${obj}`; + if (str.length <= len) { + if (typeof obj == 'number') { + return Inspect._alignRight(str, len, pad); + } + return Inspect._alignLeft(str, len, pad); } - return Inspect._alignLeft(str, len, pad); - } - return str; + return str; } - } - \ No newline at end of file +}