Skip to content

Instantly share code, notes, and snippets.

@romanmt
Created January 8, 2013 16:13
Show Gist options
  • Select an option

  • Save romanmt/4485038 to your computer and use it in GitHub Desktop.

Select an option

Save romanmt/4485038 to your computer and use it in GitHub Desktop.
join problem
var Levers = function() {
var exports = {
select: function(ids, levers, defaults) {
function mapper(element) {
return levers[element] || _(defaults).extend({id: element});
}
return _(ids).map(mapper);
},
objectify: function(levers) {
function reducer(obj, value) {
obj[value.id] = value;
return obj;
}
var object = _(levers).reduce(reducer, {})
return object;
}
}
return exports;
}
describe('Levers', function() {
describe('#select()', function() {
it("defaults if the value doesn't exist", function(done) {
var ids = ['b']
var obj = {}
var defaults = {val: 'hello'}
var expected = [{id: 'b', val: 'hello'}]
expect(Levers().select(ids, obj, defaults)).toEqual(expected)
})
it("selects elements from object into an array", function() {
var ids = ['b']
var obj = {'a': {id: "a"}, 'b': {id: "b"}}
var expected = [{id: 'b'}]
expect(Levers().select(ids, obj)).toEqual(expected)
})
})
describe('#Objectify', function() {
it("converts an array of objects to a single object", function() {
var array = [{id: 'a'}, {id: 'b'}];
expect(Levers().objectify(array)).toEqual({'a': array[0], 'b': array[1]});
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment