I have these three functions that basically do the same thing. They take data coming from the promise and make an object with the same properties for each function.
I would like to DRY this out:
var getFiles = function() {
return wg.getFiles().then(function(data) {
processData(data, {
type: "File",
title: x.filename,
created: x.Created,
path: x.path
})
})
}
var getEvents = function() {
return wg.getEvents().then(function(data) {
processData(data, {
type: "Event",
title: x.Title,
created: x.Created,
path: x.path
})
})
}
var getFeedback = function() {
return wg.getFeedback().then(function(data) {
processData(data, {
type: "Review",
title: "by " + x.Author,
created: x.Created,
path: x.path
})
})
}
var processData = function(data, props) {
var x = _(data)
.map(function(x) {return props})
.value()
.map(function(x) {
activties.push(x)
})
}
I would like to DRY this out by changing the processData
function to something like this:
var processData = function(data, props) {
var x = _(data)
.map(function(x) {
return {
type: x[props[0]],
title: x[props[1]],
created: x[props[3]],
path: "/" + x[props[4]]
}
})
.value()
.map(function(x) {
activties.push(x)
})
}
Then I could call it like this:
var getFiles = function() {
return wg.getFiles().then(function(data) {
processData(data, ['filename', 'created', ['FileRef']['lookupValue']])
})
}
That's what I have in mind but if anyone has anything better I'm open.