Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 29 additions & 16 deletions bin/unwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
45 changes: 29 additions & 16 deletions bin/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}