Created
January 8, 2013 16:13
-
-
Save romanmt/4485038 to your computer and use it in GitHub Desktop.
join problem
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
| 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; | |
| } |
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
| 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