Comparing version 1.0.5 to 1.1.0
@@ -10,2 +10,12 @@ # Changelog | ||
## [v1.1.0](https://github.com/inspect-js/is-regex/compare/v1.0.5...v1.1.0) - 2020-06-04 | ||
### Commits | ||
- [New] use `badStringifier`‑based RegExp detection [`31eff67`](https://github.com/inspect-js/is-regex/commit/31eff673243d65c3d6c05848c0eb52f5380f1be3) | ||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `tape` [`fc91458`](https://github.com/inspect-js/is-regex/commit/fc914588187b8bb00d8d792c84f06a6e15d883c1) | ||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`; add `safe-publish-latest` [`d43ed83`](https://github.com/inspect-js/is-regex/commit/d43ed83db54ea727bb0b1b77a50af79d1edb8a6d) | ||
- [Dev Deps] update `auto-changelog`, `tape`; add `aud` [`56647d1`](https://github.com/inspect-js/is-regex/commit/56647d196be34ef3c118ad67726e75169fbcb875) | ||
- [meta] only run `aud` on prod deps [`e0865b8`](https://github.com/inspect-js/is-regex/commit/e0865b8360b0ac1b9d17b7b81ae5f339e5c9036b) | ||
## [v1.0.5](https://github.com/inspect-js/is-regex/compare/v1.0.4...v1.0.5) - 2019-12-15 | ||
@@ -12,0 +22,0 @@ |
66
index.js
'use strict'; | ||
var has = require('has'); | ||
var regexExec = RegExp.prototype.exec; | ||
var gOPD = Object.getOwnPropertyDescriptor; | ||
var hasSymbols = require('has-symbols')(); | ||
var hasToStringTag = hasSymbols && typeof Symbol.toStringTag === 'symbol'; | ||
var regexExec; | ||
var isRegexMarker; | ||
var badStringifier; | ||
var tryRegexExecCall = function tryRegexExec(value) { | ||
try { | ||
var lastIndex = value.lastIndex; | ||
value.lastIndex = 0; // eslint-disable-line no-param-reassign | ||
if (hasToStringTag) { | ||
regexExec = Function.call.bind(RegExp.prototype.exec); | ||
isRegexMarker = {}; | ||
regexExec.call(value); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} finally { | ||
value.lastIndex = lastIndex; // eslint-disable-line no-param-reassign | ||
var throwRegexMarker = function () { | ||
throw isRegexMarker; | ||
}; | ||
badStringifier = { | ||
toString: throwRegexMarker, | ||
valueOf: throwRegexMarker | ||
}; | ||
if (typeof Symbol.toPrimitive === 'symbol') { | ||
badStringifier[Symbol.toPrimitive] = throwRegexMarker; | ||
} | ||
}; | ||
} | ||
var toStr = Object.prototype.toString; | ||
var regexClass = '[object RegExp]'; | ||
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'; | ||
module.exports = function isRegex(value) { | ||
if (!value || typeof value !== 'object') { | ||
return false; | ||
} | ||
if (!hasToStringTag) { | ||
return toStr.call(value) === regexClass; | ||
} | ||
module.exports = hasToStringTag | ||
// eslint-disable-next-line consistent-return | ||
? function isRegex(value) { | ||
if (!value || typeof value !== 'object') { | ||
return false; | ||
} | ||
var descriptor = gOPD(value, 'lastIndex'); | ||
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value'); | ||
if (!hasLastIndexDataProperty) { | ||
return false; | ||
try { | ||
regexExec(value, badStringifier); | ||
} catch (e) { | ||
return e === isRegexMarker; | ||
} | ||
} | ||
: function isRegex(value) { | ||
// In older browsers, typeof regex incorrectly returns 'function' | ||
if (!value || (typeof value !== 'object' && typeof value !== 'function')) { | ||
return false; | ||
} | ||
return tryRegexExecCall(value); | ||
}; | ||
return toStr.call(value) === regexClass; | ||
}; |
{ | ||
"name": "is-regex", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Is this value a JS regex? Works cross-realm/iframe, and despite ES6 @@toStringTag", | ||
@@ -12,6 +12,7 @@ "author": "Jordan Harband <ljharb@gmail.com>", | ||
"scripts": { | ||
"prepublish": "safe-publish-latest", | ||
"pretest": "npm run lint", | ||
"test": "npm run tests-only", | ||
"tests-only": "node --harmony --es-staging test", | ||
"posttest": "npx aud", | ||
"posttest": "npx aud --production", | ||
"coverage": "covert test/index.js", | ||
@@ -40,11 +41,14 @@ "lint": "eslint .", | ||
"dependencies": { | ||
"has": "^1.0.3" | ||
"has-symbols": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^15.0.2", | ||
"auto-changelog": "^1.16.2", | ||
"@ljharb/eslint-config": "^17.1.0", | ||
"aud": "^1.1.2", | ||
"auto-changelog": "^2.0.0", | ||
"covert": "^1.1.1", | ||
"eclint": "^2.8.1", | ||
"eslint": "^6.7.2", | ||
"tape": "^4.11.0" | ||
"eslint": "^7.1.0", | ||
"foreach": "^2.0.5", | ||
"safe-publish-latest": "^1.1.4", | ||
"tape": "^5.0.1" | ||
}, | ||
@@ -51,0 +55,0 @@ "testling": { |
'use strict'; | ||
var hasSymbols = require('has-symbols')(); | ||
var hasToStringTag = hasSymbols && typeof Symbol.toStringTag === 'symbol'; | ||
var forEach = require('foreach'); | ||
var test = require('tape'); | ||
var isRegex = require('..'); | ||
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'; | ||
@@ -59,1 +61,46 @@ test('not regexes', function (t) { | ||
}); | ||
test('does not perform operations observable to Proxies', { skip: typeof Proxy !== 'function' }, function (t) { | ||
var Handler = function () { | ||
this.trapCalls = 0; | ||
}; | ||
forEach([ | ||
'defineProperty', | ||
'deleteProperty', | ||
'get', | ||
'getOwnPropertyDescriptor', | ||
'getPrototypeOf', | ||
'has', | ||
'isExtensible', | ||
'ownKeys', | ||
'preventExtensions', | ||
'set', | ||
'setPrototypeOf' | ||
], function (trapName) { | ||
Handler.prototype[trapName] = function () { | ||
this.trapCalls += 1; | ||
return Reflect[trapName].apply(Reflect, arguments); | ||
}; | ||
}); | ||
t.test('proxy of object', function (st) { | ||
var handler = new Handler(); | ||
var proxy = new Proxy({ lastIndex: 0 }, handler); | ||
st.equal(isRegex(proxy), false, 'proxy of plain object is not regex'); | ||
st.equal(handler.trapCalls, 0, 'no proxy traps were triggered'); | ||
st.end(); | ||
}); | ||
t.test('proxy of RegExp instance', function (st) { | ||
var handler = new Handler(); | ||
var proxy = new Proxy(/a/, handler); | ||
st.equal(isRegex(proxy), false, 'proxy of RegExp instance is not regex'); | ||
st.equal(handler.trapCalls, 0, 'no proxy traps were triggered'); | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26170
10
132
0
9
+ Addedhas-symbols@^1.0.1
+ Addedhas-symbols@1.0.3(transitive)
- Removedhas@^1.0.3
- Removedhas@1.0.4(transitive)