UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

21 lines (20 loc) 812 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lastindexof = lastindexof; function lastindexof(needle, haystack, startIndex) { // discuss at: https://locutus.io/powershell/lastindexof/ // parity verified: PowerShell 7.4 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: lastindexof('o', 'Hello World') // returns 1: 7 // example 2: lastindexof('x', 'Hello World') // returns 2: -1 // example 3: lastindexof('o', 'Hello World', 6) // returns 3: 4 const source = String(haystack); const search = String(needle); if (startIndex === undefined) { return source.lastIndexOf(search); } return source.lastIndexOf(search, Math.trunc(Number(startIndex))); }