Skip to content

Instantly share code, notes, and snippets.

@TheWass
Forked from NielsLeenheer/console.hex.js
Created June 11, 2020 20:44
Show Gist options
  • Save TheWass/6325f5dc2b867027879ef826a8cf3d55 to your computer and use it in GitHub Desktop.
Save TheWass/6325f5dc2b867027879ef826a8cf3d55 to your computer and use it in GitHub Desktop.
console.hex
/* eslint-disable */
// @ts-nocheck
interface Console {
hex(data: any): void;
}
console.hex = (d) => console.log((Object(d).buffer instanceof ArrayBuffer ? new Uint8Array(d.buffer) :
typeof d === 'string' ? (new TextEncoder('utf-8')).encode(d) :
new Uint8ClampedArray(d)).reduce((p, c, i, a) => p + (i % 16 === 0 ? i.toString(16).padStart(6, 0) + ' ' : ' ') +
c.toString(16).padStart(2, 0) + (i === a.length - 1 || i % 16 === 15 ?
' '.repeat((15 - i % 16) * 3) + Array.from(a).splice(i - i % 16, 16).reduce((r, v) =>
r + (v > 31 && v < 127 || v > 159 ? String.fromCharCode(v) : '.'), ' ') + '\n' : ''), ''));
@TheWass
Copy link
Author

TheWass commented Jun 11, 2020

To use in TS land:
import './console.hex';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment