UNPKG

locutus

Version:

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

24 lines (23 loc) 853 B
export function last(needle, haystack, lastIndex) { // discuss at: https://locutus.io/tcl/last/ // parity verified: Tcl 8.6 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: last('or', 'Hello World') // returns 1: 7 // example 2: last('l', 'Hello World') // returns 2: 9 // example 3: last('l', 'Hello World', 3) // returns 3: 3 const source = String(haystack); const search = String(needle); if (search === '') { if (lastIndex === undefined) { return source.length; } return Math.min(Math.max(0, Math.trunc(Number(lastIndex))), source.length); } if (lastIndex === undefined) { return source.lastIndexOf(search); } return source.lastIndexOf(search, Math.trunc(Number(lastIndex))); }