Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('media', function() { | |
| 6 | |
| 7 /** | |
| 8 * This class stores hashes by their id field and provides basic methods for | |
| 9 * iterating over the collection. | |
| 10 * | |
| 11 * @constructor | |
| 12 */ | |
| 13 function ItemStore() { | |
| 14 this.items_ = {}; | |
| 15 }; | |
| 16 | |
| 17 ItemStore.prototype = { | |
| 18 /** | |
| 19 * Get a sorted list of item ids. | |
| 20 * | |
| 21 * @return {Array} A sorted array of ids. | |
| 22 */ | |
| 23 ids: function() { | |
| 24 var ids = []; | |
| 25 for (var i in this.items_) | |
| 26 ids.push(i); | |
| 27 return ids.sort(); | |
| 28 }, | |
| 29 | |
| 30 /** | |
| 31 * Add an item to the store. | |
| 32 * | |
| 33 * @param {Object} item The item to be added. | |
| 34 * @param {string} item.id The id of the item. | |
| 35 */ | |
| 36 addItem: function(item) { | |
| 37 this.items_[item.id] = item; | |
| 38 }, | |
| 39 | |
| 40 /** | |
| 41 * Add a dictionary of items to the store. | |
| 42 * | |
|
Evan Stade
2011/07/15 22:31:05
don't need blank line between description and para
Scott Franklin
2011/07/15 23:18:15
On a few with really long descriptions. It just fe
| |
| 43 * @param {Object} items A dictionary of individual items. The keys are | |
| 44 * irrelevant but each must have an id field. | |
| 45 */ | |
| 46 addItems: function(items) { | |
| 47 for (id in items) | |
| 48 this.addItem(items[id]); | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * Remove an item from the store. | |
| 53 * | |
| 54 * @param {string} id The id of the item to be removed. | |
| 55 */ | |
| 56 removeItem: function(id) { | |
| 57 delete this.items_[id]; | |
| 58 }, | |
| 59 | |
| 60 /** | |
| 61 * Map this itemStore to an Array. Items are sorted by id. | |
| 62 * | |
| 63 * @param {function(*)} mapper The mapping function applied to each item. | |
| 64 * @return {Array} An array of mapped items. | |
|
Evan Stade
2011/07/15 22:31:05
extra space
Scott Franklin
2011/07/15 23:18:15
Done.
| |
| 65 */ | |
| 66 map: function(mapper) { | |
| 67 var items = this.items_; | |
| 68 var ids = this.ids(); | |
| 69 return ids.map(function(id) { return mapper(items[id]); }); | |
| 70 } | |
| 71 }; | |
| 72 | |
| 73 return { | |
| 74 ItemStore: ItemStore | |
| 75 }; | |
| 76 | |
|
Evan Stade
2011/07/15 22:31:05
don't need this blank line
Scott Franklin
2011/07/15 23:18:15
Done.
| |
| 77 }); | |
| OLD | NEW |