Last active
March 3, 2016 02:36
-
-
Save mrgenixus/eb7f82bd943c8cfe34a7 to your computer and use it in GitHub Desktop.
group ingredients by stemmed_title
This file contains hidden or 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
| function group_or_item(values){ | |
| return (values.length > 1 ? 'group' : 'item'); | |
| } | |
| function get_group_values(key) { | |
| return groups[key]; | |
| } | |
| function getFrom(list) { | |
| return function(key) { | |
| return list[key]; | |
| } | |
| } | |
| function groupItems(callback) { | |
| var _items = [{"title":"Yellow Onion","stemmed_title":"onion"},{"title":"Sweet Onion","stemmed_title":"onion"},{"title":"Red Onion","stemmed_title":"onion"},{"title":"Olive Onion","stemmed_title":"oil"},{"title":"Canola Onion","stemmed_title":"oil"},{"title":"Flour","stemmed_title":"flour"},{"title":"Water","stemmed_title":"water"},{"title":"Salt","stemmed_title":"salt"}] | |
| _.reduce(_.reduce(_items, function(memo, value) { | |
| var stemmed_title = value.stemmed_title; | |
| memo[stemmed_title] = memo[stemmed_title] || []; | |
| memo[stemmed_title].push(value); | |
| return memo; | |
| }, {}), function(memo, values, stemmed_title) { | |
| memo['groups'] || (memo['groups'] = {}) | |
| memo['items'] || (memo['items'] = []) | |
| if (values.length > 1 ) { | |
| memo.groups[stemmed_title] = values; | |
| } else { | |
| memo.items.push(_.first(values)); | |
| } | |
| return memo; | |
| }, {}); | |
| /* | |
| { | |
| "groups":{ | |
| "onion":[ | |
| { | |
| "title":"Yellow Onion", | |
| "stemmed_title":"onion" | |
| }, | |
| { | |
| "title":"Sweet Onion", | |
| "stemmed_title":"onion" | |
| }, | |
| { | |
| "title":"Red Onion", | |
| "stemmed_title":"onion" | |
| } | |
| ], | |
| "oil":[ | |
| { | |
| "title":"Olive Onion", | |
| "stemmed_title":"oil" | |
| }, | |
| { | |
| "title":"Canola Onion", | |
| "stemmed_title":"oil" | |
| } | |
| ] | |
| }, | |
| "items":[ | |
| { | |
| "title":"Flour", | |
| "stemmed_title":"flour" | |
| }, | |
| { | |
| "title":"Water", | |
| "stemmed_title":"water" | |
| }, | |
| { | |
| "title":"Salt", | |
| "stemmed_title":"salt" | |
| } | |
| ] | |
| } | |
| */ | |
| var groups = _.groupBy(_items, 'stemmed_title'); | |
| var partitioned_groups = _.groupBy(_.keys(groups), _.flow(get_group_values, group_or_item)) | |
| var groupGroups = _.reduce(partitioned_groups.group, function(memo, key){ | |
| memo[key] = groups[key]; | |
| return memo; | |
| }, {}) | |
| var itemItems = _.flatten(_.map(partitioned_groups.item, getFrom(groups))); | |
| /* | |
| { | |
| "partitioned_groups": { | |
| "group": ["onion","oil"], | |
| "item": ["flour","water","salt"] | |
| }, | |
| "itemItems": [ | |
| { | |
| "title": "Flour", | |
| "stemmed_title": "flour" | |
| }, | |
| { | |
| "title": "Water", | |
| "stemmed_title": "water" | |
| }, | |
| { | |
| "title": "Salt", | |
| "stemmed_title": "salt" | |
| } | |
| ], | |
| "groupGroups": { | |
| "onion": [ | |
| { | |
| "title": "Yellow Onion", | |
| "stemmed_title": "onion" | |
| }, | |
| { | |
| "title": "Sweet Onion", | |
| "stemmed_title": "onion" | |
| }, | |
| { | |
| "title": "Red Onion", | |
| "stemmed_title": "onion" | |
| } | |
| ], | |
| "oil": [ | |
| { | |
| "title": "Olive Onion", | |
| "stemmed_title": "oil" | |
| }, | |
| { | |
| "title": "Canola Onion", | |
| "stemmed_title": "oil" | |
| } | |
| ] | |
| } | |
| } | |
| */ | |
| callback({partitioned_groups, groupGroups, itemItems}, arguments); | |
| } | |
| document.addEventListener('click', _.partial(groupItems, console.log.bind(console))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment