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