Skip to content

Commit

Permalink
[Refactor] when try/catch is needed, bail early if the value lacks an…
Browse files Browse the repository at this point in the history
… own `lastIndex` data property.
  • Loading branch information
ljharb committed Feb 18, 2017
1 parent 31eaca2 commit 288ad93
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
@@ -1,5 +1,9 @@
{
"root": true,

"extends": "@ljharb"
"extends": "@ljharb",

"rules": {
"id-length": [1]
}
}
9 changes: 9 additions & 0 deletions index.js
@@ -1,6 +1,9 @@
'use strict';

var has = require('has');
var regexExec = RegExp.prototype.exec;
var gOPD = Object.getOwnPropertyDescriptor;

var tryRegexExecCall = function tryRegexExec(value) {
try {
regexExec.call(value);
Expand All @@ -21,5 +24,11 @@ module.exports = function isRegex(value) {
return toStr.call(value) === regexClass;
}

var descriptor = gOPD(value, 'lastIndex');
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
if (!hasLastIndexDataProperty) {
return false;
}

return tryRegexExecCall(value);
};
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,9 @@
"regular",
"expression"
],
"dependencies": {},
"dependencies": {
"has": "^1.0.1"
},
"devDependencies": {
"tape": "^4.6.3",
"covert": "^1.1.0",
Expand Down

0 comments on commit 288ad93

Please sign in to comment.