Last active
November 22, 2015 12:07
-
-
Save Markionium/0e55e774b3c554521040 to your computer and use it in GitHub Desktop.
Transform trackedEntityAttributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 'TB number': | |
{ displayName: 'TB number', | |
valueType: 'TEXT', | |
attribute: 'ruQQnf6rswq', | |
value: '1Z 1F2 A84 59 4464 173 6' }, | |
Gender: | |
{ displayName: 'Gender', | |
valueType: 'TEXT', | |
attribute: 'cejWyOfXge6', | |
value: 'Male' }, | |
'Last name': | |
{ displayName: 'Last name', | |
valueType: 'TEXT', | |
attribute: 'zDhUuAYrxNC', | |
value: 'Doe' }, | |
'First name': | |
{ code: 'MMD_PER_NAM', | |
displayName: 'First name', | |
valueType: 'TEXT', | |
attribute: 'w75KJ2mc4zz', | |
value: 'John' }, | |
Address: | |
{ code: 'MMD_PER_ADR1', | |
displayName: 'Address', | |
valueType: 'TEXT', | |
attribute: 'VqEFza8wbwA', | |
value: 'Main street 2' }, | |
'National identifier': | |
{ code: 'National identifier', | |
displayName: 'National identifier', | |
valueType: 'TEXT', | |
attribute: 'AuPLng5hLbE', | |
value: '245435245' } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ tbNumber: | |
{ displayName: 'TB number', | |
valueType: 'TEXT', | |
attribute: 'ruQQnf6rswq', | |
value: '1Z 1F2 A84 59 4464 173 6' }, | |
gender: | |
{ displayName: 'Gender', | |
valueType: 'TEXT', | |
attribute: 'cejWyOfXge6', | |
value: 'Male' }, | |
lastName: | |
{ displayName: 'Last name', | |
valueType: 'TEXT', | |
attribute: 'zDhUuAYrxNC', | |
value: 'Doe' }, | |
firstName: | |
{ code: 'MMD_PER_NAM', | |
displayName: 'First name', | |
valueType: 'TEXT', | |
attribute: 'w75KJ2mc4zz', | |
value: 'John' }, | |
address: | |
{ code: 'MMD_PER_ADR1', | |
displayName: 'Address', | |
valueType: 'TEXT', | |
attribute: 'VqEFza8wbwA', | |
value: 'Main street 2' }, | |
nationalIdentifier: | |
{ code: 'National identifier', | |
displayName: 'National identifier', | |
valueType: 'TEXT', | |
attribute: 'AuPLng5hLbE', | |
value: '245435245' } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use as follows: | |
// Pass the attributes list into the function (optionally pass paramater to disable name transforms) | |
// | |
// var transformedAttributes = transformAttributes(trackedEntityInstance.attributes); | |
// This call will return you an object that has the displayName as a camel cased object map. | |
// | |
// var transformedAttributes = transformAttributes(trackedEntityInstance.attributes); | |
// This call will return you an object that has the displayName as a camel cased object map. Which means you can use dot | |
// notation to access them. Getting the value would be: transformedAttributes.firstName.value | |
// | |
// var transformedAttributes = transformAttributes(trackedEntityInstance.attributes, true); | |
// This call will return you an object that has the displayName as it originally was. This means you can NOT use dot notation | |
//to access them. Getting the value would be: transformedAttributes['First name'].value | |
function transformAttributes(attributes, noNameTransform) { | |
if (!Array.isArray(attributes)) { | |
throw new Error('Passed attributes must be in form of an array'); | |
} | |
function transformName(name) { | |
return name.toLowerCase().replace(/\W([a-z])/g, function (matches) { return matches[1].toUpperCase() }); | |
} | |
return attributes | |
.filter(function (value) { | |
if (value && (typeof value.displayName === 'string')) { | |
return true; | |
} | |
return false; | |
}) | |
.map(function (value) { | |
var propertyName = noNameTransform ? value.displayName : transformName(value.displayName); | |
return [propertyName, value]; | |
}) | |
.reduce(function (acc, value) { | |
acc[value[0]] = value[1]; | |
return acc; | |
}, {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment