mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-04 07:09:14 +00:00
Add indexer service (equivalent of Dereferencer, but named more accurately)
This commit is contained in:
13
app/assets/javascripts/admin/services/indexer.js.coffee
Normal file
13
app/assets/javascripts/admin/services/indexer.js.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
# Convert an array of objects into a hash, indexed by the objects' ids
|
||||
#
|
||||
# producers = [{id: 1, name: 'one'}, {id: 2, name: 'two'}]
|
||||
# Indexer.index producers
|
||||
# -> {1: {id: 1, name: 'one'}, 2: {id: 2, name: 'two'}}
|
||||
|
||||
angular.module("ofn.admin").factory 'Indexer', ->
|
||||
new class Indexer
|
||||
index: (data) ->
|
||||
index = []
|
||||
for e in data
|
||||
index[e.id] = e
|
||||
index
|
||||
13
spec/javascripts/unit/admin/services/indexer_spec.js.coffee
Normal file
13
spec/javascripts/unit/admin/services/indexer_spec.js.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
describe "indexer", ->
|
||||
Indexer = null
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin"
|
||||
|
||||
beforeEach inject (_Indexer_) ->
|
||||
Indexer = _Indexer_
|
||||
|
||||
it "indexes an array of objects by id", ->
|
||||
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'}})
|
||||
Reference in New Issue
Block a user