|
1 | 1 | import {getHeader} from "../../helpers/har";
|
| 2 | +import { |
| 3 | + formatBytes, formatDateLocalized, formatMilliseconds, parseAndFormat, parseDate, |
| 4 | + parseNonNegative, parsePositive, |
| 5 | +} from "../../helpers/parse"; |
2 | 6 | import {Entry, Header} from "../../typing/har";
|
3 | 7 | import {WaterfallEntry} from "../../typing/waterfall";
|
4 | 8 |
|
5 |
| -function parseAndFormat<S, T>(source?: S, |
6 |
| - parseFn: ((_: S) => T) = identity, |
7 |
| - formatFn: ((_: T) => string) = identity): string { |
8 |
| - if (source === undefined) { |
9 |
| - return undefined; |
10 |
| - } |
11 |
| - const parsed = parseFn(source); |
12 |
| - if (parsed === undefined) { |
13 |
| - return undefined; |
14 |
| - } |
15 |
| - return formatFn(parsed); |
16 |
| -} |
17 |
| - |
18 |
| -function identity<T>(source: T): T { |
19 |
| - return source; |
20 |
| -} |
21 |
| - |
22 |
| -function parseDate(s: string): Date { |
23 |
| - const date = new Date(s); |
24 |
| - if (isNaN(date.getTime())) { |
25 |
| - return undefined; |
26 |
| - } |
27 |
| - return date; |
28 |
| -} |
29 |
| - |
30 |
| -function parseNonNegative(input: string | number): number { |
31 |
| - const criteria = (n: number) => (n < 0); |
32 |
| - return parseToNumber(input, criteria); |
33 |
| -} |
34 |
| - |
35 |
| -function parsePositive(input: string | number): number { |
36 |
| - const criteria = (n: number) => (n <= 0); |
37 |
| - return parseToNumber(input, criteria); |
38 |
| -} |
39 |
| - |
40 |
| -function parseToNumber(input: string | number, criteria: (_: number) => boolean): number { |
41 |
| - const parse = (n: number) => criteria(n) ? undefined : n; |
42 |
| - |
43 |
| - if (typeof input === "string") { |
44 |
| - const n = parseInt(input, 10); |
45 |
| - if (!isFinite(n)) { |
46 |
| - return undefined; |
47 |
| - } |
48 |
| - return parse(n); |
49 |
| - } |
50 |
| - return parse(input); |
51 |
| -} |
52 |
| - |
53 |
| -function formatMilliseconds(millis: number): string { |
54 |
| - return `${millis} ms`; |
55 |
| -} |
56 |
| - |
57 |
| -function formatDateLocalized(d: Date): string { |
58 |
| - return `${d.toUTCString()}</br>(local time: ${d.toLocaleString()})`; |
59 |
| -} |
60 |
| - |
61 |
| -function formatBytes(size: number): string { |
62 |
| - return `${size} byte (~${Math.round(size / 1024 * 10) / 10}kb)`; |
63 |
| -} |
64 |
| - |
65 | 9 | /** get experimental feature (usually WebPageTest) */
|
66 | 10 | let getExp = (harEntry: Entry, name: string): string => {
|
67 | 11 | return harEntry[name] || harEntry["_" + name] || harEntry.request[name] || harEntry.request["_" + name] || "";
|
|
0 commit comments