1
|
'use strict';
|
2
|
|
3
|
var test = require('tape');
|
4
|
var isRegex = require('..');
|
5
|
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
6
|
|
7
|
test('not regexes', function (t) {
|
8
|
t.notOk(isRegex(), 'undefined is not regex');
|
9
|
t.notOk(isRegex(null), 'null is not regex');
|
10
|
t.notOk(isRegex(false), 'false is not regex');
|
11
|
t.notOk(isRegex(true), 'true is not regex');
|
12
|
t.notOk(isRegex(42), 'number is not regex');
|
13
|
t.notOk(isRegex('foo'), 'string is not regex');
|
14
|
t.notOk(isRegex([]), 'array is not regex');
|
15
|
t.notOk(isRegex({}), 'object is not regex');
|
16
|
t.notOk(isRegex(function () {}), 'function is not regex');
|
17
|
t.end();
|
18
|
});
|
19
|
|
20
|
test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
|
21
|
var regex = /a/g;
|
22
|
var fakeRegex = {
|
23
|
toString: function () { return String(regex); },
|
24
|
valueOf: function () { return regex; }
|
25
|
};
|
26
|
fakeRegex[Symbol.toStringTag] = 'RegExp';
|
27
|
t.notOk(isRegex(fakeRegex), 'fake RegExp with @@toStringTag "RegExp" is not regex');
|
28
|
t.end();
|
29
|
});
|
30
|
|
31
|
test('regexes', function (t) {
|
32
|
t.ok(isRegex(/a/g), 'regex literal is regex');
|
33
|
t.ok(isRegex(new RegExp('a', 'g')), 'regex object is regex');
|
34
|
t.end();
|
35
|
});
|
36
|
|
37
|
test('does not mutate regexes', function (t) {
|
38
|
t.test('lastIndex is a marker object', function (st) {
|
39
|
var regex = /a/;
|
40
|
var marker = {};
|
41
|
regex.lastIndex = marker;
|
42
|
st.equal(regex.lastIndex, marker, 'lastIndex is the marker object');
|
43
|
st.ok(isRegex(regex), 'is regex');
|
44
|
st.equal(regex.lastIndex, marker, 'lastIndex is the marker object after isRegex');
|
45
|
st.end();
|
46
|
});
|
47
|
|
48
|
t.test('lastIndex is nonzero', function (st) {
|
49
|
var regex = /a/;
|
50
|
regex.lastIndex = 3;
|
51
|
st.equal(regex.lastIndex, 3, 'lastIndex is 3');
|
52
|
st.ok(isRegex(regex), 'is regex');
|
53
|
st.equal(regex.lastIndex, 3, 'lastIndex is 3 after isRegex');
|
54
|
st.end();
|
55
|
});
|
56
|
|
57
|
t.end();
|
58
|
});
|