Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created February 12, 2015 07:04
Show Gist options
  • Select an option

  • Save liammclennan/8c642478f6e39c1303d2 to your computer and use it in GitHub Desktop.

Select an option

Save liammclennan/8c642478f6e39c1303d2 to your computer and use it in GitHub Desktop.
Mapping serialized tuples (F#) to JavaScript objects
// response from server
// "[{"item1":{"id":"ce957038-fed0-4c09-8594-5475efcac4d3","name":"https://github.com/liammcl..."},"item2":3}, ...]"
doSomethingAjaxing().then(mapSerializedTuples.bind(null, ['deck', 'count']));
// after mapping
// "[{"deck":{"id":"ce957038-fed0-4c09-8594-5475efcac4d3","name":"https://github.com/liammcl..."},"count":3}, ...]"
function mapSerializedTuple(keyNames) {
return function (tuple) {
var o = {};
keyNames.forEach(function (key,i) {
o[key] = tuple['item' + (i+1)];
});
return o;
};
}
function mapSerializedTuples(keyNames, tuples) {
return tuples.map(mapSerializedTuple(keyNames))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment