1
- import * as stringUtils from '../../src/utils/string-utils' ;
1
+ import {
2
+ splitByDelimiterWithEscapeCharacter ,
3
+ startsAtIndexWith ,
4
+ hasUnquotedSubstring ,
5
+ fastHash ,
6
+ replaceAll ,
7
+ } from '../../src/utils/string-utils' ;
2
8
3
9
describe ( 'splitByDelimiterWithEscapeCharacter' , ( ) => {
4
10
it ( 'works if splits plain strings with and w/o preserving all tokens' , ( ) => {
5
- let result = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , false ) ;
11
+ let result = splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , false ) ;
6
12
let expected = [ 'example.org' , 'example.com' ] ;
7
13
expect ( result ) . toEqual ( expected ) ;
8
14
9
15
// Empty tokens must be preserved correctly
10
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , true ) ;
16
+ result = splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , true ) ;
11
17
expected = [ 'example.org' , '' , '' , 'example.com' ] ;
12
18
expect ( result ) . toEqual ( expected ) ;
13
19
14
20
// Delimiters must be escaped correctly, with escape character removed
15
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org\\,\\,\\,example.com' , ',' , '\\' , true ) ;
21
+ result = splitByDelimiterWithEscapeCharacter ( 'example.org\\,\\,\\,example.com' , ',' , '\\' , true ) ;
16
22
expected = [ 'example.org,,,example.com' ] ;
17
23
expect ( result ) . toEqual ( expected ) ;
18
24
19
25
// Empty string must return empty array
20
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( '' , ',' , '\\' , true ) ;
26
+ result = splitByDelimiterWithEscapeCharacter ( '' , ',' , '\\' , true ) ;
21
27
expected = [ ] ;
22
28
expect ( result ) . toEqual ( expected ) ;
23
29
24
30
// Check if index 0 delimiter is trimmed correctly
25
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( ',example.org,example.com' , ',' , '\\' , false ) ;
31
+ result = splitByDelimiterWithEscapeCharacter ( ',example.org,example.com' , ',' , '\\' , false ) ;
26
32
expected = [ 'example.org' , 'example.com' ] ;
27
33
expect ( result ) . toEqual ( expected ) ;
28
34
29
35
// Forward slash splitting
30
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( '/text-to-be-replaced/new-text/i' , '/' , '\\' , true ) ;
36
+ result = splitByDelimiterWithEscapeCharacter ( '/text-to-be-replaced/new-text/i' , '/' , '\\' , true ) ;
31
37
expected = [ 'text-to-be-replaced' , 'new-text' , 'i' ] ;
32
38
expect ( result ) . toEqual ( expected ) ;
33
39
34
40
// Keep empty token after ending delimiter
35
- result = stringUtils . splitByDelimiterWithEscapeCharacter (
41
+ result = splitByDelimiterWithEscapeCharacter (
36
42
'/(<VAST[\\s\\S]*?>)[\\s\\S]*<\\/VAST>/$1<\\/VAST>/' ,
37
43
'/' ,
38
44
'\\' ,
@@ -42,7 +48,7 @@ describe('splitByDelimiterWithEscapeCharacter', () => {
42
48
expect ( result ) . toEqual ( expected ) ;
43
49
44
50
// Remove empty token after ending delimiter
45
- result = stringUtils . splitByDelimiterWithEscapeCharacter (
51
+ result = splitByDelimiterWithEscapeCharacter (
46
52
'/(<VAST[\\s\\S]*?>)[\\s\\S]*<\\/VAST>/$1<\\/VAST>/' ,
47
53
'/' ,
48
54
'\\' ,
@@ -52,12 +58,12 @@ describe('splitByDelimiterWithEscapeCharacter', () => {
52
58
expect ( result ) . toEqual ( expected ) ;
53
59
54
60
// Keep empty token after delimiter for comma
55
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com,' , ',' , '\\' , true ) ;
61
+ result = splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com,' , ',' , '\\' , true ) ;
56
62
expected = [ 'example.org' , '' , '' , 'example.com' , '' ] ;
57
63
expect ( result ) . toEqual ( expected ) ;
58
64
59
65
// Escape character should be kept if specified
60
- result = stringUtils . splitByDelimiterWithEscapeCharacter ( 'qwe\\,rty,1,2,3' , ',' , '\\' , false , false ) ;
66
+ result = splitByDelimiterWithEscapeCharacter ( 'qwe\\,rty,1,2,3' , ',' , '\\' , false , false ) ;
61
67
expected = [ 'qwe\\,rty' , '1' , '2' , '3' ] ;
62
68
expect ( result ) . toEqual ( expected ) ;
63
69
} ) ;
@@ -69,10 +75,10 @@ describe('splitByDelimiterWithEscapeCharacter', () => {
69
75
while ( count < 2000 ) {
70
76
count += 1 ;
71
77
72
- let parts = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , false ) ;
78
+ let parts = splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , false ) ;
73
79
expect ( parts . length ) . toEqual ( 2 ) ;
74
80
75
- parts = stringUtils . splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , true ) ;
81
+ parts = splitByDelimiterWithEscapeCharacter ( 'example.org,,,example.com' , ',' , '\\' , true ) ;
76
82
expect ( parts . length ) . toEqual ( 4 ) ;
77
83
}
78
84
@@ -83,41 +89,41 @@ describe('splitByDelimiterWithEscapeCharacter', () => {
83
89
84
90
describe ( 'startsAtIndexWith' , ( ) => {
85
91
it ( 'works if it can check simple strings' , ( ) => {
86
- expect ( stringUtils . startsAtIndexWith ( 'example' , 0 , 'ex' ) ) . toEqual ( true ) ;
87
- expect ( stringUtils . startsAtIndexWith ( 'example' , 1 , 'xa' ) ) . toEqual ( true ) ;
88
- expect ( stringUtils . startsAtIndexWith ( 'example' , 6 , 'e' ) ) . toEqual ( true ) ;
92
+ expect ( startsAtIndexWith ( 'example' , 0 , 'ex' ) ) . toEqual ( true ) ;
93
+ expect ( startsAtIndexWith ( 'example' , 1 , 'xa' ) ) . toEqual ( true ) ;
94
+ expect ( startsAtIndexWith ( 'example' , 6 , 'e' ) ) . toEqual ( true ) ;
89
95
} ) ;
90
96
} ) ;
91
97
92
98
describe ( 'hasUnquotedSubstring' , ( ) => {
93
99
it ( 'works if it can check simple strings' , ( ) => {
94
- expect ( stringUtils . hasUnquotedSubstring ( 'example' , 'ex' ) ) . toEqual ( true ) ;
95
- expect ( stringUtils . hasUnquotedSubstring ( '"example"' , 'ex' ) ) . toEqual ( false ) ;
96
- expect ( stringUtils . hasUnquotedSubstring ( '\\"example\\"' , 'ex' ) ) . toEqual ( true ) ;
100
+ expect ( hasUnquotedSubstring ( 'example' , 'ex' ) ) . toEqual ( true ) ;
101
+ expect ( hasUnquotedSubstring ( '"example"' , 'ex' ) ) . toEqual ( false ) ;
102
+ expect ( hasUnquotedSubstring ( '\\"example\\"' , 'ex' ) ) . toEqual ( true ) ;
97
103
} ) ;
98
104
} ) ;
99
105
100
106
describe ( 'replaceAll' , ( ) => {
101
107
it ( 'works if it can replace simple strings' , ( ) => {
102
- expect ( stringUtils . replaceAll ( 'example_example' , 'ex' , 'EX' ) ) . toEqual ( 'EXample_EXample' ) ;
108
+ expect ( replaceAll ( 'example_example' , 'ex' , 'EX' ) ) . toEqual ( 'EXample_EXample' ) ;
103
109
} ) ;
104
110
} ) ;
105
111
106
112
describe ( 'fastHash' , ( ) => {
107
113
it ( 'works if it can fastHash' , ( ) => {
108
- expect ( stringUtils . fastHash ( '' ) ) . toEqual ( 0 ) ;
114
+ expect ( fastHash ( '' ) ) . toEqual ( 0 ) ;
109
115
} ) ;
110
116
111
117
it ( 'creates unique hashes' , ( ) => {
112
- const hashOne = stringUtils . fastHash ( 'example.com' ) ;
113
- const hashTwo = stringUtils . fastHash ( 'example.net' ) ;
118
+ const hashOne = fastHash ( 'example.com' ) ;
119
+ const hashTwo = fastHash ( 'example.net' ) ;
114
120
115
121
expect ( hashOne ) . not . toBe ( hashTwo ) ;
116
122
} ) ;
117
123
118
124
it ( 'prevent overflow for too long strings' , ( ) => {
119
- const hashOne = stringUtils . fastHash ( 'verylongstringverylongstringverylongstring' ) ;
120
- const hashTwo = stringUtils . fastHash ( 'anotherverylongstringverylongstringverylongstring' ) ;
125
+ const hashOne = fastHash ( 'verylongstringverylongstringverylongstring' ) ;
126
+ const hashTwo = fastHash ( 'anotherverylongstringverylongstringverylongstring' ) ;
121
127
122
128
expect ( hashOne ) . toBeLessThan ( 2 ** 32 ) ;
123
129
expect ( hashTwo ) . toBeLessThan ( 2 ** 32 ) ;
0 commit comments