Skip to content
Permalink
Newer
Older
100644 40 lines (31 sloc) 1.08 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
var callBound = require('call-bind/callBound');
5
6
var $TypeError = GetIntrinsic('%TypeError%');
7
8
var Type = require('./Type');
9
10
var isInteger = require('../helpers/isInteger');
11
12
var $slice = callBound('String.prototype.slice');
13
14
// https://262.ecma-international.org/12.0/#sec-stringindexof
15
16
module.exports = function StringIndexOf(string, searchValue, fromIndex) {
17
if (Type(string) !== 'String') {
18
throw new $TypeError('Assertion failed: `string` must be a String');
19
}
20
if (Type(searchValue) !== 'String') {
21
throw new $TypeError('Assertion failed: `searchValue` must be a String');
22
}
23
if (!isInteger(fromIndex) || fromIndex < 0) {
24
throw new $TypeError('Assertion failed: `fromIndex` must be a non-negative integer');
25
}
26
27
var len = string.length;
28
if (searchValue === '' && fromIndex <= len) {
29
return fromIndex;
30
}
31
32
var searchLen = searchValue.length;
33
for (var i = fromIndex; i <= (len - searchLen); i += 1) {
34
var candidate = $slice(string, i, i + searchLen);
35
if (candidate === searchValue) {
36
return i;
37
}
38
}
39
return -1;
40
};