| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <!-- | 2 <!-- |
| 3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | 3 Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
| 5 found in the LICENSE file. | 5 found in the LICENSE file. |
| 6 --> | 6 --> |
| 7 | 7 |
| 8 <link rel="import" href="/tracing/base/base.html"> | 8 <link rel="import" href="/tracing/base/base.html"> |
| 9 | 9 |
| 10 <script> | 10 <script> |
| 11 'use strict'; | 11 'use strict'; |
| 12 | 12 |
| 13 tr.exportTo('tr.b', function() { | 13 tr.exportTo('tr.b', function() { |
| 14 |
| 14 /** | 15 /** |
| 15 * Converts any object which is either (a) an iterable, or (b) an | 16 * Converts any object which is either (a) an iterable, or (b) an |
| 16 * "array-ish" obect (has length property and can be indexed into) | 17 * "array-ish" object (has length property and can be indexed into) |
| 17 * into an array. | 18 * into an array. |
| 18 */ | 19 */ |
| 19 function asArray(x) { | 20 function asArray(x) { |
| 20 var values = []; | 21 var values = []; |
| 21 if (x[Symbol.iterator]) | 22 if (x[Symbol.iterator]) |
| 22 for (var value of x) | 23 for (var value of x) |
| 23 values.push(value); | 24 values.push(value); |
| 24 else | 25 else |
| 25 for (var i = 0; i < x.length; i++) | 26 for (var i = 0; i < x.length; i++) |
| 26 values.push(x[i]); | 27 values.push(x[i]); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 function every(iterable, predicate) { | 167 function every(iterable, predicate) { |
| 167 for (var x of iterable) | 168 for (var x of iterable) |
| 168 if (!predicate(x)) | 169 if (!predicate(x)) |
| 169 return false; | 170 return false; |
| 170 return true; | 171 return true; |
| 171 } | 172 } |
| 172 | 173 |
| 173 /** | 174 /** |
| 174 * Returns a new dictionary with items grouped by the return value of the | 175 * Returns a new dictionary with items grouped by the return value of the |
| 175 * specified function being called on each item. | 176 * specified function being called on each item. |
| 176 * @param {!Array.<Object>} ary The array being iterated through | 177 * @param {!Array.<!*>} ary The array being iterated through |
| 177 * @param {!Function} fn The mapping function between the array value and the | 178 * @param {!function(!*):!*} callback The mapping function between the array |
| 178 * map key. | 179 * value and the map key. |
| 180 * @param {*=} opt_this |
| 179 */ | 181 */ |
| 180 function group(ary, fn) { | 182 function group(ary, callback, opt_this) { |
| 181 return ary.reduce(function(accumulator, curr) { | 183 var results = {}; |
| 182 var key = fn(curr); | 184 for (var element of ary) { |
| 183 | 185 var key = callback.call(opt_this, element); |
| 184 if (key in accumulator) | 186 if (key in results) |
| 185 accumulator[key].push(curr); | 187 results[key].push(element); |
| 186 else | 188 else |
| 187 accumulator[key] = [curr]; | 189 results[key] = [element]; |
| 188 | 190 } |
| 189 return accumulator; | 191 return results; |
| 190 }, {}); | |
| 191 } | 192 } |
| 192 | 193 |
| 193 function iterItems(dict, fn, opt_this) { | 194 function iterItems(dict, fn, opt_this) { |
| 194 opt_this = opt_this || this; | 195 opt_this = opt_this || this; |
| 195 var keys = Object.keys(dict); | 196 var keys = Object.keys(dict); |
| 196 for (var i = 0; i < keys.length; i++) { | 197 for (var i = 0; i < keys.length; i++) { |
| 197 var key = keys[i]; | 198 var key = keys[i]; |
| 198 fn.call(opt_this, key, dict[key]); | 199 fn.call(opt_this, key, dict[key]); |
| 199 } | 200 } |
| 200 } | 201 } |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 arrayToDict: arrayToDict, | 439 arrayToDict: arrayToDict, |
| 439 identity: identity, | 440 identity: identity, |
| 440 findFirstIndexInArray: findFirstIndexInArray, | 441 findFirstIndexInArray: findFirstIndexInArray, |
| 441 findFirstInArray: findFirstInArray, | 442 findFirstInArray: findFirstInArray, |
| 442 findFirstKeyInDictMatching: findFirstKeyInDictMatching, | 443 findFirstKeyInDictMatching: findFirstKeyInDictMatching, |
| 443 mapValues: mapValues, | 444 mapValues: mapValues, |
| 444 iterMapItems: iterMapItems | 445 iterMapItems: iterMapItems |
| 445 }; | 446 }; |
| 446 }); | 447 }); |
| 447 </script> | 448 </script> |
| OLD | NEW |