Skip to content

Commit 47c7a2a

Browse files
Dev update.
1 parent 5bd91d3 commit 47c7a2a

File tree

3 files changed

+334
-41
lines changed

3 files changed

+334
-41
lines changed

tests/Padding.test.ts

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,109 @@
11
import { describe, it, expect } from 'vitest';
2-
import {padLeft, padRight} from '../src/Padding';
2+
import {
3+
padLeft,
4+
padRight,
5+
padStringLeft,
6+
padStringRight,
7+
padNumberLeft,
8+
padNumberRight
9+
} from '../src/Padding';
10+
11+
describe('padStringLeft()', () => {
12+
it('should pad string to the left', () => {
13+
expect(padStringLeft('X', 3, 'Y')).toBe('YYX');
14+
expect(padStringLeft('test', 6, '0')).toBe('00test');
15+
});
16+
17+
it('should handle empty pad character', () => {
18+
expect(padStringLeft('X', 3, '')).toBe('X');
19+
});
20+
21+
it('should handle zero or negative minLength', () => {
22+
expect(padStringLeft('X', 0, 'Y')).toBe('X');
23+
expect(padStringLeft('X', -1, 'Y')).toBe('X');
24+
});
25+
});
26+
27+
describe('padStringRight()', () => {
28+
it('should pad string to the right', () => {
29+
expect(padStringRight('X', 3, 'Y')).toBe('XYY');
30+
expect(padStringRight('test', 6, '0')).toBe('test00');
31+
});
32+
33+
it('should handle empty pad character', () => {
34+
expect(padStringRight('X', 3, '')).toBe('X');
35+
});
36+
37+
it('should handle zero or negative minLength', () => {
38+
expect(padStringRight('X', 0, 'Y')).toBe('X');
39+
expect(padStringRight('X', -1, 'Y')).toBe('X');
40+
});
41+
});
42+
43+
describe('padNumberLeft()', () => {
44+
it('should pad number to the left', () => {
45+
expect(padNumberLeft(1, 3, 0)).toBe('001');
46+
expect(padNumberLeft(42, 5, '0')).toBe('00042');
47+
});
48+
49+
it('should handle zero source', () => {
50+
expect(padNumberLeft(0, 3, '0')).toBe('000');
51+
});
52+
53+
it('should throw error for non-number', () => {
54+
expect(() => padNumberLeft('not a number' as any, 3, '0'))
55+
.toThrow('Cannot pad non-number.');
56+
});
57+
});
58+
59+
describe('padNumberRight()', () => {
60+
it('should pad number to the right', () => {
61+
expect(padNumberRight(1, 3, 0)).toBe('100');
62+
expect(padNumberRight(42, 5, '0')).toBe('42000');
63+
});
64+
65+
it('should handle zero source', () => {
66+
expect(padNumberRight(0, 3, '0')).toBe('000');
67+
});
68+
69+
it('should throw error for non-number', () => {
70+
expect(() => padNumberRight('not a number' as any, 3, '0'))
71+
.toThrow('Cannot pad non-number.');
72+
});
73+
});
374

