Skip to content

Commit 9e13a8b

Browse files
committed
fix(@angular/build): format sizes using decimal byte units consistently
Ensure that file sizes are consistently formatted using decimal byte units, adhering to the International System of Units (SI) convention. This aligns with clarity and standardization across the project. - Kilobyte (kB): 10^3 bytes (1000 bytes) - Megabyte (MB): 10^6 bytes (1,000,000 bytes) - Gigabyte (GB): 10^9 bytes (1,000,000,000 bytes) Closes: #27580
1 parent 1d2f642 commit 9e13a8b

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

packages/angular/build/src/utils/bundle-calculator_spec.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { BudgetEntry, BudgetType, ThresholdSeverity, checkBudgets } from './bundle-calculator';
1010

11-
const KB = 1024;
11+
const kB = 1000;
1212

1313
describe('bundle-calculator', () => {
1414
describe('checkBudgets()', () => {
@@ -24,11 +24,11 @@ describe('bundle-calculator', () => {
2424
assets: [
2525
{
2626
name: 'foo.js',
27-
size: 1.5 * KB,
27+
size: 1.5 * kB,
2828
},
2929
{
3030
name: 'bar.js',
31-
size: 0.5 * KB,
31+
size: 0.5 * kB,
3232
},
3333
],
3434
};
@@ -55,11 +55,11 @@ describe('bundle-calculator', () => {
5555
assets: [
5656
{
5757
name: 'foo.js',
58-
size: 1.5 * KB,
58+
size: 1.5 * kB,
5959
},
6060
{
6161
name: 'bar.js',
62-
size: 0.5 * KB,
62+
size: 0.5 * kB,
6363
},
6464
],
6565
};
@@ -93,11 +93,11 @@ describe('bundle-calculator', () => {
9393
assets: [
9494
{
9595
name: 'foo.js',
96-
size: 0.75 * KB,
96+
size: 0.75 * kB,
9797
},
9898
{
9999
name: 'bar.js',
100-
size: 0.75 * KB,
100+
size: 0.75 * kB,
101101
},
102102
],
103103
};
@@ -131,11 +131,11 @@ describe('bundle-calculator', () => {
131131
assets: [
132132
{
133133
name: 'foo.js',
134-
size: 0.5 * KB,
134+
size: 0.5 * kB,
135135
},
136136
{
137137
name: 'bar.js',
138-
size: 0.75 * KB,
138+
size: 0.75 * kB,
139139
},
140140
],
141141
};
@@ -169,15 +169,15 @@ describe('bundle-calculator', () => {
169169
assets: [
170170
{
171171
name: 'foo.js',
172-
size: 0.75 * KB,
172+
size: 0.75 * kB,
173173
},
174174
{
175175
name: 'bar.js',
176-
size: 0.75 * KB,
176+
size: 0.75 * kB,
177177
},
178178
{
179179
name: 'baz.css',
180-
size: 1.5 * KB,
180+
size: 1.5 * kB,
181181
},
182182
],
183183
};
@@ -211,11 +211,11 @@ describe('bundle-calculator', () => {
211211
assets: [
212212
{
213213
name: 'foo.js',
214-
size: 0.75 * KB,
214+
size: 0.75 * kB,
215215
},
216216
{
217217
name: 'bar.css',
218-
size: 0.75 * KB,
218+
size: 0.75 * kB,
219219
},
220220
],
221221
};
@@ -249,11 +249,11 @@ describe('bundle-calculator', () => {
249249
assets: [
250250
{
251251
name: 'foo.css',
252-
size: 1.5 * KB,
252+
size: 1.5 * kB,
253253
},
254254
{
255255
name: 'bar.js',
256-
size: 0.5 * KB,
256+
size: 0.5 * kB,
257257
},
258258
],
259259
};
@@ -282,11 +282,11 @@ describe('bundle-calculator', () => {
282282
assets: [
283283
{
284284
name: 'foo.js',
285-
size: 1.5 * KB,
285+
size: 1.5 * kB,
286286
},
287287
{
288288
name: 'bar.js',
289-
size: 0.5 * KB,
289+
size: 0.5 * kB,
290290
},
291291
],
292292
};
@@ -320,11 +320,11 @@ describe('bundle-calculator', () => {
320320
assets: [
321321
{
322322
name: 'foo.ext',
323-
size: 1.5 * KB,
323+
size: 1.5 * kB,
324324
},
325325
{
326326
name: 'bar.ext',
327-
size: 0.5 * KB,
327+
size: 0.5 * kB,
328328
},
329329
],
330330
};

packages/angular/build/src/utils/format-bytes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function formatSize(size: number): string {
1212
}
1313

1414
const abbreviations = ['bytes', 'kB', 'MB', 'GB'];
15-
const index = Math.floor(Math.log(size) / Math.log(1024));
16-
const roundedSize = size / Math.pow(1024, index);
15+
const index = Math.floor(Math.log(size) / Math.log(1000));
16+
const roundedSize = size / Math.pow(1000, index);
1717
// bytes don't have a fraction
1818
const fractionDigits = index === 0 ? 0 : 2;
1919

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { formatSize } from './format-bytes';
10+
11+
describe('formatSize', () => {
12+
it('1000 bytes to be 1kB', () => {
13+
expect(formatSize(1000)).toBe('1.00 kB');
14+
});
15+
16+
it('1_000_000 bytes to be 1MB', () => {
17+
expect(formatSize(1_000_000)).toBe('1.00 MB');
18+
});
19+
20+
it('1_500_000 bytes to be 1.5MB', () => {
21+
expect(formatSize(1_500_000)).toBe('1.50 MB');
22+
});
23+
24+
it('1_000_000_000 bytes to be 1GB', () => {
25+
expect(formatSize(1_000_000_000)).toBe('1.00 GB');
26+
});
27+
});

packages/schematics/angular/application/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,31 @@ function addAppToWorkspaceFile(
194194
}
195195

196196
const sourceRoot = join(normalize(projectRoot), 'src');
197-
let budgets = [];
197+
let budgets: { type: string; maximumWarning: string; maximumError: string }[] = [];
198198
if (options.strict) {
199199
budgets = [
200200
{
201201
type: 'initial',
202-
maximumWarning: '500kb',
203-
maximumError: '1mb',
202+
maximumWarning: '500kB',
203+
maximumError: '1MB',
204204
},
205205
{
206206
type: 'anyComponentStyle',
207-
maximumWarning: '2kb',
208-
maximumError: '4kb',
207+
maximumWarning: '2kB',
208+
maximumError: '4kB',
209209
},
210210
];
211211
} else {
212212
budgets = [
213213
{
214214
type: 'initial',
215-
maximumWarning: '2mb',
216-
maximumError: '5mb',
215+
maximumWarning: '2MB',
216+
maximumError: '5MB',
217217
},
218218
{
219219
type: 'anyComponentStyle',
220-
maximumWarning: '6kb',
221-
maximumError: '10kb',
220+
maximumWarning: '6kB',
221+
maximumError: '10kB',
222222
},
223223
];
224224
}

0 commit comments

Comments
 (0)