@@ -3,29 +3,125 @@ import * as TextUtility from '../src/Utility';
33
44const s1 = ' HI ' ;
55const 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+
685describe ( '.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
24112describe ( '.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
44176describe ( '.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
59208describe ( '.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