475
describe('.padLeft()', () => {
5-
it('should pad to the left', () => {
6-
expect(padLeft('X', 3, 'Y')).equal('YYX');
7-
expect(padLeft('X', 3)).equal(' X');
8-
expect(padLeft(1, 3, 0)).equal('001');
9-
expect(padLeft(1, 3)).equal('001');
10-
expect(padLeft(1, 3, 2)).equal('221');
76+
it('should pad string to the left', () => {
77+
expect(padLeft('X', 3, 'Y')).toBe('YYX');
78+
expect(padLeft('X', 3)).toBe(' X');
79+
});
80+
81+
it('should pad number to the left', () => {
82+
expect(padLeft(1, 3, 0)).toBe('001');
83+
expect(padLeft(1, 3)).toBe('001');
84+
expect(padLeft(1, 3, 2)).toBe('221');
85+
});
86+
87+
it('should throw error for invalid source type', () => {
88+
expect(() => padLeft({} as any, 3, 'x'))
89+
.toThrow('Invalid source type.');
1190
});
1291
});
1392

1493
describe('.padRight()', () => {
15-
it('should pad to the right', () => {
16-
expect(padRight('X', 3, 'Y')).equal('XYY');
17-
expect(padRight('X', 3)).equal('X ');
18-
expect(padRight(1, 3, 0)).equal('100');
19-
expect(padRight(1, 3)).equal('100');
20-
expect(padRight(1, 3, 2)).equal('122');
94+
it('should pad string to the right', () => {
95+
expect(padRight('X', 3, 'Y')).toBe('XYY');
96+
expect(padRight('X', 3)).toBe('X ');
97+
});
98+
99+
it('should pad number to the right', () => {
100+
expect(padRight(1, 3, 0)).toBe('100');
101+
expect(padRight(1, 3)).toBe('100');
102+
expect(padRight(1, 3, 2)).toBe('122');
103+
});
104+
105+
it('should throw error for invalid source type', () => {
106+
expect(() => padRight({} as any, 3, 'x'))
107+
.toThrow('Invalid source type.');
21108
});
22109
});

tests/Utility.test.ts

Lines changed: 193 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,125 @@ import * as TextUtility from '../src/Utility';
33

44
const s1 = ' HI ';
55
const s2 = '.-.-xHIX//\\';
6+
7+
describe('EMPTY constant', () => {
8+
it('should be empty string', () => {
9+
expect(TextUtility.EMPTY).toBe('');
10+
});
11+
});
12+
13+
describe('.getHashCode()', () => {
14+
it('should return 0 for empty string', () => {
15+
expect(TextUtility.getHashCode('')).toBe(0);
16+
});
17+
18+
it('should return different hash codes for different strings', () => {
19+
const hash1 = TextUtility.getHashCode('hello');
20+
const hash2 = TextUtility.getHashCode('world');
21+
expect(hash1).not.toBe(hash2);
22+
});
23+
24+
it('should return same hash code for same string', () => {
25+
const hash1 = TextUtility.getHashCode('test');
26+
const hash2 = TextUtility.getHashCode('test');
27+
expect(hash1).toBe(hash2);
28+
});
29+
});
30+
31+
describe('.repeat()', () => {
32+
it('should repeat string multiple times', () => {
33+
expect(TextUtility.repeat('x', 3)).toBe('xxx');
34+
expect(TextUtility.repeat('ab', 2)).toBe('abab');
35+
});
36+
37+
it('should return empty for zero count', () => {
38+
expect(TextUtility.repeat('x', 0)).toBe('');
39+
});
40+
41+
it('should return empty for NaN count', () => {
42+
expect(TextUtility.repeat('x', NaN)).toBe('');
43+
});
44+
45+
it('should return empty for empty source', () => {
46+
expect(TextUtility.repeat('', 5)).toBe('');
47+
});
48+
49+
it('should throw error for null source', () => {
50+
expect(() => TextUtility.repeat(null as any, 5)).toThrow('Cannot repeat null or undefined.');
51+
});
52+
53+
it('should throw error for undefined source', () => {
54+
expect(() => TextUtility.repeat(undefined as any, 5)).toThrow('Cannot repeat null or undefined.');
55+
});
56+
57+
it('should throw TypeError for non-string source', () => {
58+
expect(() => TextUtility.repeat(123 as any, 5)).toThrow('Expected \'source\' to be string.');
59+
});
60+
});
61+
62+
describe('.fromChars()', () => {
63+
it('should convert character codes to string', () => {
64+
expect(TextUtility.fromChars([65, 66, 67])).toBe('ABC');
65+
expect(TextUtility.fromChars([72, 101, 108, 108, 111])).toBe('Hello');
66+
});
67+
68+
it('should repeat single character code', () => {
69+
expect(TextUtility.fromChars(65, 3)).toBe('AAA');
70+
expect(TextUtility.fromChars(88, 1)).toBe('X');
71+
});
72+
73+
it('should handle empty array', () => {
74+
expect(TextUtility.fromChars([])).toBe('');
75+
});
76+
});
77+
78+
describe('.escapeRegExp()', () => {
79+
it('should escape special regex characters', () => {
80+
expect(TextUtility.escapeRegExp('.*+?^${}()|[]\\/')).toBe('\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\\\/');
81+
expect(TextUtility.escapeRegExp('hello')).toBe('hello');
82+
});
83+
});
84+
685
describe('.trim()', () => {
786
it('should leave a string without leading or trailing whitespace', () => {
8-
expect(TextUtility.trim(s1)).equal('HI');
87+
expect(TextUtility.trim(s1)).toBe('HI');
988
});
1089

1190
it('should leave a string without leading or trailing trim characters (string)', () => {
12-
expect(TextUtility.trim(s2, '.-/\\x', true)).equal('HI');
91+
expect(TextUtility.trim(s2, '.-/\\x', true)).toBe('HI');
1392
});
1493

1594
it('should leave a string without leading or trailing trim characters (array)', () => {
16-
expect(TextUtility.trim(s2, ['.', '-', '/', '\\', 'x', 'X'])).equal('HI');
95+
expect(TextUtility.trim(s2, ['.', '-', '/', '\\', 'x', 'X'])).toBe('HI');
1796
});
1897

1998
it('should leave a string untouched if no trim characters', () => {
20-
expect(TextUtility.trim(s2, '')).equal(s2);
99+
expect(TextUtility.trim(s2, '')).toBe(s2);
100+
});
101+
102+
it('should handle undefined chars parameter', () => {
103+
expect(TextUtility.trim(' hello ')).toBe('hello');
104+
});
105+
106+
it('should handle case sensitivity', () => {
107+
expect(TextUtility.trim('AAAhelloAAA', 'a', false)).toBe('AAAhelloAAA');
108+
expect(TextUtility.trim('AAAhelloAAA', 'a', true)).toBe('hello');
21109
});
22110
});
23111

24112
describe('.format(source,..args)', () => {
25113
it('should replace contents of a string', () => {
26114
expect(TextUtility.format(
27115
'Hello, my name is {0} and I\'m number {length}.', 'George', 2))
28-
.equal('Hello, my name is George and I\'m number 2.');
116+
.toBe('Hello, my name is George and I\'m number 2.');
117+
});
118+
119+
it('should handle missing parameters', () => {
120+
expect(TextUtility.format('Hello {0} {1}', 'world')).toBe('Hello world {1}');
121+
});
122+
123+
it('should handle empty format string', () => {
124+
expect(TextUtility.format('', 'test')).toBe('');
29125
});
30126
});
31127

@@ -37,42 +133,111 @@ describe('.supplant(source,..args)', () => {
37133
like: 'cheese',
38134
x: {}
39135
}))
40-
.equal('Hello, my name is George and I like cheese. [object Object] {y}');
136+
.toBe('Hello, my name is George and I like cheese. [object Object] {y}');
137+
});
138+
139+
it('should work with arrays using numeric indexes', () => {
140+
expect(TextUtility.supplant('Hello {0} and {1}!', ['world', 'everyone']))
141+
.toBe('Hello world and everyone!');
142+
});
143+
144+
it('should handle different types', () => {
145+
expect(TextUtility.supplant('Number: {num}, Boolean: {bool}', {
146+
num: 42,
147+
bool: true
148+
})).toBe('Number: 42, Boolean: true');
149+
});
150+
151+
it('should handle objects with toString method', () => {
152+
const obj = { toString: () => 'custom string' };
153+
expect(TextUtility.supplant('Object: {obj}', { obj }))
154+
.toBe('Object: custom string');
155+
});
156+
157+
it('should handle null/undefined values', () => {
158+
expect(TextUtility.supplant('Value: {val}', { val: null }))
159+
.toBe('Value: {val}');
160+
expect(TextUtility.supplant('Value: {val}', { val: undefined }))
161+
.toBe('Value: {val}');
162+
});
163+
164+
it('should handle array index properties', () => {
165+
const arr = ['a', 'b', 'c'];
166+
expect(TextUtility.supplant('Length: {length}, Item: {1}', arr))
167+
.toBe('Length: 3, Item: b');
168+
});
169+
170+
it('should handle invalid numeric indexes gracefully', () => {
171+
expect(TextUtility.supplant('Test {abc}', ['item']))
172+
.toBe('Test {abc}');
41173
});
42174
});
43175

