locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
22 lines (21 loc) • 781 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.first = first;
function first(needle, haystack, startIndex) {
// discuss at: https://locutus.io/tcl/first/
// parity verified: Tcl 8.6
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: first('or', 'Hello World')
// returns 1: 7
// example 2: first('x', 'Hello World')
// returns 2: -1
// example 3: first('', 'abc', 2)
// returns 3: -1
const source = String(haystack);
const search = String(needle);
const offset = startIndex === undefined ? 0 : Math.max(0, Math.trunc(Number(startIndex)));
if (search === '') {
return -1;
}
return source.indexOf(search, offset);
}