| 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 |
| 5 function forEach(dict, f) { | 7 function forEach(dict, f) { |
| 6 for (var key in dict) { | 8 for (var key in dict) { |
| 7 if (dict.hasOwnProperty(key)) | 9 if (dict.hasOwnProperty(key)) |
| 8 f(key, dict[key]); | 10 f(key, dict[key]); |
| 9 } | 11 } |
| 10 } | 12 } |
| 11 | 13 |
| 12 // Assuming |array_of_dictionaries| is structured like this: | 14 // Assuming |array_of_dictionaries| is structured like this: |
| 13 // [{id: 1, ... }, {id: 2, ...}, ...], you can use | 15 // [{id: 1, ... }, {id: 2, ...}, ...], you can use |
| 14 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. | 16 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. |
| 15 function lookup(array_of_dictionaries, field, value) { | 17 function lookup(array_of_dictionaries, field, value) { |
| 16 var filter = function (dict) {return dict[field] == value;}; | 18 var filter = function (dict) {return dict[field] == value;}; |
| 17 var matches = array_of_dictionaries.filter(filter); | 19 var matches = array_of_dictionaries.filter(filter); |
| 18 if (matches.length == 0) { | 20 if (matches.length == 0) { |
| 19 return undefined; | 21 return undefined; |
| 20 } else if (matches.length == 1) { | 22 } else if (matches.length == 1) { |
| 21 return matches[0] | 23 return matches[0] |
| 22 } else { | 24 } else { |
| 23 throw new Error("Failed lookup of field '" + field + "' with value '" + | 25 throw new Error("Failed lookup of field '" + field + "' with value '" + |
| 24 value + "'"); | 26 value + "'"); |
| 25 } | 27 } |
| 26 } | 28 } |
| 27 | 29 |
| 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 |
| 28 exports.forEach = forEach; | 40 exports.forEach = forEach; |
| 41 exports.loadRefDependency = loadRefDependency; |
| 29 exports.lookup = lookup; | 42 exports.lookup = lookup; |
| OLD | NEW |