Last active
August 29, 2015 14:12
-
-
Save qmacro/0f5c5f4acd12fd8c277d to your computer and use it in GitHub Desktop.
Functional JavaScript scratchpad
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
// Test data only, sourced from a JSON/YAML style data set; this | |
// is not realistic in production as the data would be normalised | |
// and the repetitive nature would be avoided, but it's a reality | |
// when you're setting up test data by hand, so valid. This data | |
// is referred to later as "d.employees" | |
[ | |
{ | |
"name":"Employee Zero", | |
"wbses":[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
} | |
] | |
}, | |
{ | |
"name":"Employee One", | |
"wbses":[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] | |
}, | |
{ | |
"name":"Employee Two", | |
"wbses":[ | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] | |
}, | |
{ | |
"name":"Employee Three", | |
"wbses":[ | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] | |
} | |
] |
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
// property getter to be used in a map() call | |
getProp = function(p) { | |
return function(m) { | |
return m[p]; | |
}; | |
}; |
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
// This function written with Joseph (@jcla1) after discussion in the UI5 model | |
// binding context. It's much better as it gives directly what is needed; it uses | |
// an index and an array, keeping track of uniqueness with the index. Lovely! | |
nubBy = function(key) { | |
return function(map, item) { | |
// set up map if required | |
["index", "array"].forEach(function(key) { | |
if (!map.hasOwnProperty(key)) { map[key] = []; } | |
}); | |
var id = item[key]; | |
if (map.index.indexOf(id) === -1) { | |
map.index.push(id); | |
map.array.push(item); | |
} | |
return map; | |
}; | |
} |
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
// returns reducer function to return a unique list | |
// of values for a given property | |
reduceToArray = function(key) { | |
return function(array, item) { | |
if (array.indexOf(item[key]) === -1) { | |
array.push(item[key]); | |
} | |
return array; | |
} | |
} |
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
// returns reducer function to return a map of maps | |
// that is unique based on the value of a given property | |
reduceToMap = function(key) { | |
return function(map, item) { | |
if (Object.keys(map).indexOf(item[key]) === -1) { | |
map[item[key]] = item; | |
} | |
return map; | |
}; | |
} |
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
// Get unflat list of all WBSes | |
// d.employees.map(getProp('wbses')) | |
// => | |
[ | |
[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
} | |
], | |
[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
], | |
[ | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
], | |
[ | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] | |
] |
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
// Get unflat list of all WBSes | |
// Flatten it with concat | |
// Array.prototype.concat.apply([], d.employees.map(getProp('wbses'))) | |
// => | |
[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
}, | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] |
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
// Get unflat list of all WBSes | |
// Flatten it with concat | |
// Reduce to a list of unique WBSes | |
// Array.prototype.concat.apply([], d.employees.map(getProp('wbses'))).reduce(reduceToArray('id'), []) | |
// => | |
[ | |
"wbs-1", | |
"wbs-2", | |
"wbs-3" | |
] |
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
// Get unflat list of all WBSes | |
// Flatten it with concat | |
// Reduce to a map of full-detail unique WBSes | |
// Array.prototype.concat.apply([], d.employees.map(getProp('wbses'))).reduce(reduceToMap('id'), {}) | |
// => | |
{ | |
"wbs-1":{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
"wbs-2":{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
"wbs-3":{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
} |
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
// Get unflat list of all WBSes | |
// Flatten it with concat | |
// Reduce to an index of unique ids with an array of full-detail unique WBSes | |
// The data in "array" is ready for setting in a client model in UI5, for example | |
// Array.prototype.concat.apply([], d.employees.map(getProp('wbses'))).reduce(nubBy('id'), {}) | |
// => | |
{ | |
"index":[ | |
"wbs-1", | |
"wbs-2", | |
"wbs-3" | |
], | |
"array":[ | |
{ | |
"id":"wbs-1", | |
"description":"this is the WBS 1" | |
}, | |
{ | |
"id":"wbs-2", | |
"description":"this is the WBS 2" | |
}, | |
{ | |
"id":"wbs-3", | |
"description":"this is the WBS 3" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment