Skip to content

Commit e5908e3

Browse files
authored
client(common): data-utils functions (#31961)
1 parent 978f508 commit e5908e3

File tree

9 files changed

+127
-78
lines changed

9 files changed

+127
-78
lines changed

generators/angular/__snapshots__/generator.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ exports[`generator - angular gateway-jwt-skipUserManagement(true)-withAdminUi(fa
803803
"clientRoot/src/app/shared/jhipster/constants.ts": {
804804
"stateCleared": "modified",
805805
},
806+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
807+
"stateCleared": "modified",
808+
},
806809
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
807810
"stateCleared": "modified",
808811
},
@@ -2033,6 +2036,9 @@ exports[`generator - angular gateway-oauth2-withAdminUi(true)-skipJhipsterDepend
20332036
"clientRoot/src/app/shared/jhipster/constants.ts": {
20342037
"stateCleared": "modified",
20352038
},
2039+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
2040+
"stateCleared": "modified",
2041+
},
20362042
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
20372043
"stateCleared": "modified",
20382044
},
@@ -3041,6 +3047,9 @@ exports[`generator - angular microservice-jwt-skipUserManagement(false)-withAdmi
30413047
"clientRoot/src/app/shared/jhipster/constants.ts": {
30423048
"stateCleared": "modified",
30433049
},
3050+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
3051+
"stateCleared": "modified",
3052+
},
30443053
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
30453054
"stateCleared": "modified",
30463055
},
@@ -4055,6 +4064,9 @@ exports[`generator - angular microservice-oauth2-withAdminUi(true)-skipJhipsterD
40554064
"src/main/webapp/app/shared/jhipster/constants.ts": {
40564065
"stateCleared": "modified",
40574066
},
4067+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
4068+
"stateCleared": "modified",
4069+
},
40584070
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
40594071
"stateCleared": "modified",
40604072
},
@@ -5504,6 +5516,9 @@ exports[`generator - angular monolith-jwt-skipUserManagement(false)-withAdminUi(
55045516
"src/main/webapp/app/shared/jhipster/constants.ts": {
55055517
"stateCleared": "modified",
55065518
},
5519+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
5520+
"stateCleared": "modified",
5521+
},
55075522
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
55085523
"stateCleared": "modified",
55095524
},
@@ -6512,6 +6527,9 @@ exports[`generator - angular monolith-oauth2-withAdminUi(false)-skipJhipsterDepe
65126527
"src/main/webapp/app/shared/jhipster/constants.ts": {
65136528
"stateCleared": "modified",
65146529
},
6530+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
6531+
"stateCleared": "modified",
6532+
},
65156533
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
65166534
"stateCleared": "modified",
65176535
},
@@ -7507,6 +7525,9 @@ exports[`generator - angular monolith-session-skipUserManagement(true)-withAdmin
75077525
"src/main/webapp/app/shared/jhipster/constants.ts": {
75087526
"stateCleared": "modified",
75097527
},
7528+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
7529+
"stateCleared": "modified",
7530+
},
75107531
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
75117532
"stateCleared": "modified",
75127533
},

generators/angular/templates/src/main/webapp/app/core/util/data-util.service.ts.ejs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { Injectable } from '@angular/core';
2020
import { FormGroup } from '@angular/forms';
2121
import { Observable, Observer } from 'rxjs';
2222

23+
import { formatAsBytes, paddingSize, size, toBase64 } from 'app/shared/jhipster/data-utils';
24+
2325
export type FileLoadErrorType = 'not.image' | 'could.not.extract';
2426

2527
export interface FileLoadError {
@@ -39,7 +41,7 @@ export class DataUtils {
3941
* Method to find the byte size of the string provides
4042
*/
4143
byteSize(base64String: string): string {
42-
return this.formatAsBytes(this.size(base64String));
44+
return formatAsBytes(size(base64String));
4345
}
4446

4547
/**
@@ -89,7 +91,7 @@ export class DataUtils {
8991
observer.error(error);
9092
} else {
9193
const fieldContentType = `${field}ContentType`;
92-
this.toBase64(file, (base64Data: string) => {
94+
toBase64(file, (base64Data: string) => {
9395
editForm.patchValue({
9496
[field]: base64Data,
9597
[fieldContentType]: file.type,
@@ -108,40 +110,4 @@ export class DataUtils {
108110
}
109111
});
110112
}
111-
112-
/**
113-
* Method to convert the file to base64
114-
*/
115-
private toBase64(file: File, callback: (base64Data: string) => void): void {
116-
const fileReader: FileReader = new FileReader();
117-
fileReader.onload = (e: ProgressEvent<FileReader>) => {
118-
if (typeof e.target?.result === 'string') {
119-
const base64Data: string = e.target.result.substring(e.target.result.indexOf('base64,') + 'base64,'.length);
120-
callback(base64Data);
121-
}
122-
};
123-
fileReader.readAsDataURL(file);
124-
}
125-
126-
private endsWith(suffix: string, str: string): boolean {
127-
return str.includes(suffix, str.length - suffix.length);
128-
}
129-
130-
private paddingSize(value: string): number {
131-
if (this.endsWith('==', value)) {
132-
return 2;
133-
}
134-
if (this.endsWith('=', value)) {
135-
return 1;
136-
}
137-
return 0;
138-
}
139-
140-
private size(value: string): number {
141-
return (value.length / 4) * 3 - this.paddingSize(value);
142-
}
143-
144-
private formatAsBytes(size: number): string {
145-
return `${size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')} bytes`; // NOSONAR
146-
}
147113
}

