Skip to content

Commit

Permalink
Export Inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jan 22, 2021
1 parent f1ab96e commit 7659e89
Showing 1 changed file with 101 additions and 102 deletions.
203 changes: 101 additions & 102 deletions Inspect.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

}

0 comments on commit 7659e89

Please sign in to comment.