UNPKG

1.64 kBJavaScriptView Raw
1'use strict';
2var call = require('../internals/function-call');
3var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
4var anObject = require('../internals/an-object');
5var isNullOrUndefined = require('../internals/is-null-or-undefined');
6var requireObjectCoercible = require('../internals/require-object-coercible');
7var sameValue = require('../internals/same-value');
8var toString = require('../internals/to-string');
9var getMethod = require('../internals/get-method');
10var regExpExec = require('../internals/regexp-exec-abstract');
11
12// @@search logic
13fixRegExpWellKnownSymbolLogic('search', function (SEARCH, nativeSearch, maybeCallNative) {
14 return [
15 // `String.prototype.search` method
16 // https://tc39.es/ecma262/#sec-string.prototype.search
17 function search(regexp) {
18 var O = requireObjectCoercible(this);
19 var searcher = isNullOrUndefined(regexp) ? undefined : getMethod(regexp, SEARCH);
20 return searcher ? call(searcher, regexp, O) : new RegExp(regexp)[SEARCH](toString(O));
21 },
22 // `RegExp.prototype[@@search]` method
23 // https://tc39.es/ecma262/#sec-regexp.prototype-@@search
24 function (string) {
25 var rx = anObject(this);
26 var S = toString(string);
27 var res = maybeCallNative(nativeSearch, rx, S);
28
29 if (res.done) return res.value;
30
31 var previousLastIndex = rx.lastIndex;
32 if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
33 var result = regExpExec(rx, S);
34 if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex;
35 return result === null ? -1 : result.index;
36 }
37 ];
38});