generators/client/generators/common/__snapshots__/generator.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ exports[`generator - client:common with defaults options should match files snap
1010
"src/main/webapp/app/shared/jhipster/constants.ts": {
1111
"stateCleared": "modified",
1212
},
13+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
14+
"stateCleared": "modified",
15+
},
1316
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
1417
"stateCleared": "modified",
1518
},

generators/client/generators/common/generator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default class CommonGenerator extends BaseApplicationGenerator {
4141
'shared/jhipster/problem-details.ts',
4242
'shared/jhipster/headers.ts',
4343
'shared/jhipster/error.constants.ts',
44+
'shared/jhipster/data-utils.ts',
4445
],
4546
}),
4647
],
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<%#
2+
Copyright 2013-2026 the original author or authors from the JHipster project.
3+
4+
This file is part of the JHipster project, see https://www.jhipster.tech/
5+
for more information.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
https://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-%>
19+
export const formatAsBytes = (size: number): string =>
20+
`${size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')} bytes`; // NOSONAR
21+
22+
export const endsWith = (suffix: string, str: string): boolean =>
23+
str.includes(suffix, str.length - suffix.length);
24+
25+
export const paddingSize = (value: string): number => {
26+
if (endsWith('==', value)) {
27+
return 2;
28+
}
29+
if (endsWith('=', value)) {
30+
return 1;
31+
}
32+
return 0;
33+
}
34+
35+
export const size = (value: string): number =>
36+
(value.length / 4) * 3 - paddingSize(value);
37+
38+
export const toBase64 = (file: File, callback: (base64Data: string) => void): void => {
39+
const fileReader: FileReader = new FileReader();
40+
fileReader.onload = (e: ProgressEvent<FileReader>) => {
41+
if (typeof e.target?.result === 'string') {
42+
const base64Data: string = e.target.result.substring(e.target.result.indexOf('base64,') + 'base64,'.length);
43+
callback(base64Data);
44+
}
45+
};
46+
fileReader.readAsDataURL(file);
47+
}

generators/react/__snapshots__/generator.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ exports[`generator - react gateway-jwt-skipUserManagement(true)-withAdminUi(fals
505505
"clientRoot/src/app/shared/jhipster/constants.ts": {
506506
"stateCleared": "modified",
507507
},
508+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
509+
"stateCleared": "modified",
510+
},
508511
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
509512
"stateCleared": "modified",
510513
},
@@ -1286,6 +1289,9 @@ exports[`generator - react gateway-oauth2-withAdminUi(true)-skipJhipsterDependen
12861289
"clientRoot/src/app/shared/jhipster/constants.ts": {
12871290
"stateCleared": "modified",
12881291
},
1292+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
1293+
"stateCleared": "modified",
1294+
},
12891295
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
12901296
"stateCleared": "modified",
12911297
},
@@ -2069,6 +2075,9 @@ exports[`generator - react microservice-jwt-skipUserManagement(false)-withAdminU
20692075
"clientRoot/src/app/shared/jhipster/constants.ts": {
20702076
"stateCleared": "modified",
20712077
},
2078+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
2079+
"stateCleared": "modified",
2080+
},
20722081
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
20732082
"stateCleared": "modified",
20742083
},
@@ -2850,6 +2859,9 @@ exports[`generator - react microservice-oauth2-withAdminUi(true)-skipJhipsterDep
28502859
"src/main/webapp/app/shared/jhipster/constants.ts": {
28512860
"stateCleared": "modified",
28522861
},
2862+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
2863+
"stateCleared": "modified",
2864+
},
28532865
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
28542866
"stateCleared": "modified",
28552867
},
@@ -3729,6 +3741,9 @@ exports[`generator - react monolith-jwt-skipUserManagement(false)-withAdminUi(tr
37293741
"src/main/webapp/app/shared/jhipster/constants.ts": {
37303742
"stateCleared": "modified",
37313743
},
3744+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
3745+
"stateCleared": "modified",
3746+
},
37323747
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
37333748
"stateCleared": "modified",
37343749
},
@@ -4491,6 +4506,9 @@ exports[`generator - react monolith-oauth2-withAdminUi(false)-skipJhipsterDepend
44914506
"src/main/webapp/app/shared/jhipster/constants.ts": {
44924507
"stateCleared": "modified",
44934508
},
4509+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
4510+
"stateCleared": "modified",
4511+
},
44944512
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
44954513
"stateCleared": "modified",
44964514
},
@@ -5256,6 +5274,9 @@ exports[`generator - react monolith-session-skipUserManagement(true)-withAdminUi
52565274
"src/main/webapp/app/shared/jhipster/constants.ts": {
52575275
"stateCleared": "modified",
52585276
},
5277+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
5278+
"stateCleared": "modified",
5279+
},
52595280
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
52605281
"stateCleared": "modified",
52615282
},

generators/vue/__snapshots__/generator.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,9 @@ exports[`generator - vue gateway-jwt-skipUserManagement(true)-withAdminUi(false)
589589
"clientRoot/src/app/shared/jhipster/constants.ts": {
590590
"stateCleared": "modified",
591591
},
592+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
593+
"stateCleared": "modified",
594+
},
592595
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
593596
"stateCleared": "modified",
594597
},
@@ -1469,6 +1472,9 @@ exports[`generator - vue gateway-oauth2-withAdminUi(true)-skipJhipsterDependenci
14691472
"clientRoot/src/app/shared/jhipster/constants.ts": {
14701473
"stateCleared": "modified",
14711474
},
1475+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
1476+
"stateCleared": "modified",
1477+
},
14721478
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
14731479
"stateCleared": "modified",
14741480
},
@@ -2273,6 +2279,9 @@ exports[`generator - vue microservice-jwt-skipUserManagement(false)-withAdminUi(
22732279
"clientRoot/src/app/shared/jhipster/constants.ts": {
22742280
"stateCleared": "modified",
22752281
},
2282+
"clientRoot/src/app/shared/jhipster/data-utils.ts": {
2283+
"stateCleared": "modified",
2284+
},
22762285
"clientRoot/src/app/shared/jhipster/error.constants.ts": {
22772286
"stateCleared": "modified",
22782287
},
@@ -3118,6 +3127,9 @@ exports[`generator - vue microservice-oauth2-withAdminUi(true)-skipJhipsterDepen
31183127
"src/main/webapp/app/shared/jhipster/constants.ts": {
31193128
"stateCleared": "modified",
31203129
},
3130+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
3131+
"stateCleared": "modified",
3132+
},
31213133
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
31223134
"stateCleared": "modified",
31233135
},
@@ -4123,6 +4135,9 @@ exports[`generator - vue monolith-jwt-skipUserManagement(false)-withAdminUi(true
41234135
"src/main/webapp/app/shared/jhipster/constants.ts": {
41244136
"stateCleared": "modified",
41254137
},
4138+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
4139+
"stateCleared": "modified",
4140+
},
41264141
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
41274142
"stateCleared": "modified",
41284143
},
@@ -4903,6 +4918,9 @@ exports[`generator - vue monolith-oauth2-withAdminUi(false)-skipJhipsterDependen
49034918
"src/main/webapp/app/shared/jhipster/constants.ts": {
49044919
"stateCleared": "modified",
49054920
},
4921+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
4922+
"stateCleared": "modified",
4923+
},
49064924
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
49074925
"stateCleared": "modified",
49084926
},
@@ -5698,6 +5716,9 @@ exports[`generator - vue monolith-session-skipUserManagement(true)-withAdminUi(f
56985716
"src/main/webapp/app/shared/jhipster/constants.ts": {
56995717
"stateCleared": "modified",
57005718
},
5719+
"src/main/webapp/app/shared/jhipster/data-utils.ts": {
5720+
"stateCleared": "modified",
5721+
},
57015722
"src/main/webapp/app/shared/jhipster/error.constants.ts": {
57025723
"stateCleared": "modified",
57035724
},

generators/vue/templates/src/main/webapp/app/shared/data/data-utils.service.spec.ts.ejs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { vitest } from 'vitest';
22
import useDataUtils from './data-utils.service';
3+
import { endsWith, paddingSize } from '@/shared/jhipster/data-utils';
34

45
describe('Formatter i18n', () => {
56
let dataUtilsService: ReturnType<typeof useDataUtils>;
@@ -47,25 +48,25 @@ describe('Formatter i18n', () => {
4748
});
4849

4950
it('should check text ends with suffix', () => {
50-
const result = dataUtilsService.endsWith('rocky', 'JHipster rocks!');
51+
const result = endsWith('rocky', 'JHipster rocks!');
5152

5253
expect(result).toBe(false);
5354
});
5455

5556
it('should paddingSize to 0', () => {
56-
const result = dataUtilsService.paddingSize('toto');
57+
const result = paddingSize('toto');
5758

5859
expect(result).toBe(0);
5960
});
6061

6162
it('should paddingSize to 1', () => {
62-
const result = dataUtilsService.paddingSize('toto=');
63+
const result = paddingSize('toto=');
6364

6465
expect(result).toBe(1);
6566
});
6667

6768
it('should paddingSize to 2', () => {
68-
const result = dataUtilsService.paddingSize('toto==');
69+
const result = paddingSize('toto==');
6970

7071
expect(result).toBe(2);
7172
});

0 commit comments

Comments
 (0)