forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
94 lines (84 loc) · 2.81 KB
/
util.ts
File metadata and controls
94 lines (84 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/* tslint:disable:no-console */
urlParamsToForm();
export function getIntParameter(name: string) {
return parseInt(getStringParameter(name), 10);
}
export function getStringParameter(name: string) {
const els = document.querySelectorAll(`input[name="${name}"]`);
let value: any;
let el: any;
for (let i = 0; i < els.length; i++) {
el = els[i];
const type = el.type;
if ((type != 'radio' && type != 'checkbox') || el.checked) {
value = el.value;
break;
}
}
if (value == null) {
throw new Error(`Could not find and input field with name ${name}`);
}
return value;
}
export function bindAction(selector: string, callback: () => void) {
document.querySelector(selector)!.addEventListener('click', callback);
}
export function profile(create: () => void, destroy: () => void, name: string) {
return function () {
// 'console.profile' is experimental and was removed from DOM lib in TS 3.9
(window.console as any).profile(name);
const noOfRuns = 150;
let durations: number[] = [];
let count = 0;
while (count++ < noOfRuns) {
const start = window.performance.now();
create();
const end = window.performance.now() - start;
durations.push(end);
destroy();
}
// 'console.profileEnd' is experimental and was removed from DOM lib in TS 3.9
(window.console as any).profileEnd();
reportProfileResults(durations, noOfRuns);
};
}
function reportProfileResults(durations: number[], count: number) {
const totalDuration = durations.reduce((soFar: number, duration: number) => soFar + duration, 0);
const avgDuration = (totalDuration / count).toFixed(2);
const minDuration = durations
.reduce((soFar: number, duration: number) => Math.min(soFar, duration), Number.MAX_SAFE_INTEGER)
.toFixed(2);
window.console.log(
`Iterations: ${count}; cold time: ${durations[0].toFixed(
2,
)} ms; average time: ${avgDuration} ms / iteration; best time: ${minDuration} ms`,
);
}
// helper script that will read out the url parameters
// and store them in appropriate form fields on the page
function urlParamsToForm() {
const regex = /(\w+)=(\w+)/g;
const search = decodeURIComponent(location.search);
let match: any[] | null;
while ((match = regex.exec(search))) {
const name = match[1];
const value = match[2];
const els = document.querySelectorAll('input[name="' + name + '"]');
let el: any;
for (let i = 0; i < els.length; i++) {
el = els[i];
if (el.type === 'radio' || el.type === 'checkbox') {
el.checked = el.value === value;
} else {
el.value = value;
}
}
}
}