|
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.1 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 |
|
|---|
| 5 | var substring = require('./substring');
|
|---|
| 6 |
|
|---|
| 7 | var isInteger = require('../helpers/isInteger');
|
|---|
| 8 |
|
|---|
| 9 | // https://262.ecma-international.org/16.0/#sec-stringlastindexof
|
|---|
| 10 |
|
|---|
| 11 | module.exports = function StringLastIndexOf(string, searchValue, fromIndex) {
|
|---|
| 12 | if (typeof string !== 'string') {
|
|---|
| 13 | throw new $TypeError('Assertion failed: `string` must be a string');
|
|---|
| 14 | }
|
|---|
| 15 | if (typeof searchValue !== 'string') {
|
|---|
| 16 | throw new $TypeError('Assertion failed: `searchValue` must be a string');
|
|---|
| 17 | }
|
|---|
| 18 | if (!isInteger(fromIndex) || fromIndex < 0) {
|
|---|
| 19 | throw new $TypeError('Assertion failed: `fromIndex` must be a non-negative integer');
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | var len = string.length; // step 1
|
|---|
| 23 |
|
|---|
| 24 | var searchLen = searchValue.length; // step 2
|
|---|
| 25 |
|
|---|
| 26 | if (!((fromIndex + searchLen) <= len)) {
|
|---|
| 27 | throw new $TypeError('Assertion failed: fromIndex + searchLen ≤ len'); // step 3
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | for (var i = fromIndex; i >= 0; i--) { // step 4
|
|---|
| 31 | var candidate = substring(string, i, i + searchLen); // step 4.a
|
|---|
| 32 | if (candidate === searchValue) {
|
|---|
| 33 | return i; // step 4.b
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | return 'NOT-FOUND'; // step 5
|
|---|
| 38 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.