Ok you *never mutate anything* nazis - here's one for you. Basically I get a user object back from Cognito that looks like this:
Code:
{
"Username": "a0ca870d-a77b-4222-978b-6f7a0ced58cb",
"Attributes": [
{
"Name": "custom:BlackBaudId",
"Value": "SomeLookupId"
},
{
"Name": "sub",
"Value": "a0ca870d-a77b-4222-978b-6f7a0ced58cb"
},
{
"Name": "email_verified",
"Value": "true"
},
{
"Name": "email",
"Value": "joe@gmail.com"
}
],
"UserCreateDate": "2018-12-18T01:22:30.423Z",
"UserLastModifiedDate": "2018-12-18T01:22:40.370Z",
"Enabled": true,
"UserStatus": "CONFIRMED"
}
But that's kind of painful to always be looping or plucking to get access to Attributes like email. So I want to massage it into something more useful at the point of extraction - like this:
Code:
{
"Username": "a0ca870d-a77b-4222-978b-6f7a0ced58cb",
"Attributes": {
"custom:BlackBaudId": "SomeLookupId",
"sub": "a0ca870d-a77b-4222-978b-6f7a0ced58cb",
"email_verified": "true",
"email": "joe@gmail.com"
},
"UserCreateDate": "2018-12-18T01:22:30.423Z",
"UserLastModifiedDate": "2018-12-18T01:22:40.370Z",
"Enabled": true,
"UserStatus": "CONFIRMED"
}
If I'm allowed to mutate my argument, then I can make a method like this:
Code:
const processUser = user => {
const attrObj = {};
user.Attributes.forEach(attr => { attrObj[attr.Name] = attr.Value; });
user.Attributes = attrObj;
};
If I am
not allowed to mutate my argument, then I have to use either something like lodash deep clone, or the JSON.parse(JSON.stringify(user)) trick - which isn't the best for performance or code simplicity. Also the JSON trick doesn't work when the object could have functions or circular references - which we don't have to worry about here, but could in other cases.
Thoughts? Am I approaching this wrong?