Skip to content

Instantly share code, notes, and snippets.

@johnfkneafsey
Created November 22, 2016 16:49
Show Gist options
  • Save johnfkneafsey/799e8090330291c2064f982e19670e59 to your computer and use it in GitHub Desktop.
Save johnfkneafsey/799e8090330291c2064f982e19670e59 to your computer and use it in GitHub Desktop.
function mergeDataStreams(stream1, stream2) {
var results = {};
for (var i=0; i < stream1.length; i++) {
results[stream1[i].id] = stream1[i];
}
for (var key in results) {
var otherData = stream2.find(
function(item) { return item.id === key; });
for (var _key in otherData) {
results[key][_key] = otherData[_key];
}
}
return Object.keys(results).map(function(item) {return results[item]; });
}
// data
var dataSource1 = [
{
id: '0',
firstName: 'Juan',
lastName: 'Doe',
age: 32
},
{
id: '1',
firstName: 'Jane',
lastName: 'Doe',
age: 31
},
{
id: '2',
firstName: 'Janice',
lastName: 'Doe',
age: 30
},
{
id: '3',
firstName: 'Jake',
lastName: 'Doe',
age: 29
},
];
var dataSource2 = [
{
id: '0',
occupation: 'architect',
address: {
street: '123 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '1',
occupation: 'architect',
address: {
street: '234 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '2',
occupation: 'architect',
address: {
street: '345 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '3',
occupation: 'architect',
address: {
street: '456 Main St',
city: 'CityTown',
country: 'USA'
}
},
];
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testMergeDataStreams(stream1, stream2) {
var expected = stream1.map(function(item) {
return _.assign(
_.clone(item), stream2.find(function(item2) {return item.id === item2.id;}));
});
var actual = mergeDataStreams(stream1, stream2);
var passing = actual && expected.map(function(item) {
return _.isEqual(
item,
actual.find(function(_item) {return _item.id === item.id; })
);
}).every(function(result) {return result;} );
if (passing) {
console.log('SUCCESS! mergeDataStreams works');
}
else {
console.log('FAILURE! mergeDataStreams not working');
}
}
testMergeDataStreams(dataSource1, dataSource2);
function recipeFactory(name, ingredients, steps) {
return {
name: name,
ingredients: ingredients,
steps: steps,
stepsHtml: function() {
return '<ol>' + this.steps.map(
function(step) {return '<li>' + step + '</li>'; }).join('') + '</ol>';
},
ingredientsHtml: function() {
return '<ul>' + this.ingredients.map(
function(ing) {return '<li>' + ing + '</li>'; }).join('') + '</ul>';
}
}
}
function testRecipeFactory() {
var grilledCheese = recipeFactory(
'grilled cheese',
['2 slices bread', 'butter', '1 slice cheese'],
['Butter bread', 'Put cheese between bread', 'Toast bread on stove']
);
$('.js-recipe-name').text(grilledCheese.name);
$('.js-ingredients').html(grilledCheese.ingredientsHtml());
$('.js-steps').html(grilledCheese.stepsHtml());
}
testRecipeFactory()
function recipeFactory(name, ingredients, steps) {
return {
name: name,
ingredients: ingredients,
steps: steps,
stepsHtml: function() {
return '<ol>' + this.steps.map(
function(step) {return '<li>' + step + '</li>'; }).join('') + '</ol>';
},
ingredientsHtml: function() {
return '<ul>' + this.ingredients.map(
function(ing) {return '<li>' + ing + '</li>'; }).join('') + '</ul>';
}
}
}
function testRecipeFactory() {
var grilledCheese = recipeFactory(
'grilled cheese',
['2 slices bread', 'butter', '1 slice cheese'],
['Butter bread', 'Put cheese between bread', 'Toast bread on stove']
);
$('.js-recipe-name').text(grilledCheese.name);
$('.js-ingredients').html(grilledCheese.ingredientsHtml());
$('.js-steps').html(grilledCheese.stepsHtml());
}
testRecipeFactory()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment