File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -87,3 +87,36 @@ test('Can be called consecutively on returned result of previous call', t => {
8787 '' ,
8888 ] ) ;
8989} ) ;
90+
91+ /**
92+ * This was to address #4, where having a match at the end of a string was
93+ * causing the first replacement to return an array where the last element was
94+ * ''. This was causing an error where I was checking for !str, even though an
95+ * empty string should actually be allwed.
96+ */
97+ test ( 'Allows empty strings within results' , t => {
98+ let replacedContent ;
99+ const string = '@username http://a_photo.jpg' ;
100+
101+ replacedContent = replaceString ( string , / ( h t t p ? : \/ \/ .* \. (?: p n g | j p g ) ) / g, match => {
102+ return { key : 'image' , match } ;
103+ } ) ;
104+
105+ t . deepEqual ( replacedContent , [
106+ '@username ' ,
107+ { key : 'image' , match : 'http://a_photo.jpg' } ,
108+ '' ,
109+ ] ) ;
110+
111+ replacedContent = replaceString ( replacedContent , / @ ( \w + ) / g, match => {
112+ return { key : 'text' , match } ;
113+ } ) ;
114+
115+ t . deepEqual ( replacedContent , [
116+ '' ,
117+ { key : 'text' , match : 'username' } ,
118+ ' ' ,
119+ { key : 'image' , match : 'http://a_photo.jpg' } ,
120+ '' ,
121+ ] ) ;
122+ } ) ;
You can’t perform that action at this time.
0 commit comments