44176
describe('.startsWith(source,pattern)', () => {
45-
it('should detect pattern at beginning', () =>
177+
it('should detect pattern at beginning', () => {
46178
expect(TextUtility.startsWith(
47179
'Hello, my name is',
48180
'Hello'))
49-
.toBe(true)
50-
);
51-
it('should not detect pattern at beginning', () =>
52-
expect(!TextUtility.startsWith(
181+
.toBe(true);
182+
});
183+
184+
it('should not detect pattern at beginning', () => {
185+
expect(TextUtility.startsWith(
53186
'Hello, my name is',
54187
'is'))
55-
.toBe(true)
56-
);
188+
.toBe(false);
189+
});
190+
191+
it('should handle exact match', () => {
192+
expect(TextUtility.startsWith('hello', 'hello')).toBe(true);
193+
});
194+
195+
it('should handle empty pattern', () => {
196+
expect(TextUtility.startsWith('hello', '')).toBe(false);
197+
});
198+
199+
it('should handle non-string source', () => {
200+
expect(TextUtility.startsWith(123 as any, 'test')).toBe(false);
201+
});
202+
203+
it('should handle pattern longer than source', () => {
204+
expect(TextUtility.startsWith('hi', 'hello')).toBe(false);
205+
});
57206
});
58207

59208
describe('.endsWith(source,pattern)', () => {
60-
it('should detect pattern at beginning', () =>
209+
it('should detect pattern at end', () => {
61210
expect(TextUtility.endsWith(
62211
'Hello, my name is',
63212
'is'))
64-
.toBe(true)
65-
);
66-
it('should not detect pattern at beginning', () => {
67-
expect(!TextUtility.endsWith(
68-
'Hello, my name is',
69-
'Hello'))
70-
.toBe(true);
71-
72-
expect(!TextUtility.endsWith(
73-
'Hello, my name is',
74-
'is '))
75-
.toBe(true);
76-
}
77-
);
213+
.toBe(true);
214+
});
215+
216+
it('should not detect pattern at end', () => {
217+
expect(TextUtility.endsWith(
218+
'Hello, my name is',
219+
'Hello'))
220+
.toBe(false);
221+
222+
expect(TextUtility.endsWith(
223+
'Hello, my name is',
224+
'is '))
225+
.toBe(false);
226+
});
227+
228+
it('should handle exact match', () => {
229+
expect(TextUtility.endsWith('hello', 'hello')).toBe(true);
230+
});
231+
232+
it('should handle empty pattern', () => {
233+
expect(TextUtility.endsWith('hello', '')).toBe(false);
234+
});
235+
236+
it('should handle non-string source', () => {
237+
expect(TextUtility.endsWith(123 as any, 'test')).toBe(false);
238+
});
239+
240+
it('should handle pattern longer than source', () => {
241+
expect(TextUtility.endsWith('hi', 'hello')).toBe(false);
242+
});
78243
});

0 commit comments

Comments
 (0)