@@ -46,8 +46,6 @@ const replacementToken = /\$(?:\{([^\}]+)\}|<([^>]+)>|(\d\d?|[\s\S]?))/g;
46
46
const correctExecNpcg = / ( ) ? ?/ . exec ( '' ) [ 1 ] === undefined ;
47
47
// Check for ES6 `flags` prop support
48
48
const hasFlagsProp = / x / . flags !== undefined ;
49
- // Shortcut to `Object.prototype.toString`
50
- const { toString} = { } ;
51
49
52
50
function hasNativeFlag ( flag ) {
53
51
// Can't check based on the presence of properties/getters since browsers might support such
@@ -327,7 +325,24 @@ function isQuantifierNext(pattern, pos, flags) {
327
325
* @returns {boolean } Whether the object matches the type.
328
326
*/
329
327
function isType ( value , type ) {
330
- return toString . call ( value ) === `[object ${ type } ]` ;
328
+ return Object . prototype . toString . call ( value ) === `[object ${ type } ]` ;
329
+ }
330
+
331
+ /**
332
+ * Returns the object, or throws an error if it is `null` or `undefined`. This is used to follow
333
+ * the ES5 abstract operation `ToObject`.
334
+ *
335
+ * @private
336
+ * @param {* } value Object to check and return.
337
+ * @returns {* } The provided object.
338
+ */
339
+ function nullThrows ( value ) {
340
+ // null or undefined
341
+ if ( value == null ) {
342
+ throw new TypeError ( 'Cannot convert null or undefined to object' ) ;
343
+ }
344
+
345
+ return value ;
331
346
}
332
347
333
348
/**
@@ -485,23 +500,6 @@ function setNamespacing(on) {
485
500
features . namespacing = on ;
486
501
}
487
502
488
- /**
489
- * Returns the object, or throws an error if it is `null` or `undefined`. This is used to follow
490
- * the ES5 abstract operation `ToObject`.
491
- *
492
- * @private
493
- * @param {* } value Object to check and return.
494
- * @returns {* } The provided object.
495
- */
496
- function toObject ( value ) {
497
- // null or undefined
498
- if ( value == null ) {
499
- throw new TypeError ( 'Cannot convert null or undefined to object' ) ;
500
- }
501
-
502
- return value ;
503
- }
504
-
505
503
// ==--------------------------==
506
504
// Constructor
507
505
// ==--------------------------==
@@ -785,7 +783,7 @@ XRegExp.cache.flush = (cacheName) => {
785
783
* XRegExp.escape('Escaped? <.>');
786
784
* // -> 'Escaped\?\ <\.>'
787
785
*/
788
- XRegExp . escape = ( str ) => String ( toObject ( str ) ) . replace ( / [ - \[ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
786
+ XRegExp . escape = ( str ) => String ( nullThrows ( str ) ) . replace ( / [ - \[ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
789
787
790
788
/**
791
789
* Executes a regex search in a specified string. Returns a match array or `null`. If the provided
@@ -982,7 +980,7 @@ XRegExp.isInstalled = (feature) => !!(features[feature]);
982
980
* XRegExp.isRegExp(RegExp('^', 'm')); // -> true
983
981
* XRegExp.isRegExp(XRegExp('(?s).')); // -> true
984
982
*/
985
- XRegExp . isRegExp = ( value ) => toString . call ( value ) === '[object RegExp]' ; // isType(value, 'RegExp');
983
+ XRegExp . isRegExp = ( value ) => isType ( value , 'RegExp' ) ;
986
984
987
985
/**
988
986
* Returns the first matched string, or in global mode, an array containing all matched strings.
@@ -1026,7 +1024,7 @@ XRegExp.match = (str, regex, scope) => {
1026
1024
} )
1027
1025
) ;
1028
1026
1029
- const result = String ( toObject ( str ) ) . match ( r2 ) ;
1027
+ const result = String ( nullThrows ( str ) ) . match ( r2 ) ;
1030
1028
1031
1029
if ( regex . global ) {
1032
1030
regex . lastIndex = (
@@ -1172,7 +1170,7 @@ XRegExp.replace = (str, search, replacement, scope) => {
1172
1170
}
1173
1171
1174
1172
// Fixed `replace` required for named backreferences, etc.
1175
- const result = fixed . replace . call ( toObject ( str ) , s2 , replacement ) ;
1173
+ const result = fixed . replace . call ( nullThrows ( str ) , s2 , replacement ) ;
1176
1174
1177
1175
if ( isRegex && search . global ) {
1178
1176
// Fixes IE, Safari bug (last tested IE 9, Safari 5.1)
@@ -1239,7 +1237,7 @@ XRegExp.replaceEach = (str, replacements) => {
1239
1237
* XRegExp.split('..word1..', /([a-z]+)(\d+)/i);
1240
1238
* // -> ['..', 'word', '1', '..']
1241
1239
*/
1242
- XRegExp . split = ( str , separator , limit ) => fixed . split . call ( toObject ( str ) , separator , limit ) ;
1240
+ XRegExp . split = ( str , separator , limit ) => fixed . split . call ( nullThrows ( str ) , separator , limit ) ;
1243
1241
1244
1242
/**
1245
1243
* Executes a regex search in a specified string. Returns `true` or `false`. Optional `pos` and
@@ -1471,7 +1469,7 @@ fixed.match = function(regex) {
1471
1469
return result ;
1472
1470
}
1473
1471
1474
- return fixed . exec . call ( regex , toObject ( this ) ) ;
1472
+ return fixed . exec . call ( regex , nullThrows ( this ) ) ;
1475
1473
} ;
1476
1474
1477
1475
/**
@@ -1535,7 +1533,7 @@ fixed.replace = function(search, replacement) {
1535
1533
} else {
1536
1534
// Ensure that the last value of `args` will be a string when given nonstring `this`,
1537
1535
// while still throwing on null or undefined context
1538
- result = String ( toObject ( this ) ) . replace ( search , ( ...args ) => {
1536
+ result = String ( nullThrows ( this ) ) . replace ( search , ( ...args ) => {
1539
1537
return String ( replacement ) . replace ( replacementToken , replacer ) ;
1540
1538
1541
1539
function replacer ( $0 , bracketed , angled , dollarToken ) {
0 commit comments