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

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

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