diff --git a/README.md b/README.md index a943298..914cfaf 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ Use with the cli for quick utility dynamo-dt-attr-wrap '{"hello":"world"}' dynamo-dt-attr-unwrap '{"hello": {"S": "world"}}' +Pipe is also supported + + echo '{"hello":"world"}' | dynamo-dt-attr-wrap + echo '{"hello": {"S": "world"}}' | dynamo-dt-attr-unwrap ### Use in the browser diff --git a/bin/unwrap.js b/bin/unwrap.js index e5174c0..7bd9215 100644 --- a/bin/unwrap.js +++ b/bin/unwrap.js @@ -3,24 +3,37 @@ 'use strict'; const attr = require('../lib/dynamodb-data-types').AttributeValue; -const content = process.argv[2]; -if (!content) { - throw new Error('Input data required.') +if (process.stdin.isTTY) { + unwrap(process.argv[2]); +} else { + let data = ""; + process.stdin.on('data', function(chunk) { + data += chunk; + }); + process.stdin.on('end', function() { + unwrap(data); + }); } -let payload = ''; -try { - payload = JSON.parse(content); -} -catch (err) { - payload = content -} -const output = attr.unwrap(payload); +function unwrap(content) { + if (!content) { + throw new Error('Input data required.') + } -if (typeof output === 'object') { - process.stdout.write(JSON.stringify(output) + '\n'); -} -else { - process.stdout.write(output + '\n'); + let payload = ''; + try { + payload = JSON.parse(content); + } + catch (err) { + payload = content + } + const output = attr.unwrap(payload); + + if (typeof output === 'object') { + process.stdout.write(JSON.stringify(output) + '\n'); + } + else { + process.stdout.write(output + '\n'); + } } diff --git a/bin/wrap.js b/bin/wrap.js index 25f25a7..2082888 100644 --- a/bin/wrap.js +++ b/bin/wrap.js @@ -3,24 +3,37 @@ 'use strict'; const attr = require('../lib/dynamodb-data-types').AttributeValue; -const content = process.argv[2]; -if (!content) { - throw new Error('Input data required.') +if (process.stdin.isTTY) { + wrap(process.argv[2]); +} else { + let data = ""; + process.stdin.on('data', function(chunk) { + data += chunk; + }); + process.stdin.on('end', function() { + wrap(data); + }); } -let payload = ''; -try { - payload = JSON.parse(content); -} -catch (err) { - payload = content -} -const output = attr.wrap(payload); +function wrap(content) { + if (!content) { + throw new Error('Input data required.') + } -if (typeof output === 'object') { - process.stdout.write(JSON.stringify(output) + '\n'); -} -else { - process.stdout.write(output + '\n'); + let payload = ''; + try { + payload = JSON.parse(content); + } + catch (err) { + payload = content + } + const output = attr.wrap(payload); + + if (typeof output === 'object') { + process.stdout.write(JSON.stringify(output) + '\n'); + } + else { + process.stdout.write(output + '\n'); + } }