Skip to content

Commit 181c8a6

Browse files
committed
Adding Text.padStart function.
1 parent 54235d3 commit 181c8a6

File tree

2 files changed

+99
-26
lines changed

2 files changed

+99
-26
lines changed

libraries/node-core-library/src/Text.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,43 @@ export class Text {
6969
}
7070

7171
/**
72-
* Append spaces to the end of a string to ensure the result has a minimum length.
72+
* Append characters to the end of a string to ensure the result has a minimum length.
7373
* @remarks
7474
* If the string length already exceeds the minimum length, then the string is unchanged.
7575
* The string is not truncated.
7676
*/
77-
public static padEnd(s: string, minimumLength: number): string {
78-
let result: string = s;
79-
while (result.length < minimumLength) {
80-
result += ' ';
77+
public static padEnd(s: string, minimumLength: number, paddingCharacter: string = ' '): string {
78+
if (paddingCharacter.length !== 1) {
79+
throw new Error('The paddingCharacter parameter must be a single character.');
80+
}
81+
82+
if (s.length < minimumLength) {
83+
const paddingArray: string[] = new Array(minimumLength - s.length);
84+
paddingArray.unshift(s);
85+
return paddingArray.join(paddingCharacter);
86+
} else {
87+
return s;
88+
}
89+
}
90+
91+
/**
92+
* Append characters to the start of a string to ensure the result has a minimum length.
93+
* @remarks
94+
* If the string length already exceeds the minimum length, then the string is unchanged.
95+
* The string is not truncated.
96+
*/
97+
public static padStart(s: string, minimumLength: number, paddingCharacter: string = ' '): string {
98+
if (paddingCharacter.length !== 1) {
99+
throw new Error('The paddingCharacter parameter must be a single character.');
100+
}
101+
102+
if (s.length < minimumLength) {
103+
const paddingArray: string[] = new Array(minimumLength - s.length);
104+
paddingArray.push(s);
105+
return paddingArray.join(paddingCharacter);
106+
} else {
107+
return s;
81108
}
82-
return result;
83109
}
84110

85111
/**

libraries/node-core-library/src/test/Text.test.ts

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,73 @@
44
import { Text } from '../Text';
55

66
describe('Text', () => {
7-
it('Text.padEnd()', () => {
8-
expect(Text.padEnd('', 5)).toEqual( ' ');
9-
expect(Text.padEnd('123', 5)).toEqual( '123 ');
10-
expect(Text.padEnd('12345', 5)).toEqual( '12345');
11-
expect(Text.padEnd('123456', 5)).toEqual('123456');
7+
describe('padEnd', () => {
8+
it('Throws an exception if the padding character isn\'t a single character', () => {
9+
expect(() => Text.padEnd('123', 1, '')).toThrow();
10+
expect(() => Text.padEnd('123', 1, ' ')).toThrow();
11+
});
12+
13+
it('Doesn\'t change the string if it\'s already at or greater than the minimum length', () => {
14+
expect(Text.padEnd('12345', 5)).toEqual( '12345');
15+
expect(Text.padEnd('123456', 5)).toEqual('123456');
16+
expect(Text.padEnd('12345', 5, '0')).toEqual( '12345');
17+
expect(Text.padEnd('123456', 5, '0')).toEqual('123456');
18+
});
19+
20+
it('Appends the default character (spaces) to the end of a string', () => {
21+
expect(Text.padEnd('', 5)).toEqual( ' ');
22+
expect(Text.padEnd('123', 5)).toEqual('123 ');
23+
});
24+
25+
it('Appends the characters to the end of a string', () => {
26+
expect(Text.padEnd('', 5, '0')).toEqual( '00000');
27+
expect(Text.padEnd('123', 5, '0')).toEqual('12300');
28+
});
29+
});
30+
31+
describe('padStart', () => {
32+
it('Throws an exception if the padding character isn\'t a single character', () => {
33+
expect(() => Text.padStart('123', 1, '')).toThrow();
34+
expect(() => Text.padStart('123', 1, ' ')).toThrow();
35+
});
36+
37+
it('Doesn\'t change the string if it\'s already at or greater than the minimum length', () => {
38+
expect(Text.padStart('12345', 5)).toEqual( '12345');
39+
expect(Text.padStart('123456', 5)).toEqual('123456');
40+
expect(Text.padStart('12345', 5, '0')).toEqual( '12345');
41+
expect(Text.padStart('123456', 5, '0')).toEqual('123456');
42+
});
43+
44+
it('Appends the default character (spaces) to the end of a string', () => {
45+
expect(Text.padStart('', 5)).toEqual( ' ');
46+
expect(Text.padStart('123', 5)).toEqual(' 123');
47+
});
48+
49+
it('Appends the characters to the end of a string', () => {
50+
expect(Text.padStart('', 5, '0')).toEqual( '00000');
51+
expect(Text.padStart('123', 5, '0')).toEqual('00123');
52+
});
1253
});
13-
it('Text.truncateWithEllipsis()', () => {
14-
expect(() => {
15-
Text.truncateWithEllipsis('123', -1);
16-
}).toThrow();
17-
expect(Text.truncateWithEllipsis('123', 0)).toEqual( '');
18-
19-
expect(Text.truncateWithEllipsis('', 2)).toEqual( '');
20-
expect(Text.truncateWithEllipsis('1', 2)).toEqual( '1');
21-
expect(Text.truncateWithEllipsis('12', 2)).toEqual( '12');
22-
expect(Text.truncateWithEllipsis('123', 2)).toEqual( '12');
23-
24-
expect(Text.truncateWithEllipsis('123', 5)).toEqual( '123');
25-
expect(Text.truncateWithEllipsis('1234', 5)).toEqual( '1234');
26-
expect(Text.truncateWithEllipsis('12345', 5)).toEqual( '12345');
27-
expect(Text.truncateWithEllipsis('123456', 5)).toEqual('12...');
54+
55+
describe('truncateWithEllipsis', () => {
56+
it('Throws an exception if the maximum length is less than zero', () => {
57+
expect(() => Text.truncateWithEllipsis('123', -1)).toThrow();
58+
});
59+
60+
it('Doesn\'t change the string if it\'s already shorter than the maximum length', () => {
61+
expect(Text.truncateWithEllipsis('', 2)).toEqual( '');
62+
expect(Text.truncateWithEllipsis('1', 2)).toEqual( '1');
63+
expect(Text.truncateWithEllipsis('12', 2)).toEqual( '12');
64+
65+
expect(Text.truncateWithEllipsis('123', 5)).toEqual( '123');
66+
expect(Text.truncateWithEllipsis('1234', 5)).toEqual( '1234');
67+
});
68+
69+
it('Truncates strings', () => {
70+
expect(Text.truncateWithEllipsis('123', 0)).toEqual( '');
71+
expect(Text.truncateWithEllipsis('123', 2)).toEqual( '12');
72+
expect(Text.truncateWithEllipsis('12345', 5)).toEqual( '12345');
73+
expect(Text.truncateWithEllipsis('123456', 5)).toEqual('12...');
74+
});
2875
});
2976
});

0 commit comments

Comments
 (0)