|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.0 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var callBound = require('call-bound');
|
|---|
| 4 |
|
|---|
| 5 | var $TypeError = require('es-errors/type');
|
|---|
| 6 | var isInteger = require('math-intrinsics/isInteger');
|
|---|
| 7 |
|
|---|
| 8 | var $slice = callBound('String.prototype.slice');
|
|---|
| 9 |
|
|---|
| 10 | // https://262.ecma-international.org/12.0/#sec-stringindexof
|
|---|
| 11 |
|
|---|
| 12 | module.exports = function StringIndexOf(string, searchValue, fromIndex) {
|
|---|
| 13 | if (typeof string !== 'string') {
|
|---|
| 14 | throw new $TypeError('Assertion failed: `string` must be a String');
|
|---|
| 15 | }
|
|---|
| 16 | if (typeof searchValue !== 'string') {
|
|---|
| 17 | throw new $TypeError('Assertion failed: `searchValue` must be a String');
|
|---|
| 18 | }
|
|---|
| 19 | if (!isInteger(fromIndex) || fromIndex < 0) {
|
|---|
| 20 | throw new $TypeError('Assertion failed: `fromIndex` must be a non-negative integer');
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | var len = string.length;
|
|---|
| 24 | if (searchValue === '' && fromIndex <= len) {
|
|---|
| 25 | return fromIndex;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | var searchLen = searchValue.length;
|
|---|
| 29 | for (var i = fromIndex; i <= (len - searchLen); i += 1) {
|
|---|
| 30 | var candidate = $slice(string, i, i + searchLen);
|
|---|
| 31 | if (candidate === searchValue) {
|
|---|
| 32 | return i;
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | return -1;
|
|---|
| 36 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.