index.js 3.0 KB

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