Last active
November 26, 2017 21:16
-
-
Save claudiopro/bd972c5d27dbd7ebd4cb3adf7fee010a to your computer and use it in GitHub Desktop.
console.hex polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) by Niels Leenheer | |
// https://twitter.com/html5test/status/934879316080517121 | |
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" | |
: ""), | |
"" | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment