Skip to content

Instantly share code, notes, and snippets.

@andrewluetgers
Last active February 25, 2017 07:09
Show Gist options
  • Select an option

  • Save andrewluetgers/adbad605d779a645808fb1b31f9f2ad6 to your computer and use it in GitHub Desktop.

Select an option

Save andrewluetgers/adbad605d779a645808fb1b31f9f2ad6 to your computer and use it in GitHub Desktop.
function mapper(data, setType) {
let keys = Object.keys(data),
ret = data.concat ? [] : {};
console.log("keys", keys);
while (keys.length) {
let key = keys.shift(),
obj = data[key];
console.log("key", key, obj);
if (setType) {
switch(setType) {
case "BB":
case "SS": ret[key] = obj; break;
case "NN": ret[key] = parseFloat(obj); break;
}
} else if (typeof obj === "object" && obj !== null) {
let type = Object.keys(obj)[0];
switch(type) {
case "S":
case "B":
case "BOOL": ret[key] = obj[type]; break;
case "N": ret[key] = parseFloat(obj[type]); break;
case "NULL": ret[key] = null; break;
case "L":
case "M": ret[key] = mapper(obj[type]); break;
case "SS":
case "NN":
case "BB": ret[key] = mapper(obj[type], type); break;
}
}
}
return ret;
}
// taken from
// https://github.com/CascadeEnergy/dynamoDb-marshaler/blob/master/examples/example-unmarshal.js
var item = {
name: {S: 'Will Vaughn'},
age: {N: '30'},
employed: {BOOL: true},
relationshipStatus: {NULL: true},
defaultReplies: {
L: [
{S: 'No.'},
{N: '42'}
]
},
appearance: {
M: {
height: {N: '183'},
weight: {N: '79'},
eyeColor: {S: 'brown'},
ethnicity: {
M: {
whiteAmerican: {BOOL: true},
nativeAmerican: {BOOL: false},
asianAmerican: {BOOL: false},
africanAmerican: {BOOL: false},
nativeHawaiianOrPacificIslander: {BOOL: false},
someOtherRace: {BOOL: false}
}
}
}
},
favoriteFoods: {
SS: [
'burritos',
'fried chicken',
'pad kee mao'
]
},
luckyNumbers: {
NN: ['42', '98', '777']
},
empty: {
L: []
},
duplicateList: {
L: [
{
S: 'foo'
},
{
S: 'bar'
},
{
S: 'foo'
}
]
}
};
mapper(item);
//{
// "name": "Will Vaughn",
// "age": 30,
// "employed": true,
// "relationshipStatus": null,
// "defaultReplies": ["No.", 42],
// "appearance": {
// "height": 183,
// "weight": 79,
// "eyeColor": "brown",
// "ethnicity": {
// "whiteAmerican": true,
// "nativeAmerican": false,
// "asianAmerican": false,
// "africanAmerican": false,
// "nativeHawaiianOrPacificIslander": false,
// "someOtherRace": false
// }
// },
// "favoriteFoods": ["burritos", "fried chicken", "pad kee mao"],
// "luckyNumbers": [42, 98, 777],
// "empty": [],
// "duplicateList": ["foo", "bar", "foo"]
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment