Skip to content

Commit 2b037a9

Browse files
authored
fix(ocir): replaced formatByteSize implementation (#946)
* fix(ocir): replaced formatByteSize implementation Signed-off-by: Yi Cai <yicai@redhat.com> * updated common size tests Signed-off-by: Yi Cai <yicai@redhat.com> --------- Signed-off-by: Yi Cai <yicai@redhat.com>
1 parent ee1da3b commit 2b037a9

7 files changed

Lines changed: 86 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-openshift-image-registry': patch
3+
---
4+
5+
Dev dependency update.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-openshift-image-registry': patch
3+
---
4+
5+
Replaced internal usage of `formatByteSize` with a local implementation using the `filesize` library, matching the original output format.

workspaces/openshift-image-registry/plugins/openshift-image-registry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@material-ui/core": "^4.9.13",
4646
"@material-ui/icons": "^4.11.3",
4747
"@material-ui/lab": "^4.0.0-alpha.45",
48+
"filesize": "^10.1.6",
4849
"react-use": "^17.4.0"
4950
},
5051
"peerDependencies": {

workspaces/openshift-image-registry/plugins/openshift-image-registry/src/hooks/useImageStreamsMetadataFromTag.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import { useAsync } from 'react-use';
1919

2020
import { useApi } from '@backstage/core-plugin-api';
2121

22-
import { formatByteSize } from '@janus-idp/shared-react';
23-
2422
import { openshiftImageRegistryApiRef } from '../api';
2523
import { ImageStream, ImageStreamMetadata } from '../types';
24+
import { formatByteSize } from '../utils';
2625

2726
export const useImageStreamsMetadataFromTag = (imageStreams: ImageStream[]) => {
2827
const client = useApi(openshiftImageRegistryApiRef);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Red Hat, Inc.
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+
import { formatByteSize } from './utils';
17+
18+
describe('formatByteSize', () => {
19+
it('should return N/A if sizeInBytes is not defined', () => {
20+
expect(formatByteSize(undefined)).toEqual('N/A');
21+
});
22+
23+
it('should return N/A if sizeInBytes is 0', () => {
24+
expect(formatByteSize(0)).toEqual('N/A');
25+
});
26+
27+
it('should format sizeInBytes', () => {
28+
expect(formatByteSize(1)).toEqual('1 B');
29+
expect(formatByteSize(1_000)).toEqual('1 kB');
30+
expect(formatByteSize(1_000_000)).toEqual('1 MB');
31+
expect(formatByteSize(1_000_000_000)).toEqual('1 GB');
32+
expect(formatByteSize(1_000_000_000_000)).toEqual('1 TB');
33+
expect(formatByteSize(1_000_000_000_000_000)).toEqual('1 PB');
34+
expect(formatByteSize(1_000_000_000_000_000_000)).toEqual('1 EB');
35+
expect(formatByteSize(1_000_000_000_000_000_000_000)).toEqual('1 ZB');
36+
expect(formatByteSize(1_000_000_000_000_000_000_000_000)).toEqual('1 YB');
37+
});
38+
39+
it('formats common sizes correctly', () => {
40+
expect(formatByteSize(500)).toEqual('500 B');
41+
expect(formatByteSize(1500)).toMatch('1.5 kB');
42+
expect(formatByteSize(1048576)).toMatch('1.05 MB');
43+
});
44+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Red Hat, Inc.
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+
import { filesize } from 'filesize';
17+
18+
export function formatByteSize(sizeInBytes: number | undefined): string {
19+
if (!sizeInBytes) return 'N/A';
20+
21+
return filesize(sizeInBytes);
22+
}

workspaces/openshift-image-registry/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10312,6 +10312,7 @@ __metadata:
1031210312
"@testing-library/react": 14.3.1
1031310313
"@testing-library/user-event": 14.6.1
1031410314
cross-fetch: 4.1.0
10315+
filesize: ^10.1.6
1031510316
msw: 1.3.5
1031610317
prettier: 3.5.3
1031710318
react-use: ^17.4.0
@@ -20269,6 +20270,13 @@ __metadata:
2026920270
languageName: node
2027020271
linkType: hard
2027120272

20273+
"filesize@npm:^10.1.6":
20274+
version: 10.1.6
20275+
resolution: "filesize@npm:10.1.6"
20276+
checksum: a797a9d41c8f27a9ae334d23f99fc5d903eac5d03c82190dc163901205435b56626fe1260c779ba3e87a2a34d426f19ff264c3f7d956e00f2d3ac69760b52e33
20277+
languageName: node
20278+
linkType: hard
20279+
2027220280
"filesize@npm:^8.0.6":
2027320281
version: 8.0.7
2027420282
resolution: "filesize@npm:8.0.7"

0 commit comments

Comments
 (0)