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 * Converts any object which is either (a) an iterable, or (b) an | |
16 * "array-ish" object (has length property and can be indexed into) | |
17 * into an array. | |
18 */ | |
19 function asArray(x) { | |
20 const values = []; | |
21 if (x[Symbol.iterator]) { | |
22 for (const value of x) { | |
23 values.push(value); | |
24 } | |
25 } else { | |
26 for (let i = 0; i < x.length; i++) { | |
27 values.push(x[i]); | |
28 } | |
29 } | |
30 return values; | |
31 } | |
32 | |
33 /** | |
34 * Returns the only element in the iterable. If the iterable is empty or has | 15 * Returns the only element in the iterable. If the iterable is empty or has |
35 * more than one element, an error is thrown. | 16 * more than one element, an error is thrown. |
36 */ | 17 */ |
37 function getOnlyElement(iterable) { | 18 function getOnlyElement(iterable) { |
38 const iterator = iterable[Symbol.iterator](); | 19 const iterator = iterable[Symbol.iterator](); |
39 | 20 |
40 const firstIteration = iterator.next(); | 21 const firstIteration = iterator.next(); |
41 if (firstIteration.done) { | 22 if (firstIteration.done) { |
42 throw new Error('getOnlyElement was passed an empty iterable.'); | 23 throw new Error('getOnlyElement was passed an empty iterable.'); |
43 } | 24 } |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 if (!(a instanceof Set) || !(b instanceof Set)) return false; | 317 if (!(a instanceof Set) || !(b instanceof Set)) return false; |
337 if (a.size !== b.size) return false; | 318 if (a.size !== b.size) return false; |
338 // Avoid Array.from() here -- it creates garbage. | 319 // Avoid Array.from() here -- it creates garbage. |
339 for (const x of a) { | 320 for (const x of a) { |
340 if (!b.has(x)) return false; | 321 if (!b.has(x)) return false; |
341 } | 322 } |
342 return true; | 323 return true; |
343 } | 324 } |
344 | 325 |
345 return { | 326 return { |
346 asArray, | |
347 concatenateObjects, | 327 concatenateObjects, |
348 compareArrays, | 328 compareArrays, |
349 comparePossiblyUndefinedValues, | 329 comparePossiblyUndefinedValues, |
350 dictionaryContainsValue, | 330 dictionaryContainsValue, |
351 getOnlyElement, | 331 getOnlyElement, |
352 getFirstElement, | 332 getFirstElement, |
353 groupIntoMap, | 333 groupIntoMap, |
354 mapItems, | 334 mapItems, |
355 filterItems, | 335 filterItems, |
356 inPlaceFilter, | 336 inPlaceFilter, |
357 invertArrayOfDicts, | 337 invertArrayOfDicts, |
358 arrayToDict, | 338 arrayToDict, |
359 identity, | 339 identity, |
360 findFirstIndexInArray, | 340 findFirstIndexInArray, |
361 findFirstInArray, | 341 findFirstInArray, |
362 findFirstKeyInDictMatching, | 342 findFirstKeyInDictMatching, |
363 mapValues, | 343 mapValues, |
364 setsEqual, | 344 setsEqual, |
365 }; | 345 }; |
366 }); | 346 }); |
367 </script> | 347 </script> |
OLD | NEW |