forked from iansinnott/react-string-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
172 lines (144 loc) · 5.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* eslint-disable import/no-extraneous-dependencies */
import test from 'ava';
import replaceString from './';
test("Doesn't throw if not given invalid input", t => {
t.notThrows(() => replaceString());
t.notThrows(() => replaceString(''));
});
test('Returns an array', t => {
t.true(Array.isArray(replaceString('blah', 'blah', x => x)));
});
test('Returns correct character offsets', t => {
const correctOffsets = [6, 17];
const charOffsets = [];
replaceString('Hey there, stranger', 'er', (m, i, o) => charOffsets.push(o));
t.deepEqual(charOffsets, correctOffsets);
});
test('Works with matching groups', t => {
t.deepEqual(
replaceString('hey there', /(hey)/g, x => ({ worked: x })),
['', { worked: 'hey' }, ' there']
);
});
test('Respects global flag to replace multiple matches', t => {
const str = 'Hey @ian_sinn and @other_handle, check out this link https://github.com/iansinnott/';
t.deepEqual(
replaceString(str, /@(\w+)/g, x => ({ worked: x })),
['Hey ', { worked: 'ian_sinn' }, ' and ', { worked: 'other_handle' }, ', check out this link https://github.com/iansinnott/']
);
});
test('Works with strings', t => {
t.deepEqual(
replaceString('hey there', 'hey', x => ({ worked: x })),
['', { worked: 'hey' }, ' there']
);
});
test('Works with arrays', t => {
const input = ['hey there', { value: 'you' }, 'again'];
t.deepEqual(
replaceString(input, 'hey', x => ({ worked: x })),
['', { worked: 'hey' }, ' there', { value: 'you' }, 'again']
);
});
test('Successfully escapes parens in strings', t => {
t.deepEqual(
replaceString('(hey) there', '(hey)', x => ({ worked: x })),
['', { worked: '(hey)' }, ' there']
);
t.deepEqual(
replaceString('hey ((y)(you)) there', '((y)(you))', x => ({ worked: x })),
['hey ', { worked: '((y)(you))' }, ' there']
);
});
test('Can be called consecutively on returned result of previous call', t => {
const originalTweet = 'Hey @iansinnott, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf';
let reactReplacedTweet;
// Match URLs
reactReplacedTweet = replaceString(originalTweet, /(https?:\/\/\S+)/g, match => (
{ type: 'url', value: match }
));
t.deepEqual(reactReplacedTweet, [
'Hey @iansinnott, check out this link ',
{ type: 'url', value: 'https://github.com/iansinnott/' },
' Hope to see you at #reactconf',
]);
// Match @-mentions
reactReplacedTweet = replaceString(reactReplacedTweet, /(@\w+)/g, match => (
{ type: 'mention', value: match }
));
t.deepEqual(reactReplacedTweet, [
'Hey ',
{ type: 'mention', value: '@iansinnott' },
', check out this link ',
{ type: 'url', value: 'https://github.com/iansinnott/' },
' Hope to see you at #reactconf',
]);
// Match hashtags
reactReplacedTweet = replaceString(reactReplacedTweet, /(#\w+)/g, match => (
{ type: 'hashtag', value: match }
));
t.deepEqual(reactReplacedTweet, [
'Hey ',
{ type: 'mention', value: '@iansinnott' },
', check out this link ',
{ type: 'url', value: 'https://github.com/iansinnott/' },
' Hope to see you at ',
{ type: 'hashtag', value: '#reactconf' },
'',
]);
});
/**
* This was to address #4, where having a match at the end of a string was
* causing the first replacement to return an array where the last element was
* ''. This was causing an error where I was checking for !str, even though an
* empty string should actually be allwed.
*/
test('Allows empty strings within results', t => {
let replacedContent;
const string = '@username http://a_photo.jpg';
replacedContent = replaceString(string, /(http?:\/\/.*\.(?:png|jpg))/g, match => {
return { key: 'image', match };
});
t.deepEqual(replacedContent, [
'@username ',
{ key: 'image', match: 'http://a_photo.jpg' },
'',
]);
replacedContent = replaceString(replacedContent, /@(\w+)/g, match => {
return { key: 'text', match };
});
t.deepEqual(replacedContent, [
'',
{ key: 'text', match: 'username' },
' ',
{ key: 'image', match: 'http://a_photo.jpg' },
'',
]);
});
test('Will not through if first element of input is empty string', t => {
const string = 'http://a_photo.jpg some string';
const replacedContent = replaceString(string, /(http?:\/\/.*\.(?:png|jpg))/g, match => {
return { key: 'image', match };
});
t.deepEqual(replacedContent, [
'',
{ key: 'image', match: 'http://a_photo.jpg' },
' some string',
]);
// This replacement would not actually give a new result from above, but it is
// simply to test that passing in an empty string as the first arg is OK
t.notThrows(() => {
replaceString(replacedContent, /@(\w+)/g, match => {
return { key: 'text', match };
});
});
});
test("Avoids undefined values due to regex", (t) => {
const string = `hey you there`;
const re = /(hey)|(you)/;
// Normal splits include undefined if you do this
t.deepEqual(string.split(re), ["", "hey", undefined, " ", undefined, "you", " there"]);
t.notThrows(() => {
replaceString(string, /(hey)|(you)/, x => x);
});
});