File tree 3 files changed +40
-0
lines changed
3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,9 @@ export {
27
27
pluralize ,
28
28
pluralizeToken ,
29
29
slugify ,
30
+ truncateDescription ,
31
+ truncateText ,
32
+ truncateTitle ,
30
33
} from './lib/formatting' ;
31
34
export { getLatestCommit , git } from './lib/git' ;
32
35
export {
Original file line number Diff line number Diff line change
1
+ import { MAX_DESCRIPTION_LENGTH , MAX_TITLE_LENGTH } from '@code-pushup/models' ;
2
+
1
3
export function slugify ( text : string ) : string {
2
4
return text
3
5
. trim ( )
@@ -40,3 +42,19 @@ export function formatDuration(duration: number): string {
40
42
}
41
43
return `${ ( duration / 1000 ) . toFixed ( 2 ) } s` ;
42
44
}
45
+
46
+ export function truncateText ( text : string , maxChars : number ) : string {
47
+ if ( text . length <= maxChars ) {
48
+ return text ;
49
+ }
50
+ const ellipsis = '...' ;
51
+ return text . slice ( 0 , maxChars - ellipsis . length ) + ellipsis ;
52
+ }
53
+
54
+ export function truncateTitle ( text : string ) : string {
55
+ return truncateText ( text , MAX_TITLE_LENGTH ) ;
56
+ }
57
+
58
+ export function truncateDescription ( text : string ) : string {
59
+ return truncateText ( text , MAX_DESCRIPTION_LENGTH ) ;
60
+ }
Original file line number Diff line number Diff line change 5
5
pluralize ,
6
6
pluralizeToken ,
7
7
slugify ,
8
+ truncateText ,
8
9
} from './formatting' ;
9
10
10
11
describe ( 'slugify' , ( ) => {
@@ -78,3 +79,21 @@ describe('formatDuration', () => {
78
79
expect ( formatDuration ( ms ) ) . toBe ( displayValue ) ;
79
80
} ) ;
80
81
} ) ;
82
+
83
+ describe ( 'truncateText' , ( ) => {
84
+ it ( 'should replace overflowing text with ellipsis' , ( ) => {
85
+ expect ( truncateText ( 'All work and no play makes Jack a dull boy' , 32 ) ) . toBe (
86
+ 'All work and no play makes Ja...' ,
87
+ ) ;
88
+ } ) ;
89
+
90
+ it ( 'should produce truncated text which fits within limit' , ( ) => {
91
+ expect (
92
+ truncateText ( 'All work and no play makes Jack a dull boy' , 32 ) . length ,
93
+ ) . toBeLessThanOrEqual ( 32 ) ;
94
+ } ) ;
95
+
96
+ it ( 'should leave text unchanged when within character limit' , ( ) => {
97
+ expect ( truncateText ( "Here's Johnny!" , 32 ) ) . toBe ( "Here's Johnny!" ) ;
98
+ } ) ;
99
+ } ) ;
You can’t perform that action at this time.
0 commit comments