index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. var hasToStringTag = require('has-tostringtag/shams')();
  3. var forEach = require('foreach');
  4. var test = require('tape');
  5. var isRegex = require('..');
  6. test('not regexes', function (t) {
  7. t.notOk(isRegex(), 'undefined is not regex');
  8. t.notOk(isRegex(null), 'null is not regex');
  9. t.notOk(isRegex(false), 'false is not regex');
  10. t.notOk(isRegex(true), 'true is not regex');
  11. t.notOk(isRegex(42), 'number is not regex');
  12. t.notOk(isRegex('foo'), 'string is not regex');
  13. t.notOk(isRegex([]), 'array is not regex');
  14. t.notOk(isRegex({}), 'object is not regex');
  15. t.notOk(isRegex(function () {}), 'function is not regex');
  16. t.end();
  17. });
  18. test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
  19. var regex = /a/g;
  20. var fakeRegex = {
  21. toString: function () { return String(regex); },
  22. valueOf: function () { return regex; }
  23. };
  24. fakeRegex[Symbol.toStringTag] = 'RegExp';
  25. t.notOk(isRegex(fakeRegex), 'fake RegExp with @@toStringTag "RegExp" is not regex');
  26. t.end();
  27. });
  28. test('regexes', function (t) {
  29. t.ok(isRegex(/a/g), 'regex literal is regex');
  30. t.ok(isRegex(new RegExp('a', 'g')), 'regex object is regex');
  31. t.end();
  32. });
  33. test('does not mutate regexes', function (t) {
  34. t.test('lastIndex is a marker object', function (st) {
  35. var regex = /a/;
  36. var marker = {};
  37. regex.lastIndex = marker;
  38. st.equal(regex.lastIndex, marker, 'lastIndex is the marker object');
  39. st.ok(isRegex(regex), 'is regex');
  40. st.equal(regex.lastIndex, marker, 'lastIndex is the marker object after isRegex');
  41. st.end();
  42. });
  43. t.test('lastIndex is nonzero', function (st) {
  44. var regex = /a/;
  45. regex.lastIndex = 3;
  46. st.equal(regex.lastIndex, 3, 'lastIndex is 3');
  47. st.ok(isRegex(regex), 'is regex');
  48. st.equal(regex.lastIndex, 3, 'lastIndex is 3 after isRegex');
  49. st.end();
  50. });
  51. t.end();
  52. });
  53. test('does not perform operations observable to Proxies', { skip: typeof Proxy !== 'function' }, function (t) {
  54. var Handler = function () {
  55. this.trapCalls = [];
  56. };
  57. forEach([
  58. 'defineProperty',
  59. 'deleteProperty',
  60. 'get',
  61. 'getOwnPropertyDescriptor',
  62. 'getPrototypeOf',
  63. 'has',
  64. 'isExtensible',
  65. 'ownKeys',
  66. 'preventExtensions',
  67. 'set',
  68. 'setPrototypeOf'
  69. ], function (trapName) {
  70. Handler.prototype[trapName] = function () {
  71. this.trapCalls.push(trapName);
  72. return Reflect[trapName].apply(Reflect, arguments);
  73. };
  74. });
  75. t.test('proxy of object', function (st) {
  76. var handler = new Handler();
  77. var proxy = new Proxy({ lastIndex: 0 }, handler);
  78. st.equal(isRegex(proxy), false, 'proxy of plain object is not regex');
  79. st.deepEqual(handler.trapCalls, ['getOwnPropertyDescriptor'], 'no unexpected proxy traps were triggered');
  80. st.end();
  81. });
  82. t.test('proxy of RegExp instance', function (st) {
  83. var handler = new Handler();
  84. var proxy = new Proxy(/a/, handler);
  85. st.equal(isRegex(proxy), false, 'proxy of RegExp instance is not regex');
  86. st.deepEqual(handler.trapCalls, ['getOwnPropertyDescriptor'], 'no unexpected proxy traps were triggered');
  87. st.end();
  88. });
  89. t.end();
  90. });