| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var chrome = requireNative('chrome').GetChrome(); | |
| 6 | |
| 7 function forEach(dict, f) { | 5 function forEach(dict, f) { |
| 8 for (var key in dict) { | 6 for (var key in dict) { |
| 9 if (dict.hasOwnProperty(key)) | 7 if (dict.hasOwnProperty(key)) |
| 10 f(key, dict[key]); | 8 f(key, dict[key]); |
| 11 } | 9 } |
| 12 } | 10 } |
| 13 | 11 |
| 14 // Assuming |array_of_dictionaries| is structured like this: | 12 // Assuming |array_of_dictionaries| is structured like this: |
| 15 // [{id: 1, ... }, {id: 2, ...}, ...], you can use | 13 // [{id: 1, ... }, {id: 2, ...}, ...], you can use |
| 16 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. | 14 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. |
| 17 function lookup(array_of_dictionaries, field, value) { | 15 function lookup(array_of_dictionaries, field, value) { |
| 18 var filter = function (dict) {return dict[field] == value;}; | 16 var filter = function (dict) {return dict[field] == value;}; |
| 19 var matches = array_of_dictionaries.filter(filter); | 17 var matches = array_of_dictionaries.filter(filter); |
| 20 if (matches.length == 0) { | 18 if (matches.length == 0) { |
| 21 return undefined; | 19 return undefined; |
| 22 } else if (matches.length == 1) { | 20 } else if (matches.length == 1) { |
| 23 return matches[0] | 21 return matches[0] |
| 24 } else { | 22 } else { |
| 25 throw new Error("Failed lookup of field '" + field + "' with value '" + | 23 throw new Error("Failed lookup of field '" + field + "' with value '" + |
| 26 value + "'"); | 24 value + "'"); |
| 27 } | 25 } |
| 28 } | 26 } |
| 29 | 27 |
| 30 // Specify |currentApi| if this should return an API for $refs in the current | |
| 31 // namespace. | |
| 32 function loadRefDependency(ref, currentApi) { | |
| 33 var parts = ref.split("."); | |
| 34 if (parts.length > 1) | |
| 35 return chrome[parts.slice(0, parts.length - 1).join(".")]; | |
| 36 else | |
| 37 return currentApi; | |
| 38 } | |
| 39 | |
| 40 exports.forEach = forEach; | 28 exports.forEach = forEach; |
| 41 exports.loadRefDependency = loadRefDependency; | |
| 42 exports.lookup = lookup; | 29 exports.lookup = lookup; |
| OLD | NEW |