UNPKG

@oada/cli

Version:

CLI OADA client

72 lines 2.08 kB
import KSUID from 'ksuid'; import Command from '../../BaseCommand.js'; import getConn from '../../connections.js'; import { input } from '../../io.js'; function humanize(value) { if (!value) { return value; } if (typeof value === 'object') { const out = new Map(); for (const [k, vValue] of Object.entries(value)) { const v = humanize(vValue); let t = false; for (const transform of transforms) { if (transform.match({ k, v })) { const { k: kk, v: vv } = transform.apply({ k, v }); out.set(kk, vv); t = true; } } if (!t) { out.set(k, v); } } return out; } return value; } const transforms = [ { match({ k }) { return typeof k === 'string' && k.length === 27; }, apply({ k, v }) { const { date, payload } = KSUID.parse(k); return { k: { date: date.toLocaleString(), payload: payload.toString('hex') }, v, }; }, }, { match: ({ k, v }) => k === 'modified' && typeof v === 'number', apply({ k, v }) { const d = new Date(v * 1000); return { k, v: d.toLocaleString(), }; }, }, ]; class Humanize extends Command { async run() { const { argv: paths } = await this.parse(Humanize); const conn = getConn(this.iconfig); for (const file of paths) { await input(conn, `${file}`, this.iconfig, async function* (source) { for await (const data of source) { const out = humanize(data); console.dir(out, { depth: null, }); } }); } } } Humanize.aliases = ['humanize']; Humanize.strict = false; export default Humanize; //# sourceMappingURL=humanize.js.map