Skip to content

Commit f00452c

Browse files
authored
chore: deduplicate msToString and bytesToString into isomorphic/formatUtils (#39770)
1 parent fd1b453 commit f00452c

26 files changed

Lines changed: 94 additions & 159 deletions

File tree

packages/html-reporter/src/testCaseView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { statusIcon } from './statusIcon';
2525
import './testCaseView.css';
2626
import { TestResultView } from './testResultView';
2727
import { linkifyText } from '@web/renderUtils';
28-
import { msToString } from './utils';
28+
import { msToString } from '@isomorphic/formatUtils';
2929
import { clsx } from '@web/uiUtils';
3030
import { CopyToClipboardContainer } from './copyToClipboard';
3131
import { HeaderView } from './headerView';

packages/html-reporter/src/testFileView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import type { TestCaseSummary, TestFileSummary } from './types';
1818
import * as React from 'react';
19-
import { msToString } from './utils';
19+
import { msToString } from '@isomorphic/formatUtils';
2020
import { Chip } from './chip';
2121
import { Link, LinkBadge, testResultHref, TraceLink, useSearchParams } from './links';
2222
import { statusIcon } from './statusIcon';

packages/html-reporter/src/testFilesView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as React from 'react';
1919
import { TestFileView } from './testFileView';
2020
import './testFileView.css';
2121
import './chip.css';
22-
import { msToString } from './utils';
22+
import { msToString } from '@isomorphic/formatUtils';
2323
import { Chip } from './chip';
2424
import { CodeSnippet } from './testErrorView';
2525
import * as icons from './icons';

packages/html-reporter/src/testResultView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import type { TestAttachment, TestCase, TestCaseSummary, TestResult, TestStep } from './types';
1818
import * as React from 'react';
1919
import { TreeItem } from './treeItem';
20-
import { formatUrl, msToString } from './utils';
20+
import { formatUrl } from './utils';
21+
import { msToString } from '@isomorphic/formatUtils';
2122
import { AutoChip } from './chip';
2223
import { traceImage } from './images';
2324
import { Anchor, AttachmentLink, generateTraceUrl, testResultHref, useSearchParams } from './links';

packages/html-reporter/src/utils.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,6 @@
1414
limitations under the License.
1515
*/
1616

17-
export function msToString(ms: number): string {
18-
if (!isFinite(ms))
19-
return '-';
20-
21-
if (ms === 0)
22-
return '0ms';
23-
24-
if (ms < 1000)
25-
return ms.toFixed(0) + 'ms';
26-
27-
const seconds = ms / 1000;
28-
if (seconds < 60)
29-
return seconds.toFixed(1) + 's';
30-
31-
const minutes = seconds / 60;
32-
if (minutes < 60)
33-
return minutes.toFixed(1) + 'm';
34-
35-
const hours = minutes / 60;
36-
if (hours < 24)
37-
return hours.toFixed(1) + 'h';
38-
39-
const days = hours / 24;
40-
return days.toFixed(1) + 'd';
41-
}
42-
4317
// hash string to integer in range [0, 6] for color index, to get same color for same tag
4418
export function hashStringToInt(str: string) {
4519
let hash = 0;

packages/playwright-core/src/tools/trace/traceCli.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { TraceModel, buildActionTree } from '../../utils/isomorphic/trace/traceM
2323
import { TraceLoader } from '../../utils/isomorphic/trace/traceLoader';
2424
import { renderTitleForCall } from '../../utils/isomorphic/protocolFormatter';
2525
import { asLocatorDescription } from '../../utils/isomorphic/locatorGenerators';
26+
import { msToString, bytesToString } from '../../utils/isomorphic/formatUtils';
2627
import { ZipTraceLoaderBackend } from './traceParser';
2728

2829
import type { ActionTraceEventInContext } from '@isomorphic/trace/traceModel';
@@ -149,43 +150,6 @@ export async function loadTraceModel(traceFile: string): Promise<TraceModel> {
149150
return (await loadTrace(traceFile)).model;
150151
}
151152

152-
function msToString(ms: number): string {
153-
if (ms < 0 || !isFinite(ms))
154-
return '-';
155-
if (ms === 0)
156-
return '0';
157-
if (ms < 1000)
158-
return ms.toFixed(0) + 'ms';
159-
const seconds = ms / 1000;
160-
if (seconds < 60)
161-
return seconds.toFixed(1) + 's';
162-
const minutes = seconds / 60;
163-
if (minutes < 60)
164-
return minutes.toFixed(1) + 'm';
165-
const hours = minutes / 60;
166-
if (hours < 24)
167-
return hours.toFixed(1) + 'h';
168-
const days = hours / 24;
169-
return days.toFixed(1) + 'd';
170-
}
171-
172-
function bytesToString(bytes: number): string {
173-
if (bytes < 0 || !isFinite(bytes))
174-
return '-';
175-
if (bytes === 0)
176-
return '0';
177-
if (bytes < 1000)
178-
return bytes.toFixed(0);
179-
const kb = bytes / 1024;
180-
if (kb < 1000)
181-
return kb.toFixed(1) + 'K';
182-
const mb = kb / 1024;
183-
if (mb < 1000)
184-
return mb.toFixed(1) + 'M';
185-
const gb = mb / 1024;
186-
return gb.toFixed(1) + 'G';
187-
}
188-
189153
function formatTimestamp(ms: number, base: number): string {
190154
const relative = ms - base;
191155
if (relative < 0)

packages/playwright-core/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export * from './utils/isomorphic/rtti';
2929
export * from './utils/isomorphic/semaphore';
3030
export * from './utils/isomorphic/stackTrace';
3131
export * from './utils/isomorphic/stringUtils';
32+
export * from './utils/isomorphic/formatUtils';
3233
export * from './utils/isomorphic/time';
3334
export * from './utils/isomorphic/timeoutRunner';
3435
export * from './utils/isomorphic/urlMatch';
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export function msToString(ms: number): string {
18+
if (ms < 0 || !isFinite(ms))
19+
return '-';
20+
21+
if (ms === 0)
22+
return '0ms';
23+
24+
if (ms < 1000)
25+
return ms.toFixed(0) + 'ms';
26+
27+
const seconds = ms / 1000;
28+
if (seconds < 60)
29+
return seconds.toFixed(1) + 's';
30+
31+
const minutes = seconds / 60;
32+
if (minutes < 60)
33+
return minutes.toFixed(1) + 'm';
34+
35+
const hours = minutes / 60;
36+
if (hours < 24)
37+
return hours.toFixed(1) + 'h';
38+
39+
const days = hours / 24;
40+
return days.toFixed(1) + 'd';
41+
}
42+
43+
export function bytesToString(bytes: number): string {
44+
if (bytes < 0 || !isFinite(bytes))
45+
return '-';
46+
47+
if (bytes === 0)
48+
return '0';
49+
50+
if (bytes < 1000)
51+
return bytes.toFixed(0);
52+
53+
const kb = bytes / 1024;
54+
if (kb < 1000)
55+
return kb.toFixed(1) + 'K';
56+
57+
const mb = kb / 1024;
58+
if (mb < 1000)
59+
return mb.toFixed(1) + 'M';
60+
61+
const gb = mb / 1024;
62+
return gb.toFixed(1) + 'G';
63+
}

packages/playwright-core/src/utilsBundle.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,3 @@ export const yaml: typeof import('../bundles/utils/node_modules/yaml') = require
3939
export type { Range as YAMLRange, Scalar as YAMLScalar, YAMLError, YAMLMap, YAMLSeq } from '../bundles/utils/node_modules/yaml';
4040
export type { Command } from '../bundles/utils/node_modules/commander';
4141
export type { EventEmitter as WebSocketEventEmitter, RawData as WebSocketRawData, WebSocket, WebSocketServer } from '../bundles/utils/node_modules/@types/ws';
42-
43-
export function ms(ms: number): string {
44-
if (!isFinite(ms))
45-
return '-';
46-
47-
if (ms === 0)
48-
return '0ms';
49-
50-
if (ms < 1000)
51-
return ms.toFixed(0) + 'ms';
52-
53-
const seconds = ms / 1000;
54-
if (seconds < 60)
55-
return seconds.toFixed(1) + 's';
56-
57-
const minutes = seconds / 60;
58-
if (minutes < 60)
59-
return minutes.toFixed(1) + 'm';
60-
61-
const hours = minutes / 60;
62-
if (hours < 24)
63-
return hours.toFixed(1) + 'h';
64-
65-
const days = hours / 24;
66-
return days.toFixed(1) + 'd';
67-
}

packages/playwright/src/reporters/base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import path from 'path';
1818

19-
import { getPackageManagerExecCommand, parseErrorStack } from 'playwright-core/lib/utils';
20-
import { ms as milliseconds } from 'playwright-core/lib/utilsBundle';
19+
import { getPackageManagerExecCommand, msToString as milliseconds, parseErrorStack } from 'playwright-core/lib/utils';
2120
import { colors as realColors, noColors } from 'playwright-core/lib/utils';
2221

2322
import { ansiRegex, resolveReporterOutputPath, stripAnsiEscapes } from '../util';

0 commit comments

Comments
 (0)