[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
[79a0317] | 4 | var forEach = require('for-each');
|
---|
[d565449] | 5 | var test = require('tape');
|
---|
| 6 | var isRegex = require('..');
|
---|
| 7 |
|
---|
| 8 | test('not regexes', function (t) {
|
---|
[79a0317] | 9 | // @ts-expect-error
|
---|
[d565449] | 10 | t.notOk(isRegex(), 'undefined is not regex');
|
---|
| 11 | t.notOk(isRegex(null), 'null is not regex');
|
---|
| 12 | t.notOk(isRegex(false), 'false is not regex');
|
---|
| 13 | t.notOk(isRegex(true), 'true is not regex');
|
---|
| 14 | t.notOk(isRegex(42), 'number is not regex');
|
---|
| 15 | t.notOk(isRegex('foo'), 'string is not regex');
|
---|
| 16 | t.notOk(isRegex([]), 'array is not regex');
|
---|
| 17 | t.notOk(isRegex({}), 'object is not regex');
|
---|
| 18 | t.notOk(isRegex(function () {}), 'function is not regex');
|
---|
| 19 | t.end();
|
---|
| 20 | });
|
---|
| 21 |
|
---|
| 22 | test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
|
---|
| 23 | var regex = /a/g;
|
---|
[79a0317] | 24 | /** @type {{ toString(): string, valueOf(): RegExp, [Symbol.toStringTag]?: string}} */
|
---|
[d565449] | 25 | var fakeRegex = {
|
---|
| 26 | toString: function () { return String(regex); },
|
---|
| 27 | valueOf: function () { return regex; }
|
---|
| 28 | };
|
---|
| 29 | fakeRegex[Symbol.toStringTag] = 'RegExp';
|
---|
| 30 | t.notOk(isRegex(fakeRegex), 'fake RegExp with @@toStringTag "RegExp" is not regex');
|
---|
| 31 | t.end();
|
---|
| 32 | });
|
---|
| 33 |
|
---|
| 34 | test('regexes', function (t) {
|
---|
| 35 | t.ok(isRegex(/a/g), 'regex literal is regex');
|
---|
| 36 | t.ok(isRegex(new RegExp('a', 'g')), 'regex object is regex');
|
---|
| 37 | t.end();
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | test('does not mutate regexes', function (t) {
|
---|
| 41 | t.test('lastIndex is a marker object', function (st) {
|
---|
| 42 | var regex = /a/;
|
---|
| 43 | var marker = {};
|
---|
[79a0317] | 44 | // @ts-expect-error
|
---|
[d565449] | 45 | regex.lastIndex = marker;
|
---|
| 46 | st.equal(regex.lastIndex, marker, 'lastIndex is the marker object');
|
---|
| 47 | st.ok(isRegex(regex), 'is regex');
|
---|
| 48 | st.equal(regex.lastIndex, marker, 'lastIndex is the marker object after isRegex');
|
---|
| 49 | st.end();
|
---|
| 50 | });
|
---|
| 51 |
|
---|
| 52 | t.test('lastIndex is nonzero', function (st) {
|
---|
| 53 | var regex = /a/;
|
---|
| 54 | regex.lastIndex = 3;
|
---|
| 55 | st.equal(regex.lastIndex, 3, 'lastIndex is 3');
|
---|
| 56 | st.ok(isRegex(regex), 'is regex');
|
---|
| 57 | st.equal(regex.lastIndex, 3, 'lastIndex is 3 after isRegex');
|
---|
| 58 | st.end();
|
---|
| 59 | });
|
---|
| 60 |
|
---|
| 61 | t.end();
|
---|
| 62 | });
|
---|
| 63 |
|
---|
| 64 | test('does not perform operations observable to Proxies', { skip: typeof Proxy !== 'function' }, function (t) {
|
---|
[79a0317] | 65 | /** @constructor */
|
---|
| 66 | function Handler() {
|
---|
| 67 | /** @type (keyof Reflect)[]} */
|
---|
[d565449] | 68 | this.trapCalls = [];
|
---|
[79a0317] | 69 | }
|
---|
[d565449] | 70 |
|
---|
[79a0317] | 71 | // eslint-disable-next-line no-extra-parens
|
---|
| 72 | forEach(/** @const @type {(keyof Reflect)[]} */ ([
|
---|
[d565449] | 73 | 'defineProperty',
|
---|
| 74 | 'deleteProperty',
|
---|
| 75 | 'get',
|
---|
| 76 | 'getOwnPropertyDescriptor',
|
---|
| 77 | 'getPrototypeOf',
|
---|
| 78 | 'has',
|
---|
| 79 | 'isExtensible',
|
---|
| 80 | 'ownKeys',
|
---|
| 81 | 'preventExtensions',
|
---|
| 82 | 'set',
|
---|
| 83 | 'setPrototypeOf'
|
---|
[79a0317] | 84 | ]), function (trapName) {
|
---|
[d565449] | 85 | Handler.prototype[trapName] = function () {
|
---|
| 86 | this.trapCalls.push(trapName);
|
---|
[79a0317] | 87 | // @ts-expect-error TODO: not sure why this is erroring
|
---|
[d565449] | 88 | return Reflect[trapName].apply(Reflect, arguments);
|
---|
| 89 | };
|
---|
| 90 | });
|
---|
| 91 |
|
---|
| 92 | t.test('proxy of object', function (st) {
|
---|
| 93 | var handler = new Handler();
|
---|
[79a0317] | 94 | // @ts-expect-error Proxy handlers can be any object
|
---|
[d565449] | 95 | var proxy = new Proxy({ lastIndex: 0 }, handler);
|
---|
| 96 |
|
---|
| 97 | st.equal(isRegex(proxy), false, 'proxy of plain object is not regex');
|
---|
[79a0317] | 98 | st.deepEqual(
|
---|
| 99 | handler.trapCalls,
|
---|
| 100 | handler.trapCalls.length > 0 ? ['getOwnPropertyDescriptor'] : [],
|
---|
| 101 | 'no unexpected proxy traps were triggered'
|
---|
| 102 | );
|
---|
[d565449] | 103 | st.end();
|
---|
| 104 | });
|
---|
| 105 |
|
---|
| 106 | t.test('proxy of RegExp instance', function (st) {
|
---|
| 107 | var handler = new Handler();
|
---|
[79a0317] | 108 | // @ts-expect-error Proxy handlers can be any object
|
---|
[d565449] | 109 | var proxy = new Proxy(/a/, handler);
|
---|
| 110 |
|
---|
| 111 | st.equal(isRegex(proxy), false, 'proxy of RegExp instance is not regex');
|
---|
[79a0317] | 112 | st.deepEqual(
|
---|
| 113 | handler.trapCalls,
|
---|
| 114 | handler.trapCalls.length > 0 ? ['getOwnPropertyDescriptor'] : [],
|
---|
| 115 | 'no unexpected proxy traps were triggered'
|
---|
| 116 | );
|
---|
[d565449] | 117 | st.end();
|
---|
| 118 | });
|
---|
| 119 |
|
---|
| 120 | t.end();
|
---|
| 121 | });
|
---|