mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Indexer accepts arbitrary key to index by. Fix bug: Return an object instead of an array.
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
|
||||
angular.module("ofn.admin").factory 'Indexer', ->
|
||||
new class Indexer
|
||||
index: (data) ->
|
||||
index = []
|
||||
index: (data, key='id') ->
|
||||
index = {}
|
||||
for e in data
|
||||
index[e.id] = e
|
||||
index[e[key]] = e
|
||||
index
|
||||
|
||||
@@ -11,3 +11,12 @@ describe "indexer", ->
|
||||
objects = [{id: 1, name: 'one'}, {id: 2, name: 'two'}]
|
||||
index = Indexer.index objects
|
||||
expect(index).toEqual({1: {id: 1, name: 'one'}, 2: {id: 2, name: 'two'}})
|
||||
|
||||
it "indexes an array of objects by another field", ->
|
||||
objects = [{widget_id: 1, name: 'one'}, {widget_id: 2, name: 'two'}]
|
||||
index = Indexer.index objects, 'widget_id'
|
||||
expect(index).toEqual({1: {widget_id: 1, name: 'one'}, 2: {widget_id: 2, name: 'two'}})
|
||||
|
||||
it "returns an object, not an array", ->
|
||||
index = Indexer.index []
|
||||
expect(index.constructor).not.toEqual(Array)
|
||||
|
||||
Reference in New Issue
Block a user