File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Truncates a string value to the specified length with an ellipsis.
3
+ * @param {string } value - The value to truncate.
4
+ * @param {number } maxLength - The maximum length of the value.
5
+ * @return {string } A truncated string with ellipsis if the value's length is greater than the max length.
6
+ */
7
+ export function truncate ( value : string , maxLength : number ) : string {
8
+ if ( isNullOrWhiteSpace ( value ) || value . length <= maxLength ) {
9
+ return value ;
10
+ }
11
+
12
+ const suffix = "..." ;
13
+ return value . substring ( 0 , maxLength - suffix . length ) + suffix ;
14
+ }
15
+
1
16
/**
2
17
* Converts a string value to a number value.
3
18
* @param {string } value - The string value to convert.
Original file line number Diff line number Diff line change
1
+ import { truncate } from '../../src'
2
+
3
+ describe ( "when truncating a string" , ( ) => {
4
+ it ( "should return a truncated string if greater than max length" , ( ) => {
5
+ // Arrange
6
+ const input = "Hello, World!"
7
+
8
+ // Act
9
+ const result = truncate ( input , 8 ) ;
10
+
11
+ // Assert
12
+ expect ( result ) . toBe ( "Hello..." ) ;
13
+ } ) ;
14
+
15
+ it ( "should return original string if less than max length" , ( ) => {
16
+ // Arrange
17
+ const input = "Hello, World!"
18
+
19
+ // Act
20
+ const result = truncate ( input , input . length ) ;
21
+
22
+ // Assert
23
+ expect ( result ) . toBe ( input ) ;
24
+ } ) ;
25
+ } ) ;
You can’t perform that action at this time.
0 commit comments