Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: tracing/tracing/base/utils.html

Issue 2995843002: Merge iteration_helpers into utils.html. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tracing/tracing/base/unittest/suite_loader.html ('k') | tracing/tracing/base/utils_test.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 } 181 }
182 182
183 /** 183 /**
184 * @param {string} s 184 * @param {string} s
185 * @return {boolean} 185 * @return {boolean}
186 */ 186 */
187 function isUrl(s) { 187 function isUrl(s) {
188 return typeof(s) === 'string' && s.match(URL_REGEX) !== null; 188 return typeof(s) === 'string' && s.match(URL_REGEX) !== null;
189 } 189 }
190 190
191 /**
192 * Returns the only element in the iterable. If the iterable is empty or has
193 * more than one element, an error is thrown.
194 */
195 function getOnlyElement(iterable) {
196 const iterator = iterable[Symbol.iterator]();
197
198 const firstIteration = iterator.next();
199 if (firstIteration.done) {
200 throw new Error('getOnlyElement was passed an empty iterable.');
201 }
202
203 const secondIteration = iterator.next();
204 if (!secondIteration.done) {
205 throw new Error(
206 'getOnlyElement was passed an iterable with multiple elements.');
207 }
208
209 return firstIteration.value;
210 }
211
212 /**
213 * Returns the first element in the iterable. If the iterable is empty, an
214 * error is thrown.
215 */
216 function getFirstElement(iterable) {
217 const iterator = iterable[Symbol.iterator]();
218 const result = iterator.next();
219 if (result.done) {
220 throw new Error('getFirstElement was passed an empty iterable.');
221 }
222
223 return result.value;
224 }
225
226 function compareArrays(x, y, elementCmp) {
227 const minLength = Math.min(x.length, y.length);
228 let i;
229 for (i = 0; i < minLength; i++) {
230 const tmp = elementCmp(x[i], y[i]);
231 if (tmp) return tmp;
232 }
233 if (x.length === y.length) return 0;
234
235 if (x[i] === undefined) return -1;
236
237 return 1;
238 }
239
240 /**
241 * Returns a new Map with items grouped by the return value of the
242 * specified function being called on each item.
243 * @param {!Array.<!*>} ary The array being iterated through
244 * @param {!function(!*):!*} callback The mapping function between the array
245 * value and the map key.
246 * @param {*=} opt_this
247 */
248 function groupIntoMap(ary, callback, opt_this, opt_arrayConstructor) {
249 const arrayConstructor = opt_arrayConstructor || Array;
250 const results = new Map();
251 for (const element of ary) {
252 const key = callback.call(opt_this, element);
253 let items = results.get(key);
254 if (items === undefined) {
255 items = new arrayConstructor();
256 results.set(key, items);
257 }
258 items.push(element);
259 }
260 return results;
261 }
262
263 function inPlaceFilter(array, predicate, opt_this) {
264 opt_this = opt_this || this;
265 let nextPosition = 0;
266 for (let i = 0; i < array.length; i++) {
267 if (!predicate.call(opt_this, array[i], i)) continue;
268 if (nextPosition < i) {
269 array[nextPosition] = array[i]; // Move elements only if necessary.
270 }
271 nextPosition++;
272 }
273
274 if (nextPosition < array.length) {
275 array.length = nextPosition; // Truncate the array only if necessary.
276 }
277 }
278
279 /**
280 * Convert an array of dictionaries to a dictionary of arrays.
281 *
282 * The keys of the resulting dictionary are a union of the keys of all
283 * dictionaries in the provided array. Each array in the resulting dictionary
284 * has the same length as the provided array and contains the values of its
285 * key in the dictionaries in the provided array. Example:
286 *
287 * INPUT:
288 *
289 * [
290 * {a: 6, b: 5 },
291 * undefined,
292 * {a: 4, b: 3, c: 2},
293 * { b: 1, c: 0}
294 * ]
295 *
296 * OUTPUT:
297 *
298 * {
299 * a: [6, undefined, 4, undefined],
300 * b: [5, undefined, 3, 1 ],
301 * c: [undefined, undefined, 2, 0 ]
302 * }
303 *
304 * @param {!Array} array Array of items to be inverted. If opt_dictGetter
305 * is not provided, all elements of the array must be either undefined,
306 * or dictionaries.
307 * @param {?(function(*): (!Object|undefined))=} opt_dictGetter Optional
308 * function mapping defined elements of array to dictionaries.
309 * @param {*=} opt_this Optional 'this' context for opt_dictGetter.
310 */
311 function invertArrayOfDicts(array, opt_dictGetter, opt_this) {
312 opt_this = opt_this || this;
313 const result = {};
314 for (let i = 0; i < array.length; i++) {
315 const item = array[i];
316 if (item === undefined) continue;
317 const dict = opt_dictGetter ? opt_dictGetter.call(opt_this, item) : item;
318 if (dict === undefined) continue;
319 for (const key in dict) {
320 let valueList = result[key];
321 if (valueList === undefined) {
322 result[key] = valueList = new Array(array.length);
323 }
324 valueList[i] = dict[key];
325 }
326 }
327 return result;
328 }
329
330 function setsEqual(a, b) {
331 if (!(a instanceof Set) || !(b instanceof Set)) return false;
332 if (a.size !== b.size) return false;
333 // Avoid Array.from() here -- it creates garbage.
334 for (const x of a) {
335 if (!b.has(x)) return false;
336 }
337 return true;
338 }
339
191 return { 340 return {
192 addSingletonGetter, 341 addSingletonGetter,
342 compareArrays,
193 deepCopy, 343 deepCopy,
194 formatDate, 344 formatDate,
345 getFirstElement,
346 getOnlyElement,
195 getUsingPath, 347 getUsingPath,
348 groupIntoMap,
349 inPlaceFilter,
350 invertArrayOfDicts,
196 isUrl, 351 isUrl,
197 normalizeException, 352 normalizeException,
198 numberFromJson, 353 numberFromJson,
199 numberToJson, 354 numberToJson,
200 runLengthEncoding, 355 runLengthEncoding,
356 setsEqual,
201 stackTrace, 357 stackTrace,
202 stackTraceAsString, 358 stackTraceAsString,
203 }; 359 };
204 }); 360 });
205 </script> 361 </script>
OLDNEW
« no previous file with comments | « tracing/tracing/base/unittest/suite_loader.html ('k') | tracing/tracing/base/utils_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698