Skip to content

Commit 5a0931c

Browse files
committed
code cleanup
1 parent 9b63588 commit 5a0931c

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/xregexp.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ const replacementToken = /\$(?:\{([^\}]+)\}|<([^>]+)>|(\d\d?|[\s\S]?))/g;
4646
const correctExecNpcg = /()??/.exec('')[1] === undefined;
4747
// Check for ES6 `flags` prop support
4848
const hasFlagsProp = /x/.flags !== undefined;
49-
// Shortcut to `Object.prototype.toString`
50-
const {toString} = {};
5149

5250
function hasNativeFlag(flag) {
5351
// Can't check based on the presence of properties/getters since browsers might support such
@@ -327,7 +325,24 @@ function isQuantifierNext(pattern, pos, flags) {
327325
* @returns {boolean} Whether the object matches the type.
328326
*/
329327
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;
331346
}
332347

333348
/**
@@ -485,23 +500,6 @@ function setNamespacing(on) {
485500
features.namespacing = on;
486501
}
487502

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-
505503
// ==--------------------------==
506504
// Constructor
507505
// ==--------------------------==
@@ -785,7 +783,7 @@ XRegExp.cache.flush = (cacheName) => {
785783
* XRegExp.escape('Escaped? <.>');
786784
* // -> 'Escaped\?\ <\.>'
787785
*/
788-
XRegExp.escape = (str) => String(toObject(str)).replace(/[-\[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
786+
XRegExp.escape = (str) => String(nullThrows(str)).replace(/[-\[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
789787

790788
/**
791789
* 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]);
982980
* XRegExp.isRegExp(RegExp('^', 'm')); // -> true
983981
* XRegExp.isRegExp(XRegExp('(?s).')); // -> true
984982
*/
985-
XRegExp.isRegExp = (value) => toString.call(value) === '[object RegExp]'; // isType(value, 'RegExp');
983+
XRegExp.isRegExp = (value) => isType(value, 'RegExp');
986984

987985
/**
988986
* Returns the first matched string, or in global mode, an array containing all matched strings.
@@ -1026,7 +1024,7 @@ XRegExp.match = (str, regex, scope) => {
10261024
})
10271025
);
10281026

1029-
const result = String(toObject(str)).match(r2);
1027+
const result = String(nullThrows(str)).match(r2);
10301028

10311029
if (regex.global) {
10321030
regex.lastIndex = (
@@ -1172,7 +1170,7 @@ XRegExp.replace = (str, search, replacement, scope) => {
11721170
}
11731171

11741172
// 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);
11761174

11771175
if (isRegex && search.global) {
11781176
// Fixes IE, Safari bug (last tested IE 9, Safari 5.1)
@@ -1239,7 +1237,7 @@ XRegExp.replaceEach = (str, replacements) => {
12391237
* XRegExp.split('..word1..', /([a-z]+)(\d+)/i);
12401238
* // -> ['..', 'word', '1', '..']
12411239
*/
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);
12431241

12441242
/**
12451243
* Executes a regex search in a specified string. Returns `true` or `false`. Optional `pos` and
@@ -1471,7 +1469,7 @@ fixed.match = function(regex) {
14711469
return result;
14721470
}
14731471

1474-
return fixed.exec.call(regex, toObject(this));
1472+
return fixed.exec.call(regex, nullThrows(this));
14751473
};
14761474

14771475
/**
@@ -1535,7 +1533,7 @@ fixed.replace = function(search, replacement) {
15351533
} else {
15361534
// Ensure that the last value of `args` will be a string when given nonstring `this`,
15371535
// while still throwing on null or undefined context
1538-
result = String(toObject(this)).replace(search, (...args) => {
1536+
result = String(nullThrows(this)).replace(search, (...args) => {
15391537
return String(replacement).replace(replacementToken, replacer);
15401538

15411539
function replacer($0, bracketed, angled, dollarToken) {

0 commit comments

Comments
 (0)