diff --git a/content/_code-samples/tx-serialization/js/README.md b/content/_code-samples/tx-serialization/js/README.md index cd2fbd25b3..1a1bc47518 100644 --- a/content/_code-samples/tx-serialization/js/README.md +++ b/content/_code-samples/tx-serialization/js/README.md @@ -18,6 +18,10 @@ On first run, you have to install the necessary node.js dependencies: node index.js -v +### Raw output without formatting, use --raw or -r: + + node index.js -r + ### Pick JSON fixture file: node index.js -f test-cases/tx3.json diff --git a/content/_code-samples/tx-serialization/js/index.js b/content/_code-samples/tx-serialization/js/index.js index 67fef50a15..859fa34be1 100644 --- a/content/_code-samples/tx-serialization/js/index.js +++ b/content/_code-samples/tx-serialization/js/index.js @@ -7,16 +7,16 @@ const TxSerializer = require('./tx-serializer') function main(rawJson, verbose) { const json = JSON.parse(rawJson) - console.log('\x1b[33m%s\x1b[0m', '\nXRPL Transaction Serialization Example') - console.log('\x1b[33m%s\x1b[0m', '--------------------------------------') - console.log('\x1b[37m%s\x1b[0m', '\nSerializing the following transaction:') - console.log(json) - if (verbose) console.log('') + _pretty('\nXRPL Transaction Serialization Example', '\x1b[33m%s\x1b[0m') + _pretty('--------------------------------------', '\x1b[33m%s\x1b[0m') + _pretty('\nSerializing the following transaction:', '\x1b[37m%s\x1b[0m') + _pretty(json) + if (verbose) _pretty('') const serializer = new TxSerializer(verbose) const serializedTx = serializer.serializeTx(json) - console.log('\x1b[37m%s\x1b[0m', '\nSerialized Transaction:') + _pretty('\nSerialized Transaction:', '\x1b[37m%s\x1b[0m') console.log(serializedTx.toUpperCase()) } @@ -24,15 +24,23 @@ const args = parseArgs(process.argv.slice(2), { alias: { 'f': 'filename', 'j': 'json', + 'r': 'raw', 's': 'stdin', 'v': 'verbose', }, default: { 'f': 'test-cases/tx1.json', + 'r': false, 'v': false } }) +function _pretty(message, color) { + if (!args.raw) { + console.log(color, message) + } +} + let rawJson if (args.json) { rawJson = args.json diff --git a/content/_code-samples/tx-serialization/js/test-cases/README.md b/content/_code-samples/tx-serialization/js/test-cases/README.md index 09408d2608..fac686e78c 100644 --- a/content/_code-samples/tx-serialization/js/test-cases/README.md +++ b/content/_code-samples/tx-serialization/js/test-cases/README.md @@ -18,7 +18,7 @@ For an example of how the output is different if you change the `Fee` parameter ```bash $ cat test-cases/tx1.json | \ sed -e 's/"Fee": "10"/"Fee": "100"/' | \ - node index.js --json | \ + node index.js --raw --stdin | \ diff - test-cases/tx1-binary.txt --color ``` @@ -46,7 +46,7 @@ CDC63E1DEE7FE3744630440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED For a friendlier display, you could pipe the output of the serializer to a file and use a visual tool like [Meld](http://meldmerge.org/) that shows intra-line differences: ```bash -$ cat test-cases/tx1.json | sed -e 's/"Fee": "10"/"Fee": "100"/' | node index.js --stdin --stdin > /tmp/tx1-modified.txt && meld /tmp/tx1-modified.txt test-cases/tx1-binary.txt +$ cat test-cases/tx1.json | sed -e 's/"Fee": "10"/"Fee": "100"/' | node index.js --stdin > /tmp/tx1-modified.txt && meld /tmp/tx1-modified.txt test-cases/tx1-binary.txt ``` ![Meld screenshot showing the `0A` / `64` difference](meld-example.png)