diff --git a/README.md b/README.md index 2616402..f2ffb7d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# @root/hexdump +# [@root/hexdump](https://git.rootprojects.org/root/hexdump.js) | Built by [Root](https://rootprojects.org) @@ -12,21 +12,29 @@ For example, the text of "Hello, World!\n" looks something like this: 000000e ``` -## Usage +# Usage So far it just does one thing: print an ArrayBuffer in hex, with a header: -### JavaScript +## Node.js (and WebPack) ```bash -var hexdump = require('@root/hexdump').hexdump; +var hexdump = require('hexdump.js').hexdump; var str = hexdump(new Uint8Array([ 0, 1, 2, 127, 254, 255 ])); console.log(str); ``` -### Browser +## Vanilla JS (Browser) + +```html + +``` + +```html + +``` ```javascript console.log(window.hexdump(new Uint8Array([0, 1, 2, 127, 254, 255]))); diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..ed3dbfd --- /dev/null +++ b/build.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# TODO convert to JS +cat hexdump.js > all.tmp.js +sed -i '' '/use strict/d' all.tmp.js +sed -i '' '/require/d' all.tmp.js +sed -i '' '/exports/d' all.tmp.js +echo ';(function () {' > all.js +echo "'use strict';" >> all.js +cat all.tmp.js >> all.js +rm all.tmp.js +echo "window.hexdump = hexdump;" >> all.js +echo '}());' >> all.js + +mv all.js dist/hexdump.js +uglifyjs dist/hexdump.js > dist/hexdump.min.js diff --git a/dist/hexdump.js b/dist/hexdump.js new file mode 100644 index 0000000..bbb26ed --- /dev/null +++ b/dist/hexdump.js @@ -0,0 +1,70 @@ +;(function () { +'use strict'; + +function hexdump(ab, offset, len, opts) { + if (!opts) { + opts = {}; + } + var ui8 = new Uint8Array(ab.buffer || ab, offset || ab.byteOffset, len || ab.byteLength); + var bytecount = 0; + var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F'; + var trail; + var str = [].slice + .call(ui8) + .map(function(i) { + var h = i.toString(16); + if (h.length < 2) { + h = '0' + h; + } + return h; + }) + .join('') + .match(/.{1,2}/g) + .join(' ') + .match(/.{1,48}/g) + .map(function(str) { + var lead = bytecount.toString(16); + bytecount += 16; + + while (lead.length < 7) { + lead = '0' + lead; + } + + while (str.length < 48) { + str += ' '; + } + + if (opts.C) { + return ( + lead + + ' ' + + str + + ' |' + + str + .replace(/ /g, '') + .match(/.{1,2}/g) + .map(function(ch) { + var c = String.fromCharCode(parseInt(ch, 16)); + if (!/[ -~]/.test(c)) { + c = '.'; + } + return c; + }) + .join('') + + '|' + ); + } else { + return lead + ' ' + str; + } + }) + .join('\n'); + + trail = (len || ab.byteLength).toString(16); + while (trail.length < 7) { + trail = '0' + trail; + } + return head + '\n' + str + '\n' + trail; +} + +window.hexdump = hexdump; +}()); diff --git a/dist/hexdump.min.js b/dist/hexdump.min.js new file mode 100644 index 0000000..3d6265b --- /dev/null +++ b/dist/hexdump.min.js @@ -0,0 +1 @@ +(function(){"use strict";function hexdump(ab,offset,len,opts){if(!opts){opts={}}var ui8=new Uint8Array(ab.buffer||ab,offset||ab.byteOffset,len||ab.byteLength);var bytecount=0;var head=" 0 1 2 3 4 5 6 7 8 9 A B C D E F";var trail;var str=[].slice.call(ui8).map(function(i){var h=i.toString(16);if(h.length<2){h="0"+h}return h}).join("").match(/.{1,2}/g).join(" ").match(/.{1,48}/g).map(function(str){var lead=bytecount.toString(16);bytecount+=16;while(lead.length<7){lead="0"+lead}while(str.length<48){str+=" "}if(opts.C){return lead+" "+str+" |"+str.replace(/ /g,"").match(/.{1,2}/g).map(function(ch){var c=String.fromCharCode(parseInt(ch,16));if(!/[ -~]/.test(c)){c="."}return c}).join("")+"|"}else{return lead+" "+str}}).join("\n");trail=(len||ab.byteLength).toString(16);while(trail.length<7){trail="0"+trail}return head+"\n"+str+"\n"+trail}window.hexdump=hexdump})(); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..aace039 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "@root/hexdump", + "version": "1.1.1", + "lockfileVersion": 1 +} diff --git a/package.json b/package.json index 6520ef9..d28c7c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@root/hexdump", - "version": "1.1.0", + "version": "1.1.1", "description": "Like hexdump on *nix, but in JavaScript.", "main": "hexdump.js", "homepage": "https://git.rootprojects.org/root/hexdump.js", diff --git a/test.txt b/test.txt index 8ab686e..017a65a 100644 --- a/test.txt +++ b/test.txt @@ -1 +1 @@ -Hello, World! +Hello, 世界!